@ -1,4 +1,4 @@
From 1dbd23dc2566b3fe9113bf09fd9e190dfd4651b6 Mon Sep 17 00:00:00 2001
From 4884c917237e53e34d3fc75dcf4f07217cfd7584 Mon Sep 17 00:00:00 2001
From: Petr Lautrbach <plautrba@redhat.com>
From: Petr Lautrbach <plautrba@redhat.com>
Date: Fri, 30 Jul 2021 14:14:37 +0200
Date: Fri, 30 Jul 2021 14:14:37 +0200
Subject: [PATCH] Use SHA-2 instead of SHA-1
Subject: [PATCH] Use SHA-2 instead of SHA-1
@ -17,20 +17,20 @@ The use of SHA-1 in RHEL9 is deprecated
libselinux/src/label_internal.h | 10 +-
libselinux/src/label_internal.h | 10 +-
libselinux/src/label_support.c | 10 +-
libselinux/src/label_support.c | 10 +-
libselinux/src/selinux_restorecon.c | 24 +-
libselinux/src/selinux_restorecon.c | 24 +-
libselinux/src/sha1.c | 220 -------------
libselinux/src/sha1.c | 223 -------------
libselinux/src/sha1.h | 85 -----
libselinux/src/sha1.h | 85 -----
libselinux/src/sha256.c | 294 ++++++++++++++++++
libselinux/src/sha256.c | 294 ++++++++++++++++++
libselinux/src/sha256.h | 89 ++++++
libselinux/src/sha256.h | 89 ++++++
libselinux/utils/selabel_digest.c | 26 +-
libselinux/utils/selabel_digest.c | 26 +-
.../selabel_get_digests_all_partial_matches.c | 28 +-
.../selabel_get_digests_all_partial_matches.c | 28 +-
17 files changed, 471 insertions(+), 393 deletions(-)
17 files changed, 471 insertions(+), 396 deletions(-)
delete mode 100644 libselinux/src/sha1.c
delete mode 100644 libselinux/src/sha1.c
delete mode 100644 libselinux/src/sha1.h
delete mode 100644 libselinux/src/sha1.h
create mode 100644 libselinux/src/sha256.c
create mode 100644 libselinux/src/sha256.c
create mode 100644 libselinux/src/sha256.h
create mode 100644 libselinux/src/sha256.h
diff --git a/libselinux/include/selinux/label.h b/libselinux/include/selinux/label.h
diff --git a/libselinux/include/selinux/label.h b/libselinux/include/selinux/label.h
index e8983606d93b..a35d84d63b0a 100644
index ce189a3ae2fe..ce77d32dfed1 100644
--- a/libselinux/include/selinux/label.h
--- a/libselinux/include/selinux/label.h
+++ b/libselinux/include/selinux/label.h
+++ b/libselinux/include/selinux/label.h
@@ -120,13 +120,13 @@ extern int selabel_lookup_best_match_raw(struct selabel_handle *rec, char **con,
@@ -120,13 +120,13 @@ extern int selabel_lookup_best_match_raw(struct selabel_handle *rec, char **con,
@ -185,23 +185,23 @@ index c56326814b94..098c840fc59b 100644
.BR selabel_open (3)
.BR selabel_open (3)
must be called specifying the required
must be called specifying the required
diff --git a/libselinux/src/Makefile b/libselinux/src/Makefile
diff --git a/libselinux/src/Makefile b/libselinux/src/Makefile
index 70ba063ada5d..0c803d8d4aae 100644
index 41cfbdca490c..658a4c3d80e0 100644
--- a/libselinux/src/Makefile
--- a/libselinux/src/Makefile
+++ b/libselinux/src/Makefile
+++ b/libselinux/src/Makefile
@@ -1 25,7 +125 ,7 @@ DISABLE_FLAGS+= -DNO_MEDIA_BACKEND -DNO_DB_BACKEND -DNO_X_BACKEND \
@@ -1 30,7 +130 ,7 @@ DISABLE_FLAGS+= -DNO_MEDIA_BACKEND -DNO_DB_BACKEND -DNO_X_BACKEND \
-DBUILD_HOST
-DBUILD_HOST
SRCS= callbacks.c freecon.c label.c label_file.c \
SRCS= callbacks.c freecon.c label.c label_file.c \
label_backends_android.c regex.c label_support.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 booleans.c
+ matchpathcon.c setrans_client.c sha256.c booleans.c
+ matchpathcon.c setrans_client.c sha256.c booleans.c
else
LABEL_BACKEND_ANDROID=y
LABEL_BACKEND_ANDROID=y
endif
endif
diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c
diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c
index 74ae9b9feb70..33d395e414f0 100644
index 2732972e61cf..6c6fe328b353 100644
--- a/libselinux/src/label_file.c
--- a/libselinux/src/label_file.c
+++ b/libselinux/src/label_file.c
+++ b/libselinux/src/label_file.c
@@ -1 0 10,7 +10 10,7 @@ static struct spec *lookup_common(struct selabel_handle *rec,
@@ -1 105 ,7 +1105 ,7 @@ static struct spec *lookup_common(struct selabel_handle *rec,
/*
/*
* Returns true if the digest of all partial matched contexts is the same as
* Returns true if the digest of all partial matched contexts is the same as
@ -210,7 +210,7 @@ index 74ae9b9feb70..33d395e414f0 100644
* digest will always be returned. The caller must free any returned digests.
* digest will always be returned. The caller must free any returned digests.
*/
*/
static bool get_digests_all_partial_matches(struct selabel_handle *rec,
static bool get_digests_all_partial_matches(struct selabel_handle *rec,
@@ -1 019,39 +1019 ,39 @@ static bool get_digests_all_partial_matches(struct selabel_handle *rec,
@@ -1 114,39 +1114 ,39 @@ static bool get_digests_all_partial_matches(struct selabel_handle *rec,
uint8_t **xattr_digest,
uint8_t **xattr_digest,
size_t *digest_len)
size_t *digest_len)
{
{
@ -261,7 +261,7 @@ index 74ae9b9feb70..33d395e414f0 100644
return true;
return true;
return false;
return false;
@@ -1 07 1,22 +107 1,22 @@ static bool hash_all_partial_matches(struct selabel_handle *rec, const char *key
@@ -1 166 ,22 +1166 ,22 @@ static bool hash_all_partial_matches(struct selabel_handle *rec, const char *key
return false;
return false;
}
}
@ -293,7 +293,7 @@ index 74ae9b9feb70..33d395e414f0 100644
free(matches);
free(matches);
return true;
return true;
diff --git a/libselinux/src/label_internal.h b/libselinux/src/label_internal.h
diff --git a/libselinux/src/label_internal.h b/libselinux/src/label_internal.h
index 782c6aa8cc0c..304e8d96490a 100644
index ea60cd9a058f..77ac8173c7a9 100644
--- a/libselinux/src/label_internal.h
--- a/libselinux/src/label_internal.h
+++ b/libselinux/src/label_internal.h
+++ b/libselinux/src/label_internal.h
@@ -13,7 +13,7 @@
@@ -13,7 +13,7 @@
@ -334,10 +334,10 @@ index 782c6aa8cc0c..304e8d96490a 100644
};
};
diff --git a/libselinux/src/label_support.c b/libselinux/src/label_support.c
diff --git a/libselinux/src/label_support.c b/libselinux/src/label_support.c
index 54fd49a5b7b9..4003eb8dc7af 100644
index f7ab9292562e..1c3c1728f6ba 100644
--- a/libselinux/src/label_support.c
--- a/libselinux/src/label_support.c
+++ b/libselinux/src/label_support.c
+++ b/libselinux/src/label_support.c
@@ -11 5,7 +115 ,7 @@ int read_spec_entries(char *line_buf, const char **errbuf, int num_args, ...)
@@ -11 4,7 +114 ,7 @@ int read_spec_entries(char *line_buf, const char **errbuf, int num_args, ...)
/* Once all the specfiles are in the hash_buf, generate the hash. */
/* Once all the specfiles are in the hash_buf, generate the hash. */
void digest_gen_hash(struct selabel_digest *digest)
void digest_gen_hash(struct selabel_digest *digest)
{
{
@ -346,7 +346,7 @@ index 54fd49a5b7b9..4003eb8dc7af 100644
size_t remaining_size;
size_t remaining_size;
const unsigned char *ptr;
const unsigned char *ptr;
@@ -12 3,19 +123 ,19 @@ void digest_gen_hash(struct selabel_digest *digest)
@@ -12 2,19 +122 ,19 @@ void digest_gen_hash(struct selabel_digest *digest)
if (!digest)
if (!digest)
return;
return;
@ -369,9 +369,9 @@ index 54fd49a5b7b9..4003eb8dc7af 100644
+ Sha256Finalise(&context, (SHA256_HASH *)digest->digest);
+ Sha256Finalise(&context, (SHA256_HASH *)digest->digest);
free(digest->hashbuf);
free(digest->hashbuf);
digest->hashbuf = NULL;
digest->hashbuf = NULL;
return;
}
diff --git a/libselinux/src/selinux_restorecon.c b/libselinux/src/selinux_restorecon.c
diff --git a/libselinux/src/selinux_restorecon.c b/libselinux/src/selinux_restorecon.c
index 6b5f6921b82b..24604776974 e 100644
index acb729c8ad96..2422b415008 e 100644
--- a/libselinux/src/selinux_restorecon.c
--- a/libselinux/src/selinux_restorecon.c
+++ b/libselinux/src/selinux_restorecon.c
+++ b/libselinux/src/selinux_restorecon.c
@@ -37,7 +37,7 @@
@@ -37,7 +37,7 @@
@ -383,7 +383,7 @@ index 6b5f6921b82b..24604776974e 100644
#define STAR_COUNT 1024
#define STAR_COUNT 1024
@@ -30 5,7 +305 ,7 @@ static uint64_t exclude_non_seclabel_mounts(void)
@@ -30 6,7 +306 ,7 @@ static uint64_t exclude_non_seclabel_mounts(void)
static int add_xattr_entry(const char *directory, bool delete_nonmatch,
static int add_xattr_entry(const char *directory, bool delete_nonmatch,
bool delete_all)
bool delete_all)
{
{
@ -392,7 +392,7 @@ index 6b5f6921b82b..24604776974e 100644
size_t i, digest_len = 0;
size_t i, digest_len = 0;
int rc;
int rc;
enum digest_result digest_result;
enum digest_result digest_result;
@@ -3 29,15 +329 ,15 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch,
@@ -3 30,15 +330 ,15 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch,
}
}
/* Convert entry to a hex encoded string. */
/* Convert entry to a hex encoded string. */
@ -411,7 +411,7 @@ index 6b5f6921b82b..24604776974e 100644
digest_result = match ? MATCH : NOMATCH;
digest_result = match ? MATCH : NOMATCH;
@@ -35 7,7 +357 ,7 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch,
@@ -35 8,7 +358 ,7 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch,
/* Now add entries to link list. */
/* Now add entries to link list. */
new_entry = malloc(sizeof(struct dir_xattr));
new_entry = malloc(sizeof(struct dir_xattr));
if (!new_entry) {
if (!new_entry) {
@ -420,7 +420,7 @@ index 6b5f6921b82b..24604776974e 100644
goto oom;
goto oom;
}
}
new_entry->next = NULL;
new_entry->next = NULL;
@@ -36 5,15 +365 ,15 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch,
@@ -36 6,15 +366 ,15 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch,
new_entry->directory = strdup(directory);
new_entry->directory = strdup(directory);
if (!new_entry->directory) {
if (!new_entry->directory) {
free(new_entry);
free(new_entry);
@ -439,7 +439,7 @@ index 6b5f6921b82b..24604776974e 100644
goto oom;
goto oom;
}
}
@@ -38 7,7 +387 ,7 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch,
@@ -38 8,7 +388 ,7 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch,
dir_xattr_last = new_entry;
dir_xattr_last = new_entry;
}
}
@ -448,7 +448,7 @@ index 6b5f6921b82b..24604776974e 100644
return 0;
return 0;
oom:
oom:
@@ -77 5,7 +775 ,7 @@ err:
@@ -77 8,7 +778 ,7 @@ err:
struct dir_hash_node {
struct dir_hash_node {
char *path;
char *path;
@ -457,7 +457,7 @@ index 6b5f6921b82b..24604776974e 100644
struct dir_hash_node *next;
struct dir_hash_node *next;
};
};
/*
/*
@@ -128 1,7 +1281 ,7 @@ static int selinux_restorecon_common(const char *pathname_orig,
@@ -128 4,7 +1284 ,7 @@ static int selinux_restorecon_common(const char *pathname_orig,
if (setxattr(current->path,
if (setxattr(current->path,
RESTORECON_PARTIAL_MATCH_DIGEST,
RESTORECON_PARTIAL_MATCH_DIGEST,
current->digest,
current->digest,
@ -468,10 +468,10 @@ index 6b5f6921b82b..24604776974e 100644
current->path);
current->path);
diff --git a/libselinux/src/sha1.c b/libselinux/src/sha1.c
diff --git a/libselinux/src/sha1.c b/libselinux/src/sha1.c
deleted file mode 100644
deleted file mode 100644
index 9d51e04ac331 ..000000000000
index 452b0cc2ad5a ..000000000000
--- a/libselinux/src/sha1.c
--- a/libselinux/src/sha1.c
+++ /dev/null
+++ /dev/null
@@ -1,22 0 +0,0 @@
@@ -1,22 3 +0,0 @@
-///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-// LibSha1
-// LibSha1
-//
-//
@ -500,6 +500,8 @@ index 9d51e04ac331..000000000000
-#include "sha1.h"
-#include "sha1.h"
-#include <memory.h>
-#include <memory.h>
-
-
-#include "selinux_internal.h"
-
-///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-// TYPES
-// TYPES
-///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -536,6 +538,7 @@ index 9d51e04ac331..000000000000
-//
-//
-// Hash a single 512-bit block. This is the core of the algorithm
-// Hash a single 512-bit block. This is the core of the algorithm
-///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-ignore_unsigned_overflow_
-static
-static
-void
-void
- TransformFunction
- TransformFunction
@ -1179,10 +1182,10 @@ index 000000000000..406ed869cd82
+ SHA256_HASH* Digest // [in]
+ SHA256_HASH* Digest // [in]
+ );
+ );
diff --git a/libselinux/utils/selabel_digest.c b/libselinux/utils/selabel_digest.c
diff --git a/libselinux/utils/selabel_digest.c b/libselinux/utils/selabel_digest.c
index 6a8313a2c88d..a69331f1c6b5 100644
index 47aad21ff1fb..8bcd44a1dc73 100644
--- a/libselinux/utils/selabel_digest.c
--- a/libselinux/utils/selabel_digest.c
+++ b/libselinux/utils/selabel_digest.c
+++ b/libselinux/utils/selabel_digest.c
@@ -1 5,8 +15 ,8 @@ static __attribute__ ((__noreturn__)) void usage(const char *progname)
@@ -1 3,8 +13 ,8 @@ static __attribute__ ((__noreturn__)) void usage(const char *progname)
"Where:\n\t"
"Where:\n\t"
"-b The backend - \"file\", \"media\", \"x\", \"db\" or "
"-b The backend - \"file\", \"media\", \"x\", \"db\" or "
"\"prop\"\n\t"
"\"prop\"\n\t"
@ -1193,22 +1196,22 @@ index 6a8313a2c88d..a69331f1c6b5 100644
"-B Use base specfiles only (valid for \"-b file\" only).\n\t"
"-B Use base specfiles only (valid for \"-b file\" only).\n\t"
"-i Do not request a digest.\n\t"
"-i Do not request a digest.\n\t"
"-f Optional file containing the specs (defaults to\n\t"
"-f Optional file containing the specs (defaults to\n\t"
@@ -6 2,12 +62 ,12 @@ int main(int argc, char **argv)
@@ -6 1,12 +61 ,12 @@ int main(int argc, char **argv)
int backend = 0, rc, opt, validate = 0;
int rc, opt, validate = 0;
char *baseonly = NULL, *file = NULL, *digest = (char *)1;
char *baseonly = NULL, *file = NULL, *digest = (char *)1;
char **specfiles = NULL;
char **specfiles = NULL;
- unsigned char *sha1_digest = NULL;
- unsigned char *sha1_digest = NULL;
+ unsigned char *sha256_digest = NULL;
+ unsigned char *sha256_digest = NULL;
size_t i, num_specfiles;
size_t digest_len, i, num_specfiles;
char cmd_buf[4096];
char cmd_buf[4096];
char *cmd_ptr;
char *cmd_ptr;
- char *sha1_buf ;
- char *sha1_buf = NULL ;
+ char *sha256_buf ;
+ char *sha256_buf = NULL ;
struct selabel_handle *hnd;
struct selabel_handle *hnd;
struct selinux_opt selabel_option[] = {
struct selinux_opt selabel_option[] = {
@@ -13 7,7 +137 ,7 @@ int main(int argc, char **argv)
@@ -13 6,7 +136 ,7 @@ int main(int argc, char **argv)
return -1;
return -1;
}
}
@ -1217,7 +1220,7 @@ index 6a8313a2c88d..a69331f1c6b5 100644
&num_specfiles);
&num_specfiles);
if (rc) {
if (rc) {
@@ -15 2,19 +152 ,19 @@ int main(int argc, char **argv)
@@ -15 1,19 +151 ,19 @@ int main(int argc, char **argv)
goto err;
goto err;
}
}
@ -1242,28 +1245,34 @@ index 6a8313a2c88d..a69331f1c6b5 100644
printf("calculated using the following specfile(s):\n");
printf("calculated using the following specfile(s):\n");
if (specfiles) {
if (specfiles) {
@@ -177,13 +177,13 @@ int main(int argc, char **argv)
@@ -198,19 +198,19 @@ int main(int argc, char **argv)
cmd_ptr += strlen(specfiles[i]) + 1;
printf("%s\n", specfiles[i]);
}
}
- sprintf(cmd_ptr, "| /usr/bin/openssl dgst -sha1 -hex");
+ sprintf(cmd_ptr, "| /usr/bin/openssl dgst -sha256 -hex");
if (validate)
if (validate) {
- rc = run_check_digest(cmd_buf, sha1_buf);
- ret = snprintf(cmd_ptr, cmd_rem, "| /usr/bin/openssl dgst -sha1 -hex");
+ rc = run_check_digest(cmd_buf, sha256_buf);
+ ret = snprintf(cmd_ptr, cmd_rem, "| /usr/bin/openssl dgst -sha256 -hex");
if (ret < 0 || (size_t)ret >= cmd_rem) {
fprintf(stderr, "Could not format validate command\n");
rc = -1;
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(sha1_buf);
+ free(sha256_buf);
+ free(sha256_buf);
err:
selabel_close(hnd);
selabel_close(hnd);
return rc;
return rc;
}
diff --git a/libselinux/utils/selabel_get_digests_all_partial_matches.c b/libselinux/utils/selabel_get_digests_all_partial_matches.c
diff --git a/libselinux/utils/selabel_get_digests_all_partial_matches.c b/libselinux/utils/selabel_get_digests_all_partial_matches.c
index c4e0f836b260..80723f714264 100644
index e2733b4195ff..98e533dc2692 100644
--- a/libselinux/utils/selabel_get_digests_all_partial_matches.c
--- a/libselinux/utils/selabel_get_digests_all_partial_matches.c
+++ b/libselinux/utils/selabel_get_digests_all_partial_matches.c
+++ b/libselinux/utils/selabel_get_digests_all_partial_matches.c
@@ -1 8,8 +18 ,8 @@ static __attribute__ ((__noreturn__)) void usage(const char *progname)
@@ -1 6,8 +16 ,8 @@ static __attribute__ ((__noreturn__)) void usage(const char *progname)
"-v Validate file_contxts entries against loaded policy.\n\t"
"-v Validate file_contxts entries against loaded policy.\n\t"
"-r Recursively descend directories.\n\t"
"-r Recursively descend directories.\n\t"
"-f Optional file_contexts file (defaults to current policy).\n\t"
"-f Optional file_contexts file (defaults to current policy).\n\t"
@ -1274,7 +1283,7 @@ index c4e0f836b260..80723f714264 100644
"<path> against\na newly generated digest based on the "
"<path> against\na newly generated digest based on the "
"file_context entries for that node\n(using the regx, mode "
"file_context entries for that node\n(using the regx, mode "
"and path entries).\n", progname);
"and path entries).\n", progname);
@@ -3 7,7 +37 ,7 @@ int main(int argc, char **argv)
@@ -3 5,7 +35 ,7 @@ int main(int argc, char **argv)
char *paths[2] = { NULL, NULL };
char *paths[2] = { NULL, NULL };
uint8_t *xattr_digest = NULL;
uint8_t *xattr_digest = NULL;
uint8_t *calculated_digest = NULL;
uint8_t *calculated_digest = NULL;
@ -1283,7 +1292,7 @@ index c4e0f836b260..80723f714264 100644
struct selabel_handle *hnd;
struct selabel_handle *hnd;
struct selinux_opt selabel_option[] = {
struct selinux_opt selabel_option[] = {
@@ -10 6,27 +106 ,27 @@ int main(int argc, char **argv)
@@ -10 4,27 +104 ,27 @@ int main(int argc, char **argv)
&xattr_digest,
&xattr_digest,
&digest_len);
&digest_len);
@ -1317,7 +1326,7 @@ index c4e0f836b260..80723f714264 100644
ftsent->fts_path);
ftsent->fts_path);
printf("as file_context entry is \"<<none>>\"\n");
printf("as file_context entry is \"<<none>>\"\n");
goto cleanup;
goto cleanup;
@@ -13 6,25 +136 ,25 @@ int main(int argc, char **argv)
@@ -13 4,25 +134 ,25 @@ int main(int argc, char **argv)
ftsent->fts_path);
ftsent->fts_path);
for (i = 0; i < digest_len; i++)
for (i = 0; i < digest_len; i++)
@ -1349,5 +1358,5 @@ index c4e0f836b260..80723f714264 100644
}
}
default:
default:
--
--
2.39.0
2.45.2