Merge commit 'b1e7e97d3b60469b243b3b2e22c7d8cbd11c7c90'
[unleashed.git] / usr / src / cmd / svc / common / manifest_hash.c
blobd8a13464ec3bcf78588c7ccb2cdc603934d6174a
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
27 #include <sys/stat.h>
28 #include <sys/types.h>
30 #include <assert.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <libintl.h>
35 #include <libscf.h>
36 #include <libuutil.h>
37 #include <limits.h>
38 #include <md5.h>
39 #include <pthread.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <strings.h>
44 #include <unistd.h>
46 #include <manifest_hash.h>
49 * Translate a file name to property name. Return an allocated string or NULL
50 * if realpath() fails. If deathrow is true, realpath() is skipped. This
51 * allows to return the property name even if the file doesn't exist.
53 char *
54 mhash_filename_to_propname(const char *in, boolean_t deathrow)
56 char *out, *cp, *base;
57 size_t len, piece_len;
58 size_t base_sz = 0;
60 out = uu_zalloc(PATH_MAX + 1);
61 if (deathrow) {
62 /* used only for service deathrow handling */
63 if (strlcpy(out, in, PATH_MAX + 1) >= (PATH_MAX + 1)) {
64 uu_free(out);
65 return (NULL);
67 } else {
68 if (realpath(in, out) == NULL) {
69 uu_free(out);
70 return (NULL);
74 base = getenv("PKG_INSTALL_ROOT");
77 * We copy-shift over the basedir and the leading slash, since it's
78 * not relevant to when we boot with this repository.
81 if (base != NULL && strncmp(out, base, strlen(base)) == 0)
82 base_sz = strlen(base);
84 cp = out + base_sz;
85 if (*cp == '/')
86 cp++;
87 (void) memmove(out, cp, strlen(cp) + 1);
89 len = strlen(out);
90 if (len > scf_limit(SCF_LIMIT_MAX_NAME_LENGTH)) {
91 /* Use the first half and the second half. */
92 piece_len = (scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) - 3) / 2;
94 (void) strncpy(out + piece_len, "__", 2);
96 (void) memmove(out + piece_len + 2, out + (len - piece_len),
97 piece_len + 1);
101 * Translate non-property characters to '_', first making sure that
102 * we don't begin with '_'.
105 if (!isalpha(*out))
106 *out = 'A';
108 for (cp = out + 1; *cp != '\0'; ++cp) {
109 if (!(isalnum(*cp) || *cp == '_' || *cp == '-'))
110 *cp = '_';
113 return (out);
117 mhash_retrieve_entry(scf_handle_t *hndl, const char *name, uchar_t *hash,
118 apply_action_t *action)
120 scf_scope_t *scope;
121 scf_service_t *svc;
122 scf_propertygroup_t *pg;
123 scf_property_t *prop;
124 scf_value_t *val;
125 scf_error_t err;
126 ssize_t szret;
127 int result = 0;
129 if (action)
130 *action = APPLY_NONE;
133 * In this implementation the hash for name is the opaque value of
134 * svc:/MHASH_SVC/:properties/name/MHASH_PROP
137 scope = scf_scope_create(hndl);
138 svc = scf_service_create(hndl);
139 pg = scf_pg_create(hndl);
140 prop = scf_property_create(hndl);
141 val = scf_value_create(hndl);
142 if (scope == NULL || svc == NULL || pg == NULL || prop == NULL ||
143 val == NULL) {
144 result = -1;
145 goto out;
148 if (scf_handle_get_local_scope(hndl, scope) < 0) {
149 result = -1;
150 goto out;
153 if (scf_scope_get_service(scope, MHASH_SVC, svc) < 0) {
154 result = -1;
155 goto out;
158 if (scf_service_get_pg(svc, name, pg) != SCF_SUCCESS) {
159 result = -1;
160 goto out;
163 if (scf_pg_get_property(pg, MHASH_PROP, prop) != SCF_SUCCESS) {
164 result = -1;
165 goto out;
168 if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
169 result = -1;
170 goto out;
173 szret = scf_value_get_opaque(val, hash, MHASH_SIZE);
174 if (szret < 0) {
175 result = -1;
176 goto out;
180 * Make sure that the old hash is returned with
181 * remainder of the bytes zeroed.
183 if (szret == MHASH_SIZE_OLD) {
184 (void) memset(hash + MHASH_SIZE_OLD, 0,
185 MHASH_SIZE - MHASH_SIZE_OLD);
186 } else if (szret != MHASH_SIZE) {
187 scf_value_destroy(val);
188 result = -1;
189 goto out;
193 * If caller has requested the apply_last property, read the
194 * property if it exists.
196 if (action != NULL) {
197 uint8_t apply_value;
199 if (scf_pg_get_property(pg, MHASH_APPLY_PROP, prop) !=
200 SCF_SUCCESS) {
201 err = scf_error();
202 if ((err != SCF_ERROR_DELETED) &&
203 (err != SCF_ERROR_NOT_FOUND)) {
204 result = -1;
206 goto out;
208 if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
209 err = scf_error();
210 if ((err != SCF_ERROR_DELETED) &&
211 (err != SCF_ERROR_NOT_FOUND)) {
212 result = -1;
214 goto out;
216 if (scf_value_get_boolean(val, &apply_value) != SCF_SUCCESS) {
217 result = -1;
218 goto out;
220 if (apply_value)
221 *action = APPLY_LATE;
224 out:
225 (void) scf_value_destroy(val);
226 scf_property_destroy(prop);
227 scf_pg_destroy(pg);
228 scf_service_destroy(svc);
229 scf_scope_destroy(scope);
231 return (result);
235 mhash_store_entry(scf_handle_t *hndl, const char *name, const char *fname,
236 uchar_t *hash, apply_action_t apply_late, char **errstr)
238 scf_scope_t *scope = NULL;
239 scf_service_t *svc = NULL;
240 scf_propertygroup_t *pg = NULL;
241 scf_property_t *prop = NULL;
242 scf_value_t *aval = NULL;
243 scf_value_t *val = NULL;
244 scf_value_t *fval = NULL;
245 scf_transaction_t *tx = NULL;
246 scf_transaction_entry_t *ae = NULL;
247 scf_transaction_entry_t *e = NULL;
248 scf_transaction_entry_t *fe = NULL;
249 scf_error_t err;
250 int ret, result = 0;
251 char *base;
252 size_t base_sz = 0;
254 int i;
256 if ((scope = scf_scope_create(hndl)) == NULL ||
257 (svc = scf_service_create(hndl)) == NULL ||
258 (pg = scf_pg_create(hndl)) == NULL ||
259 (prop = scf_property_create(hndl)) == NULL) {
260 if (errstr != NULL)
261 *errstr = gettext("Could not create scf objects");
262 result = -1;
263 goto out;
266 if (scf_handle_get_local_scope(hndl, scope) != SCF_SUCCESS) {
267 if (errstr != NULL)
268 *errstr = gettext("Could not get local scope");
269 result = -1;
270 goto out;
273 for (i = 0; i < 5; ++i) {
275 if (scf_scope_get_service(scope, MHASH_SVC, svc) ==
276 SCF_SUCCESS)
277 break;
279 if (scf_error() != SCF_ERROR_NOT_FOUND) {
280 if (errstr != NULL)
281 *errstr = gettext("Could not get manifest hash "
282 "service");
283 result = -1;
284 goto out;
287 if (scf_scope_add_service(scope, MHASH_SVC, svc) ==
288 SCF_SUCCESS)
289 break;
291 err = scf_error();
293 if (err == SCF_ERROR_EXISTS)
294 /* Try again. */
295 continue;
296 else if (err == SCF_ERROR_PERMISSION_DENIED) {
297 if (errstr != NULL)
298 *errstr = gettext("Could not store file hash: "
299 "permission denied.\n");
300 result = -1;
301 goto out;
304 if (errstr != NULL)
305 *errstr = gettext("Could not add manifest hash "
306 "service");
307 result = -1;
308 goto out;
311 if (i == 5) {
312 if (errstr != NULL)
313 *errstr = gettext("Could not store file hash: "
314 "service addition contention.\n");
315 result = -1;
316 goto out;
319 for (i = 0; i < 5; ++i) {
320 if (scf_service_get_pg(svc, name, pg) == SCF_SUCCESS)
321 break;
323 if (scf_error() != SCF_ERROR_NOT_FOUND) {
324 if (errstr != NULL)
325 *errstr = gettext("Could not get service's "
326 "hash record)");
327 result = -1;
328 goto out;
331 if (scf_service_add_pg(svc, name, MHASH_PG_TYPE,
332 MHASH_PG_FLAGS, pg) == SCF_SUCCESS)
333 break;
335 err = scf_error();
337 if (err == SCF_ERROR_EXISTS)
338 /* Try again. */
339 continue;
340 else if (err == SCF_ERROR_PERMISSION_DENIED) {
341 if (errstr != NULL)
342 *errstr = gettext("Could not store file hash: "
343 "permission denied.\n");
344 result = -1;
345 goto out;
348 if (errstr != NULL)
349 *errstr = gettext("Could not store file hash");
350 result = -1;
351 goto out;
353 if (i == 5) {
354 if (errstr != NULL)
355 *errstr = gettext("Could not store file hash: "
356 "property group addition contention.\n");
357 result = -1;
358 goto out;
361 if ((e = scf_entry_create(hndl)) == NULL ||
362 (val = scf_value_create(hndl)) == NULL ||
363 (fe = scf_entry_create(hndl)) == NULL ||
364 (fval = scf_value_create(hndl)) == NULL ||
365 (ae = scf_entry_create(hndl)) == NULL ||
366 (aval = scf_value_create(hndl)) == NULL) {
367 if (errstr != NULL)
368 *errstr = gettext("Could not store file hash: "
369 "permission denied.\n");
370 result = -1;
371 goto out;
375 * Remove any PKG_INSTALL_ROOT from the manifest filename so that it
376 * points to the correct location following installation.
378 base = getenv("PKG_INSTALL_ROOT");
379 if (base != NULL && strncmp(fname, base, strlen(base)) == 0)
380 base_sz = strlen(base);
382 ret = scf_value_set_opaque(val, hash, MHASH_SIZE);
383 assert(ret == SCF_SUCCESS);
384 ret = scf_value_set_astring(fval, fname + base_sz);
385 assert(ret == SCF_SUCCESS);
386 if (apply_late == APPLY_LATE) {
387 scf_value_set_boolean(aval, 1);
390 tx = scf_transaction_create(hndl);
391 if (tx == NULL) {
392 if (errstr != NULL)
393 *errstr = gettext("Could not create transaction");
394 result = -1;
395 goto out;
398 do {
399 if (scf_pg_update(pg) == -1) {
400 if (errstr != NULL)
401 *errstr = gettext("Could not update hash "
402 "entry");
403 result = -1;
404 goto out;
406 if (scf_transaction_start(tx, pg) != SCF_SUCCESS) {
407 if (scf_error() != SCF_ERROR_PERMISSION_DENIED) {
408 if (errstr != NULL)
409 *errstr = gettext("Could not start "
410 "hash transaction.\n");
411 result = -1;
412 goto out;
415 if (errstr != NULL)
416 *errstr = gettext("Could not store file hash: "
417 "permission denied.\n");
418 result = -1;
420 scf_transaction_destroy(tx);
421 (void) scf_entry_destroy(e);
422 goto out;
425 if (scf_transaction_property_new(tx, e, MHASH_PROP,
426 SCF_TYPE_OPAQUE) != SCF_SUCCESS &&
427 scf_transaction_property_change_type(tx, e, MHASH_PROP,
428 SCF_TYPE_OPAQUE) != SCF_SUCCESS) {
429 if (errstr != NULL)
430 *errstr = gettext("Could not modify hash "
431 "entry");
432 result = -1;
433 goto out;
436 ret = scf_entry_add_value(e, val);
437 assert(ret == SCF_SUCCESS);
439 if (scf_transaction_property_new(tx, fe, MHASH_FILE_PROP,
440 SCF_TYPE_ASTRING) != SCF_SUCCESS &&
441 scf_transaction_property_change_type(tx, fe,
442 MHASH_FILE_PROP, SCF_TYPE_ASTRING) != SCF_SUCCESS) {
443 if (errstr != NULL)
444 *errstr = gettext("Could not modify file "
445 "entry");
446 result = -1;
447 goto out;
450 ret = scf_entry_add_value(fe, fval);
451 assert(ret == SCF_SUCCESS);
453 switch (apply_late) {
454 case APPLY_NONE:
455 if (scf_transaction_property_delete(tx, ae,
456 MHASH_APPLY_PROP) != 0) {
457 err = scf_error();
458 if ((err != SCF_ERROR_DELETED) &&
459 (err != SCF_ERROR_NOT_FOUND)) {
460 if (errstr != NULL) {
461 *errstr = gettext("Could not "
462 "delete apply_late "
463 "property");
465 result = -1;
466 goto out;
469 break;
470 case APPLY_LATE:
471 if ((scf_transaction_property_new(tx, ae,
472 MHASH_APPLY_PROP,
473 SCF_TYPE_BOOLEAN) != SCF_SUCCESS) &&
474 (scf_transaction_property_change_type(tx, ae,
475 MHASH_APPLY_PROP, SCF_TYPE_BOOLEAN) !=
476 SCF_SUCCESS)) {
477 if (errstr != NULL) {
478 *errstr = gettext("Could not modify "
479 "apply_late property");
481 result = -1;
482 goto out;
485 ret = scf_entry_add_value(ae, aval);
486 assert(ret == SCF_SUCCESS);
487 break;
488 default:
489 abort();
492 ret = scf_transaction_commit(tx);
494 if (ret == 0)
495 scf_transaction_reset(tx);
496 } while (ret == 0);
498 if (ret < 0) {
499 if (scf_error() != SCF_ERROR_PERMISSION_DENIED) {
500 if (errstr != NULL)
501 *errstr = gettext("Could not store file hash: "
502 "permission denied.\n");
503 result = -1;
504 goto out;
507 if (errstr != NULL)
508 *errstr = gettext("Could not commit transaction");
509 result = -1;
512 scf_transaction_destroy(tx);
513 (void) scf_entry_destroy(e);
514 (void) scf_entry_destroy(fe);
515 (void) scf_entry_destroy(ae);
517 out:
518 (void) scf_value_destroy(val);
519 (void) scf_value_destroy(fval);
520 (void) scf_value_destroy(aval);
521 scf_property_destroy(prop);
522 scf_pg_destroy(pg);
523 scf_service_destroy(svc);
524 scf_scope_destroy(scope);
526 return (result);
530 * Generate the md5 hash of a file; manifest files are smallish
531 * so we can read them in one gulp.
533 static int
534 md5_hash_file(const char *file, off64_t sz, uchar_t *hash)
536 char *buf;
537 int fd;
538 ssize_t res;
539 int ret;
541 fd = open(file, O_RDONLY);
542 if (fd < 0)
543 return (-1);
545 buf = malloc(sz);
546 if (buf == NULL) {
547 (void) close(fd);
548 return (-1);
551 res = read(fd, buf, (size_t)sz);
553 (void) close(fd);
555 if (res == sz) {
556 ret = 0;
557 md5_calc(hash, (uchar_t *)buf, (unsigned int) sz);
558 } else {
559 ret = -1;
562 free(buf);
563 return (ret);
567 * int mhash_test_file(scf_handle_t *, const char *, uint_t, char **, uchar_t *)
568 * Test the given filename against the hashed metadata in the repository.
569 * The behaviours for import and apply are slightly different. For imports,
570 * if the hash value is absent or different, then the import operation
571 * continues. For profile application, the operation continues only if the
572 * hash value for the file is absent.
574 * We keep two hashes: one which can be quickly test: the metadata hash,
575 * and one which is more expensive to test: the file contents hash.
577 * If either hash matches, the file does not need to be re-read.
578 * If only one of the hashes matches, a side effect of this function
579 * is to store the newly computed hash.
580 * If neither hash matches, the hash computed for the new file is returned
581 * and not stored.
583 * Return values:
584 * MHASH_NEWFILE - the file no longer matches the hash or no hash existed
585 * ONLY in this case we return the new file's hash.
586 * MHASH_FAILURE - an internal error occurred, or the file was not found.
587 * MHASH_RECONCILED- based on the metadata/file hash, the file does
588 * not need to be re-read; if necessary,
589 * the hash was upgraded or reconciled.
591 * NOTE: no hash is returned UNLESS MHASH_NEWFILE is returned.
594 mhash_test_file(scf_handle_t *hndl, const char *file, uint_t is_profile,
595 char **pnamep, uchar_t *hashbuf)
597 apply_action_t action;
598 boolean_t do_hash;
599 struct stat64 st;
600 char *cp;
601 char *data;
602 uchar_t stored_hash[MHASH_SIZE];
603 uchar_t hash[MHASH_SIZE];
604 char *pname;
605 int ret;
606 int hashash;
607 int metahashok = 0;
609 if (pnamep)
610 *pnamep = NULL;
613 * In the case where we are doing automated imports, we reduce the UID,
614 * the GID, the size, and the mtime into a string (to eliminate
615 * endianness) which we then make opaque as a single MD5 digest.
617 * The previous hash was composed of the inode number, the UID, the file
618 * size, and the mtime. This formulation was found to be insufficiently
619 * portable for use in highly replicated deployments. The current
620 * algorithm will allow matches of this "v1" hash, but always returns
621 * the effective "v2" hash, such that updates result in the more
622 * portable hash being used.
624 * An unwanted side effect of a hash based solely on the file
625 * meta data is the fact that we pay no attention to the contents
626 * which may remain the same despite meta data changes. This happens
627 * with (live) upgrades. We extend the V2 hash with an additional
628 * digest of the file contents and the code retrieving the hash
629 * from the repository zero fills the remainder so we can detect
630 * it is missing.
632 * If the the V2 digest matches, we check for the presence of
633 * the contents digest and compute and store it if missing.
635 * If the V2 digest doesn't match but we also have a non-zero
636 * file hash, we match the file content digest. If it matches,
637 * we compute and store the new complete hash so that later
638 * checks will find the meta data digest correct.
640 * If the above matches fail and the V1 hash doesn't match either,
641 * we consider the test to have failed, implying that some aspect
642 * of the manifest has changed.
645 cp = getenv("SVCCFG_CHECKHASH");
646 do_hash = (cp != NULL && *cp != '\0');
647 if (!do_hash) {
648 return (MHASH_NEWFILE);
651 pname = mhash_filename_to_propname(file, B_FALSE);
652 if (pname == NULL)
653 return (MHASH_FAILURE);
655 hashash = mhash_retrieve_entry(hndl, pname, stored_hash, &action) == 0;
656 if (is_profile == 0) {
657 /* Actions other than APPLY_NONE are restricted to profiles. */
658 assert(action == APPLY_NONE);
662 * As a general rule, we do not reread a profile. The exception to
663 * this rule is when we are running as part of the manifest import
664 * service and the apply_late property is set to true.
666 if (hashash && is_profile) {
667 cp = getenv("SMF_FMRI");
668 if ((cp == NULL) ||
669 (strcmp(cp, SCF_INSTANCE_MI) != 0) ||
670 (action != APPLY_LATE)) {
671 uu_free(pname);
672 return (MHASH_RECONCILED);
677 * No hash and not interested in one, then don't bother computing it.
678 * We also skip returning the property name in that case.
680 if (!hashash && hashbuf == NULL) {
681 uu_free(pname);
682 return (MHASH_NEWFILE);
685 do {
686 ret = stat64(file, &st);
687 } while (ret < 0 && errno == EINTR);
688 if (ret < 0) {
689 uu_free(pname);
690 return (MHASH_FAILURE);
693 data = uu_msprintf(MHASH_FORMAT_V2, st.st_uid, st.st_gid,
694 st.st_size, st.st_mtime);
695 if (data == NULL) {
696 uu_free(pname);
697 return (MHASH_FAILURE);
700 (void) memset(hash, 0, MHASH_SIZE);
701 md5_calc(hash, (uchar_t *)data, strlen(data));
703 uu_free(data);
706 * Verify the meta data hash.
708 if (hashash && memcmp(hash, stored_hash, MD5_DIGEST_LENGTH) == 0) {
709 int i;
711 metahashok = 1;
713 * The metadata hash matches; now we see if there was a
714 * content hash; if not, we will continue on and compute and
715 * store the updated hash.
716 * If there was no content hash, mhash_retrieve_entry()
717 * will have zero filled it.
719 for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
720 if (stored_hash[MD5_DIGEST_LENGTH+i] != 0) {
721 if (action == APPLY_LATE) {
722 if (pnamep != NULL)
723 *pnamep = pname;
724 ret = MHASH_NEWFILE;
725 } else {
726 uu_free(pname);
727 ret = MHASH_RECONCILED;
729 return (ret);
735 * Compute the file hash as we can no longer avoid having to know it.
736 * Note: from this point on "hash" contains the full, current, hash.
738 if (md5_hash_file(file, st.st_size, &hash[MHASH_SIZE_OLD]) != 0) {
739 uu_free(pname);
740 return (MHASH_FAILURE);
742 if (hashash) {
743 uchar_t hash_v1[MHASH_SIZE_OLD];
745 if (metahashok ||
746 memcmp(&hash[MHASH_SIZE_OLD], &stored_hash[MHASH_SIZE_OLD],
747 MD5_DIGEST_LENGTH) == 0) {
750 * Reconcile entry: we get here when either the
751 * meta data hash matches or the content hash matches;
752 * we then update the database with the complete
753 * new hash so we can be a bit quicker next time.
755 (void) mhash_store_entry(hndl, pname, file, hash,
756 APPLY_NONE, NULL);
757 if (action == APPLY_LATE) {
758 if (pnamep != NULL)
759 *pnamep = pname;
760 ret = MHASH_NEWFILE;
761 } else {
762 uu_free(pname);
763 ret = MHASH_RECONCILED;
765 return (ret);
769 * No match on V2 hash or file content; compare V1 hash.
771 data = uu_msprintf(MHASH_FORMAT_V1, st.st_ino, st.st_uid,
772 st.st_size, st.st_mtime);
773 if (data == NULL) {
774 uu_free(pname);
775 return (MHASH_FAILURE);
778 md5_calc(hash_v1, (uchar_t *)data, strlen(data));
780 uu_free(data);
782 if (memcmp(hash_v1, stored_hash, MD5_DIGEST_LENGTH) == 0) {
784 * Update the new entry so we don't have to go through
785 * all this trouble next time.
787 (void) mhash_store_entry(hndl, pname, file, hash,
788 APPLY_NONE, NULL);
789 uu_free(pname);
790 return (MHASH_RECONCILED);
794 if (pnamep != NULL)
795 *pnamep = pname;
796 else
797 uu_free(pname);
799 if (hashbuf != NULL)
800 (void) memcpy(hashbuf, hash, MHASH_SIZE);
802 return (MHASH_NEWFILE);