Skip to contentSkip to conversation

libsemanage: backport sha256 WaterJuice/WjCryptLib 3.0.0 fixes

PATCH In review Petr Lautrbach 2 participants 5 messages Oct 5 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 libsemanage: backport sha256 WaterJuice/WjCryptLib 3.0.0 fixes

https://github.com/WaterJuice/WjCryptLib/releases/tag/Version_3.0.0

---
 libsemanage/src/sha256.c | 4 ++--
 libsemanage/src/sha256.h | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
libsemanage/src/sha256.c +2 -2
index a62ebd6f20fa..1fc80cefa5a5 100644
@@ -181,7 +181,7 @@ void Sha256Update(Sha256Context *Context, // [in out]
 {
 	uint32_t n;
 
-	if (Context->curlen > sizeof(Context->buf)) {
+	if (Context->curlen >= sizeof(Context->buf)) {
 		return;
 	}
 
@@ -263,7 +263,7 @@ void Sha256Finalise(Sha256Context *Context, // [in out]
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 void Sha256Calculate(void const *Buffer, // [in]
 		     size_t BufferSize, // [in]
-		     SHA256_HASH *Digest // [in]
+		     SHA256_HASH *Digest // [out]
 )
 {
 	Sha256Context context;
libsemanage/src/sha256.h +1 -1
index 1390c1bd481e..a55a5bcdb5f8 100644
@@ -71,5 +71,5 @@ void Sha256Finalise(Sha256Context *Context, // [in out]
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 void Sha256Calculate(void const *Buffer, // [in]
 		     size_t BufferSize, // [in]
-		     SHA256_HASH *Digest // [in]
+		     SHA256_HASH *Digest // [out]
 );
Signature · 1 line
2.55.0
#1 lore raw

PATCH v2 2/2 libselinux: Use SHA256 for label digests

SHA-1 is considered obsolete

Use hashing code from libsemanage/src/sha256.*

Provide versioned selabel_hash_all_partial_matches() symbol
- selabel_hash_all_partial_matches@LIBSELINUX_1.0 generates original SHA1 digest and uses 20 bytes (SHA1_HASH_SIZE)
- selabel_hash_all_partial_matches@@LIBSELINUX_3.12 generates SHA256 digest and uses 32 bytes (SHA256_HASH_SIZE)

All external selabel_hash_all_partial_matches() users has to make sure that they provide "uint8_t *digest" with size at least 32 bytes.

The change in digest means that the first run of restorecon -D ... or setfiles -D ... after libselinux update will resets all directory SHA256 digest. This could cause one time performance hit.

---
v1 - https://lore.kernel.org/selinux/[email protected]/

v2 changes:

-- libselinux/src/sha256.* are copies of libsemanage/src/sha256.* -- improved commit message
-- selabel_hash_all_partial_matches is symbol versioned

 libselinux/include/selinux/label.h            |   6 +-
 libselinux/include/selinux/restorecon.h       |   4 +-
 libselinux/man/man3/selabel_digest.3          |   4 +-
 libselinux/man/man3/selabel_open.3            |   2 +-
 libselinux/man/man3/selinux_restorecon.3      |  18 +-
 .../man/man3/selinux_restorecon_xattr.3       |   2 +-
 libselinux/src/Makefile                       |   2 +-
 libselinux/src/label.c                        |  24 +-
 libselinux/src/label_file.c                   |  64 +++-
 libselinux/src/label_internal.h               |  12 +-
 libselinux/src/label_support.c                |  17 +-
 libselinux/src/libselinux.map                 |   5 +-
 libselinux/src/selinux_restorecon.c           |  42 ++-
 libselinux/src/sha256.c                       | 274 ++++++++++++++++++
 libselinux/src/sha256.h                       |  75 +++++
 libselinux/utils/selabel_digest.c             |  26 +-
 .../selabel_get_digests_all_partial_matches.c |  30 +-
 policycoreutils/setfiles/restorecon.8         |  10 +-
 policycoreutils/setfiles/restorecon_xattr.8   |   8 +-
 policycoreutils/setfiles/restorecon_xattr.c   |  12 +-
 policycoreutils/setfiles/setfiles.8           |  10 +-
 21 files changed, 531 insertions(+), 116 deletions(-)
 create mode 100644 libselinux/src/sha256.c
 create mode 100644 libselinux/src/sha256.h
libselinux/include/selinux/label.h +3 -3
index 994b3c9cf0a1..dc136d3589b5 100644
@@ -129,13 +129,13 @@ extern int selabel_lookup_best_match_raw(struct selabel_handle *handle,
 					 const char **aliases, int type);
 
 /**
- * selabel_digest - Retrieve the SHA1 digest and the list of specfiles used to
+ * selabel_digest - Retrieve the SHA256 digest and the list of specfiles used to
  *		    generate the digest. The SELABEL_OPT_DIGEST option must
  *		    be set in selabel_open() to initiate the digest generation.
  * @handle: specifies backend instance to query
- * @digest: returns a pointer to the SHA1 digest.
+ * @digest: returns a pointer to the SHA256 digest.
  * @digest_len: returns length of digest in bytes.
- * @specfiles: a list of specfiles used in the SHA1 digest generation.
+ * @specfiles: a list of specfiles used in the SHA256 digest generation.
  *	       The list is NULL terminated and will hold @num_specfiles entries.
  * @num_specfiles: number of specfiles in the list.
  *
libselinux/include/selinux/restorecon.h +2 -2
index 34ba0f43a26c..46fcfb611f67 100644
@@ -43,8 +43,8 @@ extern int selinux_restorecon_parallel(const char *pathname,
  * restorecon_flags options
  */
 /*
- * Force the checking of labels even if the stored SHA1 digest
- * matches the specfiles SHA1 digest (requires CAP_SYS_ADMIN).
+ * Force the checking of labels even if the stored SHA256 digest
+ * matches the specfiles SHA256 digest (requires CAP_SYS_ADMIN).
  */
 #define SELINUX_RESTORECON_IGNORE_DIGEST 0x00001
 /*
libselinux/man/man3/selabel_digest.3 +2 -2
index 56a008f00df0..5f7c42533d0e 100644
@@ -20,11 +20,11 @@ selabel_digest \- Return digest of specfiles and list of files used
 .BR selabel_digest ()
 performs an operation on the handle
 .IR hnd ,
-returning the results of the SHA1 digest pointed to by
+returning the results of the SHA256 digest pointed to by
 .IR digest ,
 whose length will be
 .IR digest_len .
-The list of specfiles used in the SHA1 digest calculation is returned in
+The list of specfiles used in the SHA256 digest calculation is returned in
 .I specfiles
 with the number of entries in
 .IR num_specfiles .
libselinux/man/man3/selabel_open.3 +1 -1
index fa71d27c3482..9df777cdfcf9 100644
@@ -69,7 +69,7 @@ is used; a custom validation function can be provided via
 Note that an invalid context may not be treated as an error unless it is actually encountered during a lookup operation.
 .TP
 .B SELABEL_OPT_DIGEST
-A non-null value for this option enables the generation of an SHA1 digest of
+A non-null value for this option enables the generation of an SHA256 digest of
 the spec files loaded as described in
 .BR selabel_digest (3)
 .
libselinux/man/man3/selinux_restorecon.3 +9 -9
index bafa86a29768..1b1fd58b9d70 100644
@@ -36,7 +36,7 @@ If this is a directory and the
 .B SELINUX_RESTORECON_RECURSE
 has been set (for descending through directories), then
 .BR selinux_restorecon ()
-will write an SHA1 digest of specfile entries calculated by
+will write an SHA256 digest of specfile entries calculated by
 .BR selabel_get_digests_all_partial_matches (3)
 to an extended attribute of
 .IR security.sehash
@@ -55,7 +55,7 @@ will take place.
 .br
 The
 .IR restorecon_flags
-that can be used to manage the usage of the SHA1 digest are:
+that can be used to manage the usage of the SHA256 digest are:
 .RS
 .B SELINUX_RESTORECON_SKIP_DIGEST
 .br
@@ -73,8 +73,8 @@ Do not check or update any extended attribute
 entries.
 .sp
 .B SELINUX_RESTORECON_IGNORE_DIGEST
-force the checking of labels even if the stored SHA1 digest matches the
-specfile entries SHA1 digest. The specfile entries digest will be written to the
+force the checking of labels even if the stored SHA256 digest matches the
+specfile entries SHA256 digest. The specfile entries digest will be written to the
 .IR security.sehash
 extended attribute once relabeling has been completed successfully provided the
 .B SELINUX_RESTORECON_NOCHANGE
@@ -95,7 +95,7 @@ default specfile context.
 .sp
 .B SELINUX_RESTORECON_RECURSE
 change file and directory labels recursively (descend directories)
-and if successful write an SHA1 digest of the specfile entries to an
+and if successful write an SHA256 digest of the specfile entries to an
 extended attribute as described in the
 .B NOTES
 section.
@@ -194,7 +194,7 @@ for fetching the ignored (skipped) error count after
 or
 .BR selinux_restorecon_parallel (3)
 completes with success. In case any errors were skipped during the file tree
-walk, the specfile entries SHA1 digest will not have been written to the
+walk, the specfile entries SHA256 digest will not have been written to the
 .IR security.sehash
 extended attribute.
 .sp
@@ -212,7 +212,7 @@ Use this option when relabeling untrusted directories that might
 contain hard links to files owned by others.
 .RE
 .sp
-The behavior regarding the checking and updating of the SHA1 digest described
+The behavior regarding the checking and updating of the SHA256 digest described
 above is the default behavior. It is possible to change this by first calling
 .BR selabel_open (3)
 and not enabling the
@@ -275,7 +275,7 @@ To improve performance when relabeling file systems recursively (e.g. the
 .B SELINUX_RESTORECON_RECURSE
 flag is set)
 .BR selinux_restorecon ()
-will write a calculated SHA1 digest of the specfile entries returned by
+will write a calculated SHA256 digest of the specfile entries returned by
 .BR selabel_get_digests_all_partial_matches (3)
 to an extended attribute named
 .IR security.sehash
@@ -297,7 +297,7 @@ Should any of the specfile entries have changed, then when
 .BR selinux_restorecon ()
 is run again with the
 .B SELINUX_RESTORECON_RECURSE
-flag set, new SHA1 digests will be calculated and all files automatically
+flag set, new SHA256 digests will be calculated and all files automatically
 relabeled depending on the settings of the
 .B SELINUX_RESTORECON_SET_SPECFILE_CTX
 flag (provided
libselinux/man/man3/selinux_restorecon_xattr.3 +1 -1
index c56326814b94..098c840fc59b 100644
@@ -119,7 +119,7 @@ By default
 .BR selinux_restorecon_xattr (3)
 will use the default set of specfiles described in
 .BR files_contexts (5)
-to calculate the SHA1 digests to be used for comparison.
+to calculate the SHA256 digests to be used for comparison.
 To change this default behavior
 .BR selabel_open (3)
 must be called specifying the required
libselinux/src/Makefile +1 -1
index 9ecdbd4d0af8..7ce094cd0855 100644
@@ -130,7 +130,7 @@ DISABLE_FLAGS+= -DNO_MEDIA_BACKEND -DNO_DB_BACKEND -DNO_X_BACKEND \
 	-DBUILD_HOST
 SRCS= callbacks.c freecon.c label.c label_file.c \
 	label_backends_android.c regex.c label_support.c \
-	matchpathcon.c setrans_client.c sha1.c booleans.c
+	matchpathcon.c setrans_client.c sha1.c sha256.c booleans.c
 LABEL_BACKEND_ANDROID=y
 endif
 
libselinux/src/label.c +20 -4
index 93848bfebd16..03606fa248e6 100644
@@ -338,15 +338,31 @@ bool selabel_get_digests_all_partial_matches(struct selabel_handle *rec,
 		rec, key, calculated_digest, xattr_digest, digest_len);
 }
 
-bool selabel_hash_all_partial_matches(struct selabel_handle *rec,
-				      const char *key, uint8_t *digest)
+bool selabel_hash_all_partial_matches_sha1_compat(struct selabel_handle *rec,
+						  const char *key,
+						  uint8_t *digest);
+bool selabel_hash_all_partial_matches_sha1_compat(struct selabel_handle *rec,
+						  const char *key,
+						  uint8_t *digest)
 {
-	if (!rec->func_hash_all_partial_matches) {
+	if (!rec->func_hash_all_partial_matches_sha1)
+		return false;
+
+	return rec->func_hash_all_partial_matches_sha1(rec, key, digest);
+}
+__asm__(".symver selabel_hash_all_partial_matches_sha1_compat, selabel_hash_all_partial_matches@LIBSELINUX_1.0");
+
+bool selabel_hash_all_partial_matches_sha256(struct selabel_handle *rec,
+					     const char *key, uint8_t *digest);
+bool selabel_hash_all_partial_matches_sha256(struct selabel_handle *rec,
+					     const char *key, uint8_t *digest)
+{
+	if (!rec->func_hash_all_partial_matches)
 		return false;
-	}
 
 	return rec->func_hash_all_partial_matches(rec, key, digest);
 }
+__asm__(".symver selabel_hash_all_partial_matches_sha256, selabel_hash_all_partial_matches@@LIBSELINUX_3.12");
 
 int selabel_lookup_best_match(struct selabel_handle *rec, char **con,
 			      const char *key, const char **aliases, int type)
libselinux/src/label_file.c +49 -15

116 lines not on this page · raw message

libselinux/src/label_internal.h +8 -4
index 47c5d3cb3798..d4d4d3598017 100644
@@ -15,6 +15,7 @@
 #include <selinux/selinux.h>
 #include <selinux/label.h>
 #include "sha1.h"
+#include "sha256.h"
 
 #if defined(ANDROID) || defined(__APPLE__)
 // Android and Mac do not have fgets_unlocked()
@@ -42,15 +43,15 @@ int selabel_service_init(struct selabel_handle *rec,
  */
 
 /*
- * Calculate an SHA1 hash of all the files used to build the specs.
+ * Calculate an SHA256 hash of all the files used to build the specs.
  * The hash value is held in rec->digest if SELABEL_OPT_DIGEST set. To
  * calculate the hash the hashbuf will hold a concatenation of all the files
  * used. This is released once the value has been calculated.
  */
-#define DIGEST_SPECFILE_SIZE SHA1_HASH_SIZE
+#define DIGEST_SPECFILE_SIZE SHA256_HASH_SIZE
 #define DIGEST_FILES_MAX 8
 struct selabel_digest {
-	unsigned char *digest; /* SHA1 digest of specfiles */
+	unsigned char *digest; /* SHA256 digest of specfiles */
 	unsigned char *hashbuf; /* buffer to hold specfiles */
 	size_t hashbuf_size; /* buffer size */
 	size_t specfile_cnt; /* how many specfiles processed */
@@ -88,6 +89,9 @@ struct selabel_handle {
 		size_t *digest_len);
 	bool (*func_hash_all_partial_matches)(struct selabel_handle *h,
 					      const char *key, uint8_t *digest);
+	bool (*func_hash_all_partial_matches_sha1)(struct selabel_handle *h,
+						   const char *key,
+						   uint8_t *digest);
 	struct selabel_lookup_rec *(*func_lookup_best_match)(
 		struct selabel_handle *h, const char *key, const char **aliases,
 		int type);
@@ -103,7 +107,7 @@ struct selabel_handle {
 	size_t spec_files_len;
 	char **spec_files;
 
-	/* ptr to SHA1 hash information if SELABEL_OPT_DIGEST set */
+	/* ptr to SHA256 hash information if SELABEL_OPT_DIGEST set */
 	struct selabel_digest *digest;
 };
 
libselinux/src/label_support.c +8 -9
index 5112c59cdd5c..a229373af09c 100644
@@ -214,28 +214,27 @@ int read_spec_entries(char *line_buf, size_t nread, const char **errbuf,
 /* Once all the specfiles are in the hash_buf, generate the hash. */
 void digest_gen_hash(struct selabel_digest *digest)
 {
-	Sha1Context context;
+	Sha256Context context;
 	size_t remaining_size;
 	const unsigned char *ptr;
-	const uint32_t chunkSize = UINT32_MAX >> 3;
 
 	/* If SELABEL_OPT_DIGEST not set then just return */
 	if (!digest)
 		return;
 
-	Sha1Initialise(&context);
+	Sha256Initialise(&context);
 
 	/* Process in blocks of chunkSize bytes */
 	remaining_size = digest->hashbuf_size;
 	ptr = digest->hashbuf;
-	while (remaining_size > chunkSize) {
-		Sha1Update(&context, ptr, chunkSize);
-		remaining_size -= chunkSize;
-		ptr += chunkSize;
+	while (remaining_size > UINT32_MAX) {
+		Sha256Update(&context, ptr, UINT32_MAX);
+		remaining_size -= UINT32_MAX;
+		ptr += UINT32_MAX;
 	}
-	Sha1Update(&context, ptr, remaining_size);
+	Sha256Update(&context, ptr, remaining_size);
 
-	Sha1Finalise(&context, (SHA1_HASH *)digest->digest);
+	Sha256Finalise(&context, (SHA256_HASH *)digest->digest);
 	free(digest->hashbuf);
 	digest->hashbuf = NULL;
 }
libselinux/src/libselinux.map +3 -2
index 9b439d8e7f22..b39e41d40aff 100644
@@ -237,8 +237,8 @@ LIBSELINUX_1.0 {
     sidput;
     string_to_av_perm;
     string_to_security_class;
-  local:
-    *;
+    local:
+      *;
 };
 
 LIBSELINUX_3.4 {
@@ -271,4 +271,5 @@ LIBSELINUX_3.10 {
 LIBSELINUX_3.12 {
   global:
     selinux_policy_open;
+    selabel_hash_all_partial_matches;
 } LIBSELINUX_3.10;
libselinux/src/selinux_restorecon.c +26 -16

127 lines not on this page · raw message

libselinux/src/sha256.c new file +274 -0

274 lines not on this page · raw message

libselinux/src/sha256.h new file +75 -0

75 lines not on this page · raw message

libselinux/utils/selabel_digest.c +13 -13
index 689ca7f47c5f..402f4d163fca 100644
@@ -13,8 +13,8 @@ static __attribute__((__noreturn__)) void usage(const char *progname)
 		"Where:\n\t"
 		"-b  The backend - \"file\", \"media\", \"x\", \"db\" or "
 		"\"prop\"\n\t"
-		"-v  Run \"cat <specfile_list> | openssl dgst -sha1 -hex\"\n\t"
-		"    on the list of specfiles to compare the SHA1 digests.\n\t"
+		"-v  Run \"cat <specfile_list> | openssl dgst -sha256 -hex\"\n\t"
+		"    on the list of specfiles to compare the SHA256 digests.\n\t"
 		"-B  Use base specfiles only (valid for \"-b file\" only).\n\t"
 		"-i  Do not request a digest.\n\t"
 		"-f  Optional file containing the specs (defaults to\n\t"
@@ -70,12 +70,12 @@ int main(int argc, char **argv)
 	int rc, opt, validate = 0;
 	char *baseonly = NULL, *file = NULL, *digest = (char *)1;
 	char **specfiles = NULL;
-	unsigned char *sha1_digest = NULL;
+	unsigned char *sha256_digest = NULL;
 	size_t digest_len, i, num_specfiles;
 
 	char cmd_buf[4096];
 	char *cmd_ptr;
-	char *sha1_buf = NULL;
+	char *sha256_buf = NULL;
 
 	struct selabel_handle *hnd;
 	struct selinux_opt selabel_option[] = { { SELABEL_OPT_PATH, file },
@@ -145,7 +145,7 @@ int main(int argc, char **argv)
 		return -1;
 	}
 
-	rc = selabel_digest(hnd, &sha1_digest, &digest_len, &specfiles,
+	rc = selabel_digest(hnd, &sha256_digest, &digest_len, &specfiles,
 			    &num_specfiles);
 
 	if (rc) {
@@ -160,19 +160,19 @@ int main(int argc, char **argv)
 		goto err;
 	}
 
-	sha1_buf = malloc(digest_len * 2 + 1);
-	if (!sha1_buf) {
+	sha256_buf = malloc(digest_len * 2 + 1);
+	if (!sha256_buf) {
 		fprintf(stderr, "Could not malloc buffer ERROR: %s\n",
 			strerror(errno));
 		rc = -1;
 		goto err;
 	}
 
-	printf("SHA1 digest: ");
+	printf("SHA256 digest: ");
 	for (i = 0; i < digest_len; i++)
-		sprintf(&(sha1_buf[i * 2]), "%02x", sha1_digest[i]);
+		sprintf(&(sha256_buf[i * 2]), "%02x", sha256_digest[i]);
 
-	printf("%s\n", sha1_buf);
+	printf("%s\n", sha256_buf);
 	printf("calculated using the following specfile(s):\n");
 
 	if (specfiles) {
@@ -211,7 +211,7 @@ int main(int argc, char **argv)
 
 		if (validate) {
 			ret = snprintf(cmd_ptr, cmd_rem,
-				       "| /usr/bin/openssl dgst -sha1 -hex");
+				       "| /usr/bin/openssl dgst -sha256 -hex");
 			if (ret < 0 || (size_t)ret >= cmd_rem) {
 				fprintf(stderr,
 					"Could not format validate command\n");
@@ -219,12 +219,12 @@ int main(int argc, char **argv)
 				goto err;
 			}
 
-			rc = run_check_digest(cmd_buf, sha1_buf, digest_len);
+			rc = run_check_digest(cmd_buf, sha256_buf, digest_len);
 		}
 	}
 
 err:
-	free(sha1_buf);
+	free(sha256_buf);
 	selabel_close(hnd);
 	return rc;
 }
libselinux/utils/selabel_get_digests_all_partial_matches.c +16 -14
index 0552ef0f58df..0fc193c858df 100644
@@ -16,8 +16,8 @@ static __attribute__((__noreturn__)) void usage(const char *progname)
 		"-v  Validate file_contxts entries against loaded policy.\n\t"
 		"-r  Recursively descend directories.\n\t"
 		"-f  Optional file_contexts file (defaults to current policy).\n\t"
-		"path  Path to check current SHA1 digest against file_contexts entries.\n\n"
-		"This will check the directory selinux.sehash SHA1 digest for "
+		"path  Path to check current SHA256 digest against file_contexts entries.\n\n"
+		"This will check the directory selinux.sehash SHA256 digest for "
 		"<path> against\na newly generated digest based on the "
 		"file_context entries for that node\n(using the regx, mode "
 		"and path entries).\n",
@@ -36,7 +36,7 @@ int main(int argc, char **argv)
 	char *paths[2] = { NULL, NULL };
 	uint8_t *xattr_digest = NULL;
 	uint8_t *calculated_digest = NULL;
-	char *sha1_buf = NULL;
+	char *sha256_buf = NULL;
 
 	struct selabel_handle *hnd;
 	struct selinux_opt selabel_option[] = {
@@ -101,8 +101,8 @@ int main(int argc, char **argv)
 				hnd, ftsent->fts_path, &calculated_digest,
 				&xattr_digest, &digest_len);
 
-			sha1_buf = calloc(1, digest_len * 2 + 1);
-			if (!sha1_buf) {
+			sha256_buf = calloc(1, digest_len * 2 + 1);
+			if (!sha256_buf) {
 				fprintf(stderr,
 					"Could not calloc buffer ERROR: %s\n",
 					strerror(errno));
@@ -110,19 +110,20 @@ int main(int argc, char **argv)
 			}
 
 			if (status) { /* They match */
-				printf("xattr and file_contexts SHA1 digests match for: %s\n",
+				printf("xattr and file_contexts SHA256 digests match for: %s\n",
 				       ftsent->fts_path);
 
 				if (calculated_digest) {
 					for (i = 0; i < digest_len; i++)
-						sprintf((&sha1_buf[i * 2]),
+						sprintf((&sha256_buf[i * 2]),
 							"%02x",
 							calculated_digest[i]);
-					printf("SHA1 digest: %s\n", sha1_buf);
+					printf("SHA256 digest: %s\n",
+					       sha256_buf);
 				}
 			} else {
 				if (!calculated_digest) {
-					printf("No SHA1 digest available for: %s\n",
+					printf("No SHA256 digest available for: %s\n",
 					       ftsent->fts_path);
 					printf("as file_context entry is \"<<none>>\"\n");
 					goto cleanup;
@@ -132,25 +133,26 @@ int main(int argc, char **argv)
 				       ftsent->fts_path);
 
 				for (i = 0; i < digest_len; i++)
-					sprintf((&sha1_buf[i * 2]), "%02x",
+					sprintf((&sha256_buf[i * 2]), "%02x",
 						calculated_digest[i]);
-				printf("generated SHA1 digest: %s\n", sha1_buf);
+				printf("generated SHA256 digest: %s\n",
+				       sha256_buf);
 
 				if (!xattr_digest) {
 					printf("however there is no selinux.sehash xattr entry.\n");
 				} else {
 					printf("however it does NOT match the current entry of:\n");
 					for (i = 0; i < digest_len; i++)
-						sprintf((&sha1_buf[i * 2]),
+						sprintf((&sha256_buf[i * 2]),
 							"%02x",
 							xattr_digest[i]);
-					printf("%s\n", sha1_buf);
+					printf("%s\n", sha256_buf);
 				}
 			}
 cleanup:
 			free(xattr_digest);
 			free(calculated_digest);
-			free(sha1_buf);
+			free(sha256_buf);
 			break;
 		}
 		default:
policycoreutils/setfiles/restorecon.8 +5 -5
index 3a8b8ee098c6..6b84604982e0 100644
@@ -111,14 +111,14 @@ display usage information and exit.
 ignore files that do not exist.
 .TP
 .B \-I
-ignore digest to force checking of labels even if the stored SHA1 digest
-matches the specfiles SHA1 digest. The digest will then be updated provided
+ignore digest to force checking of labels even if the stored SHA256 digest
+matches the specfiles SHA256 digest. The digest will then be updated provided
 there are no errors. See the
 .B NOTES
 section for further details.
 .TP
 .B \-D
-Set or update any directory SHA1 digests. Use this option to
+Set or update any directory SHA256 digests. Use this option to
 enable usage of the
 .IR security.sehash
 extended attribute.
@@ -237,7 +237,7 @@ the
 .B \-D
 option to
 .B restorecon
-will cause it to store a SHA1 digest of the default specfiles set in an extended
+will cause it to store a SHA256 digest of the default specfiles set in an extended
 attribute named
 .IR security.sehash
 on each directory specified in
@@ -254,7 +254,7 @@ for further details.
 .sp
 The
 .B \-I
-option will ignore the SHA1 digest from each directory specified in
+option will ignore the SHA256 digest from each directory specified in
 .IR pathname \ ...
 and provided the
 .B \-n
policycoreutils/setfiles/restorecon_xattr.8 +4 -4
index 51d12a4dbb80..09bfd8c40ab4 100644
@@ -23,7 +23,7 @@ or
 
 .SH "DESCRIPTION"
 .B restorecon_xattr
-will display the SHA1 digests added to extended attributes
+will display the SHA256 digests added to extended attributes
 .I security.sehash
 or delete the attribute completely. These attributes are set by
 .BR restorecon (8)
@@ -48,12 +48,12 @@ extended attribute and are automatically excluded from searches.
 .sp
 By default
 .B restorecon_xattr
-will display the SHA1 digests with "Match" appended if they match the default
+will display the SHA256 digests with "Match" appended if they match the default
 specfile set or the
 .I specfile
 set used with the
 .B \-f
-option. Non-matching SHA1 digests will be displayed with "No Match" appended.
+option. Non-matching SHA256 digests will be displayed with "No Match" appended.
 This feature can be disabled by the
 .B \-n
 option.
@@ -87,7 +87,7 @@ Do not append "Match" or "No Match" to displayed digests.
 recursively descend directories.
 .TP
 .B \-v
-display SHA1 digest generated by specfile set (Note that this digest is not
+display SHA256 digest generated by specfile set (Note that this digest is not
 used to match the
 .I security.sehash
 directory digest entries, and is shown for reference only).
policycoreutils/setfiles/restorecon_xattr.c +6 -6
index e8604ec63c29..738319edb4a4 100644
@@ -38,7 +38,7 @@ int main(int argc, char **argv)
 	unsigned int xattr_flags = 0, delete_digest = 0, recurse = 0;
 	unsigned int delete_all_digests = 0, ignore_mounts = 0;
 	bool display_digest = false;
-	char *sha1_buf, **specfiles, *fc_file = NULL, *pathname = NULL;
+	char *sha256_buf, **specfiles, *fc_file = NULL, *pathname = NULL;
 	unsigned char *fc_digest = NULL;
 	size_t i, fc_digest_len = 0, num_specfiles;
 
@@ -134,8 +134,8 @@ int main(int argc, char **argv)
 			exit(-1);
 		}
 
-		sha1_buf = malloc(fc_digest_len * 2 + 1);
-		if (!sha1_buf) {
+		sha256_buf = malloc(fc_digest_len * 2 + 1);
+		if (!sha256_buf) {
 			fprintf(stderr, "Error allocating digest buffer: %s\n",
 				strerror(errno));
 			selabel_close(hnd);
@@ -143,16 +143,16 @@ int main(int argc, char **argv)
 		}
 
 		for (i = 0; i < fc_digest_len; i++)
-			sprintf((&sha1_buf[i * 2]), "%02x", fc_digest[i]);
+			sprintf((&sha256_buf[i * 2]), "%02x", fc_digest[i]);
 
-		printf("specfiles SHA1 digest: %s\n", sha1_buf);
+		printf("specfiles SHA256 digest: %s\n", sha256_buf);
 
 		printf("calculated using the following specfile(s):\n");
 		if (specfiles) {
 			for (i = 0; i < num_specfiles; i++)
 				printf("%s\n", specfiles[i]);
 		}
-		free(sha1_buf);
+		free(sha256_buf);
 		printf("\n");
 	}
 
policycoreutils/setfiles/setfiles.8 +5 -5
index 88f4ecefcde7..fe7aa17f05c7 100644
@@ -106,14 +106,14 @@ display usage information and exit.
 ignore files that do not exist.
 .TP
 .B \-I
-ignore digest to force checking of labels even if the stored SHA1 digest
-matches the specfiles SHA1 digest. The digest will then be updated provided
+ignore digest to force checking of labels even if the stored SHA256 digest
+matches the specfiles SHA256 digest. The digest will then be updated provided
 there are no errors. See the
 .B NOTES
 section for further details.
 .TP
 .B \-D
-Set or update any directory SHA1 digests. Use this option to
+Set or update any directory SHA256 digests. Use this option to
 enable usage of the
 .IR security.sehash
 extended attribute.
@@ -298,7 +298,7 @@ the
 .B \-D
 option to
 .B setfiles
-will cause it to store a SHA1 digest of the
+will cause it to store a SHA256 digest of the
 .B spec_file
 set in an extended attribute named
 .IR security.sehash
@@ -319,7 +319,7 @@ for further details.
 .sp
 The
 .B \-I
-option will ignore the SHA1 digest from each directory specified in
+option will ignore the SHA256 digest from each directory specified in
 .IR pathname \ ...
 and provided the
 .B \-n
Signature · 1 line
2.55.0
maintainer
#2 lore raw

Re: [PATCH v2 2/2] libselinux: Use SHA256 for label digests

Quoted 5 lines · Petr Lautrbach

SHA-1 is considered obsolete

Use hashing code from libsemanage/src/sha256.*

Quoted 8 lines

Provide versioned selabel_hash_all_partial_matches() symbol
- selabel_hash_all_partial_matches@LIBSELINUX_1.0 generates original SHA1 digest and uses 20 bytes (SHA1_HASH_SIZE)
- selabel_hash_all_partial_matches@@LIBSELINUX_3.12 generates SHA256 digest and uses 32 bytes (SHA256_HASH_SIZE)

All external selabel_hash_all_partial_matches() users has to make sure that they provide "uint8_t *digest" with size at least 32 bytes.

To be clear, you mean all external users of LIBSELINUX_3.12 and later versions have to do this.

I don't think the symbol versioning helps Android because its user is within libselinux itself and will always be built against the latest version, but the in-libselinux caller is still expecting SHA1 digests. That's why we need #ifdef ANDROID within hash_all_partial_matches() to fall back to using SHA1 for Android builds IIUC.

Otherwise, a couple of nits below.

Quoted 12 lines
libselinux/src/label_support.c +1 -1
index 5112c59cdd5c..a229373af09c 100644
@@ -214,28 +214,27 @@ int read_spec_entries(char *line_buf, size_t nread, const char **errbuf,
 /* Once all the specfiles are in the hash_buf, generate the hash. */
 void digest_gen_hash(struct selabel_digest *digest)
 {
-       Sha1Context context;
+       Sha256Context context;
        size_t remaining_size;
        const unsigned char *ptr;

libselinux/src/label_support.c int read_spec_entries(char *line_buf, size_t nread, const char **errbuf, · line 227

Quoted 10 lines
libselinux/src/label_support.c +1 -2
-       const uint32_t chunkSize = UINT32_MAX >> 3;
 
        /* If SELABEL_OPT_DIGEST not set then just return */
        if (!digest)
                return;
 
-       Sha1Initialise(&context);
+       Sha256Initialise(&context);
 
        /* Process in blocks of chunkSize bytes */

This comment is no longer correct and can be dropped.

libselinux/src/label_support.c int read_spec_entries(char *line_buf, size_t nread, const char **errbuf, · line 231

Quoted 8 lines
libselinux/src/label_support.c +2 -4
        remaining_size = digest->hashbuf_size;
        ptr = digest->hashbuf;
-       while (remaining_size > chunkSize) {
-               Sha1Update(&context, ptr, chunkSize);
-               remaining_size -= chunkSize;
-               ptr += chunkSize;
+       while (remaining_size > UINT32_MAX) {
+               Sha256Update(&context, ptr, UINT32_MAX);

Sha256Update() takes size_t and tracks length as uint64_t so the

Quoted 1 line

UINT32_MAX split that existed for Sha1 is no longer necessary. You

could collapse the entire loop and the subsequent Sha256Update() below to a single Sha256Update(,,,,, digest->hashbuf_size) call.

libselinux/src/libselinux.map LIBSELINUX_1.0 { · line 241

Quoted 12 lines
libselinux/src/libselinux.map +2 -2
index 9b439d8e7f22..b39e41d40aff 100644
@@ -237,8 +237,8 @@ LIBSELINUX_1.0 {
     sidput;
     string_to_av_perm;
     string_to_security_class;
-  local:
-    *;
+    local:
+      *;

This seems like a spurious whitespace change.

maintainer
#3 lore raw

Re: [PATCH v2 2/2] libselinux: Use SHA256 for label digests

Quoted 16 lines · Stephen Smalley
Quoted 5 lines · Petr Lautrbach

SHA-1 is considered obsolete

Use hashing code from libsemanage/src/sha256.*

Quoted 8 lines

Provide versioned selabel_hash_all_partial_matches() symbol
- selabel_hash_all_partial_matches@LIBSELINUX_1.0 generates original SHA1 digest and uses 20 bytes (SHA1_HASH_SIZE)
- selabel_hash_all_partial_matches@@LIBSELINUX_3.12 generates SHA256 digest and uses 32 bytes (SHA256_HASH_SIZE)

All external selabel_hash_all_partial_matches() users has to make sure that they provide "uint8_t *digest" with size at least 32 bytes.

Quoted 8 lines

To be clear, you mean all external users of LIBSELINUX_3.12 and later versions have to do this.

I don't think the symbol versioning helps Android because its user is within libselinux itself and will always be built against the latest version, but the in-libselinux caller is still expecting SHA1 digests. That's why we need #ifdef ANDROID within hash_all_partial_matches() to fall back to using SHA1 for Android builds IIUC.

Actually, what we likely want is in label.c, add:
#if defined(ANDROID) || defined(__APPLE__)
bool selabel_hash_all_partial_matches(struct selabel_handle *rec,
const char *key, uint8_t *digest)

{
        if (!rec->func_hash_all_partial_matches_sha1)
                return false;
        return rec->func_hash_all_partial_matches_sha1(rec, key, digest);
}

#else
... the two .symver'd functions as in v2 ...
#endif

The reason for including __APPLE__ above is because .symver is an ELF directive and label_internal.h already has a similar construct.