Skip to content

Commit 9d9602d

Browse files
committed
Use SHA-2 instead of SHA-1
The use of SHA-1 in RHEL9 is deprecated
1 parent 9f79f57 commit 9d9602d

21 files changed

+491
-413
lines changed

libselinux/include/selinux/label.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@ extern int selabel_lookup_best_match_raw(struct selabel_handle *rec, char **con,
120120
const char *key, const char **aliases, int type);
121121

122122
/**
123-
* selabel_digest - Retrieve the SHA1 digest and the list of specfiles used to
123+
* selabel_digest - Retrieve the SHA256 digest and the list of specfiles used to
124124
* generate the digest. The SELABEL_OPT_DIGEST option must
125125
* be set in selabel_open() to initiate the digest generation.
126126
* @handle: specifies backend instance to query
127-
* @digest: returns a pointer to the SHA1 digest.
127+
* @digest: returns a pointer to the SHA256 digest.
128128
* @digest_len: returns length of digest in bytes.
129-
* @specfiles: a list of specfiles used in the SHA1 digest generation.
129+
* @specfiles: a list of specfiles used in the SHA256 digest generation.
130130
* The list is NULL terminated and will hold @num_specfiles entries.
131131
* @num_specfiles: number of specfiles in the list.
132132
*

libselinux/include/selinux/restorecon.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ extern int selinux_restorecon_parallel(const char *pathname,
4141
* restorecon_flags options
4242
*/
4343
/*
44-
* Force the checking of labels even if the stored SHA1 digest
45-
* matches the specfiles SHA1 digest (requires CAP_SYS_ADMIN).
44+
* Force the checking of labels even if the stored SHA256 digest
45+
* matches the specfiles SHA256 digest (requires CAP_SYS_ADMIN).
4646
*/
4747
#define SELINUX_RESTORECON_IGNORE_DIGEST 0x00001
4848
/*

libselinux/man/man3/selabel_digest.3

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ selabel_digest \- Return digest of specfiles and list of files used
2020
.BR selabel_digest ()
2121
performs an operation on the handle
2222
.IR hnd ,
23-
returning the results of the SHA1 digest pointed to by
23+
returning the results of the SHA256 digest pointed to by
2424
.IR digest ,
2525
whose length will be
2626
.IR digest_len .
27-
The list of specfiles used in the SHA1 digest calculation is returned in
27+
The list of specfiles used in the SHA256 digest calculation is returned in
2828
.I specfiles
2929
with the number of entries in
3030
.IR num_specfiles .

libselinux/man/man3/selabel_open.3

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ is used; a custom validation function can be provided via
6969
Note that an invalid context may not be treated as an error unless it is actually encountered during a lookup operation.
7070
.TP
7171
.B SELABEL_OPT_DIGEST
72-
A non-null value for this option enables the generation of an SHA1 digest of
72+
A non-null value for this option enables the generation of an SHA256 digest of
7373
the spec files loaded as described in
7474
.BR selabel_digest (3)
7575
.

libselinux/man/man3/selinux_restorecon.3

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ If this is a directory and the
3636
.B SELINUX_RESTORECON_RECURSE
3737
has been set (for descending through directories), then
3838
.BR selinux_restorecon ()
39-
will write an SHA1 digest of specfile entries calculated by
39+
will write an SHA256 digest of specfile entries calculated by
4040
.BR selabel_get_digests_all_partial_matches (3)
4141
to an extended attribute of
4242
.IR security.sehash
@@ -55,7 +55,7 @@ will take place.
5555
.br
5656
The
5757
.IR restorecon_flags
58-
that can be used to manage the usage of the SHA1 digest are:
58+
that can be used to manage the usage of the SHA256 digest are:
5959
.RS
6060
.B SELINUX_RESTORECON_SKIP_DIGEST
6161
.br
@@ -73,8 +73,8 @@ Do not check or update any extended attribute
7373
entries.
7474
.sp
7575
.B SELINUX_RESTORECON_IGNORE_DIGEST
76-
force the checking of labels even if the stored SHA1 digest matches the
77-
specfile entries SHA1 digest. The specfile entries digest will be written to the
76+
force the checking of labels even if the stored SHA256 digest matches the
77+
specfile entries SHA256 digest. The specfile entries digest will be written to the
7878
.IR security.sehash
7979
extended attribute once relabeling has been completed successfully provided the
8080
.B SELINUX_RESTORECON_NOCHANGE
@@ -95,7 +95,7 @@ default specfile context.
9595
.sp
9696
.B SELINUX_RESTORECON_RECURSE
9797
change file and directory labels recursively (descend directories)
98-
and if successful write an SHA1 digest of the specfile entries to an
98+
and if successful write an SHA256 digest of the specfile entries to an
9999
extended attribute as described in the
100100
.B NOTES
101101
section.
@@ -179,12 +179,12 @@ for fetching the ignored (skipped) error count after
179179
or
180180
.BR selinux_restorecon_parallel (3)
181181
completes with success. In case any errors were skipped during the file tree
182-
walk, the specfile entries SHA1 digest will not have been written to the
182+
walk, the specfile entries SHA256 digest will not have been written to the
183183
.IR security.sehash
184184
extended attribute.
185185
.RE
186186
.sp
187-
The behavior regarding the checking and updating of the SHA1 digest described
187+
The behavior regarding the checking and updating of the SHA256 digest described
188188
above is the default behavior. It is possible to change this by first calling
189189
.BR selabel_open (3)
190190
and not enabling the
@@ -247,7 +247,7 @@ To improve performance when relabeling file systems recursively (e.g. the
247247
.B SELINUX_RESTORECON_RECURSE
248248
flag is set)
249249
.BR selinux_restorecon ()
250-
will write a calculated SHA1 digest of the specfile entries returned by
250+
will write a calculated SHA256 digest of the specfile entries returned by
251251
.BR selabel_get_digests_all_partial_matches (3)
252252
to an extended attribute named
253253
.IR security.sehash
@@ -269,7 +269,7 @@ Should any of the specfile entries have changed, then when
269269
.BR selinux_restorecon ()
270270
is run again with the
271271
.B SELINUX_RESTORECON_RECURSE
272-
flag set, new SHA1 digests will be calculated and all files automatically
272+
flag set, new SHA256 digests will be calculated and all files automatically
273273
relabeled depending on the settings of the
274274
.B SELINUX_RESTORECON_SET_SPECFILE_CTX
275275
flag (provided

libselinux/man/man3/selinux_restorecon_xattr.3

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ By default
119119
.BR selinux_restorecon_xattr (3)
120120
will use the default set of specfiles described in
121121
.BR files_contexts (5)
122-
to calculate the SHA1 digests to be used for comparison.
122+
to calculate the SHA256 digests to be used for comparison.
123123
To change this default behavior
124124
.BR selabel_open (3)
125125
must be called specifying the required

libselinux/src/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ DISABLE_FLAGS+= -DNO_MEDIA_BACKEND -DNO_DB_BACKEND -DNO_X_BACKEND \
130130
-DBUILD_HOST
131131
SRCS= callbacks.c freecon.c label.c label_file.c \
132132
label_backends_android.c regex.c label_support.c \
133-
matchpathcon.c setrans_client.c sha1.c booleans.c
133+
matchpathcon.c setrans_client.c sha256.c booleans.c
134134
LABEL_BACKEND_ANDROID=y
135135
endif
136136

libselinux/src/label_file.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ static struct spec *lookup_common(struct selabel_handle *rec,
10931093

10941094
/*
10951095
* Returns true if the digest of all partial matched contexts is the same as
1096-
* the one saved by setxattr, otherwise returns false. The length of the SHA1
1096+
* the one saved by setxattr, otherwise returns false. The length of the SHA256
10971097
* digest will always be returned. The caller must free any returned digests.
10981098
*/
10991099
static bool get_digests_all_partial_matches(struct selabel_handle *rec,
@@ -1102,39 +1102,39 @@ static bool get_digests_all_partial_matches(struct selabel_handle *rec,
11021102
uint8_t **xattr_digest,
11031103
size_t *digest_len)
11041104
{
1105-
uint8_t read_digest[SHA1_HASH_SIZE];
1105+
uint8_t read_digest[SHA256_HASH_SIZE];
11061106
ssize_t read_size = getxattr(pathname, RESTORECON_PARTIAL_MATCH_DIGEST,
1107-
read_digest, SHA1_HASH_SIZE
1107+
read_digest, SHA256_HASH_SIZE
11081108
#ifdef __APPLE__
11091109
, 0, 0
11101110
#endif /* __APPLE __ */
11111111
);
1112-
uint8_t hash_digest[SHA1_HASH_SIZE];
1112+
uint8_t hash_digest[SHA256_HASH_SIZE];
11131113
bool status = selabel_hash_all_partial_matches(rec, pathname,
11141114
hash_digest);
11151115

11161116
*xattr_digest = NULL;
11171117
*calculated_digest = NULL;
1118-
*digest_len = SHA1_HASH_SIZE;
1118+
*digest_len = SHA256_HASH_SIZE;
11191119

1120-
if (read_size == SHA1_HASH_SIZE) {
1121-
*xattr_digest = calloc(1, SHA1_HASH_SIZE + 1);
1120+
if (read_size == SHA256_HASH_SIZE) {
1121+
*xattr_digest = calloc(1, SHA256_HASH_SIZE + 1);
11221122
if (!*xattr_digest)
11231123
goto oom;
11241124

1125-
memcpy(*xattr_digest, read_digest, SHA1_HASH_SIZE);
1125+
memcpy(*xattr_digest, read_digest, SHA256_HASH_SIZE);
11261126
}
11271127

11281128
if (status) {
1129-
*calculated_digest = calloc(1, SHA1_HASH_SIZE + 1);
1129+
*calculated_digest = calloc(1, SHA256_HASH_SIZE + 1);
11301130
if (!*calculated_digest)
11311131
goto oom;
11321132

1133-
memcpy(*calculated_digest, hash_digest, SHA1_HASH_SIZE);
1133+
memcpy(*calculated_digest, hash_digest, SHA256_HASH_SIZE);
11341134
}
11351135

1136-
if (status && read_size == SHA1_HASH_SIZE &&
1137-
memcmp(read_digest, hash_digest, SHA1_HASH_SIZE) == 0)
1136+
if (status && read_size == SHA256_HASH_SIZE &&
1137+
memcmp(read_digest, hash_digest, SHA256_HASH_SIZE) == 0)
11381138
return true;
11391139

11401140
return false;
@@ -1154,22 +1154,22 @@ static bool hash_all_partial_matches(struct selabel_handle *rec, const char *key
11541154
return false;
11551155
}
11561156

1157-
Sha1Context context;
1158-
Sha1Initialise(&context);
1157+
Sha256Context context;
1158+
Sha256Initialise(&context);
11591159
size_t i;
11601160
for (i = 0; i < total_matches; i++) {
11611161
char* regex_str = matches[i]->regex_str;
11621162
mode_t mode = matches[i]->mode;
11631163
char* ctx_raw = matches[i]->lr.ctx_raw;
11641164

1165-
Sha1Update(&context, regex_str, strlen(regex_str) + 1);
1166-
Sha1Update(&context, &mode, sizeof(mode_t));
1167-
Sha1Update(&context, ctx_raw, strlen(ctx_raw) + 1);
1165+
Sha256Update(&context, regex_str, strlen(regex_str) + 1);
1166+
Sha256Update(&context, &mode, sizeof(mode_t));
1167+
Sha256Update(&context, ctx_raw, strlen(ctx_raw) + 1);
11681168
}
11691169

1170-
SHA1_HASH sha1_hash;
1171-
Sha1Finalise(&context, &sha1_hash);
1172-
memcpy(digest, sha1_hash.bytes, SHA1_HASH_SIZE);
1170+
SHA256_HASH sha256_hash;
1171+
Sha256Finalise(&context, &sha256_hash);
1172+
memcpy(digest, sha256_hash.bytes, SHA256_HASH_SIZE);
11731173

11741174
free(matches);
11751175
return true;

libselinux/src/label_internal.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <stdio.h>
1414
#include <selinux/selinux.h>
1515
#include <selinux/label.h>
16-
#include "sha1.h"
16+
#include "sha256.h"
1717

1818
#if defined(ANDROID) || defined(__APPLE__)
1919
// Android and Mac do not have fgets_unlocked()
@@ -47,15 +47,15 @@ int selabel_service_init(struct selabel_handle *rec,
4747
*/
4848

4949
/*
50-
* Calculate an SHA1 hash of all the files used to build the specs.
50+
* Calculate an SHA256 hash of all the files used to build the specs.
5151
* The hash value is held in rec->digest if SELABEL_OPT_DIGEST set. To
5252
* calculate the hash the hashbuf will hold a concatenation of all the files
5353
* used. This is released once the value has been calculated.
5454
*/
55-
#define DIGEST_SPECFILE_SIZE SHA1_HASH_SIZE
55+
#define DIGEST_SPECFILE_SIZE SHA256_HASH_SIZE
5656
#define DIGEST_FILES_MAX 8
5757
struct selabel_digest {
58-
unsigned char *digest; /* SHA1 digest of specfiles */
58+
unsigned char *digest; /* SHA256 digest of specfiles */
5959
unsigned char *hashbuf; /* buffer to hold specfiles */
6060
size_t hashbuf_size; /* buffer size */
6161
size_t specfile_cnt; /* how many specfiles processed */
@@ -110,7 +110,7 @@ struct selabel_handle {
110110
*/
111111
char *spec_file;
112112

113-
/* ptr to SHA1 hash information if SELABEL_OPT_DIGEST set */
113+
/* ptr to SHA256 hash information if SELABEL_OPT_DIGEST set */
114114
struct selabel_digest *digest;
115115
};
116116

libselinux/src/label_support.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,27 +114,27 @@ int read_spec_entries(char *line_buf, const char **errbuf, int num_args, ...)
114114
/* Once all the specfiles are in the hash_buf, generate the hash. */
115115
void digest_gen_hash(struct selabel_digest *digest)
116116
{
117-
Sha1Context context;
117+
Sha256Context context;
118118
size_t remaining_size;
119119
const unsigned char *ptr;
120120

121121
/* If SELABEL_OPT_DIGEST not set then just return */
122122
if (!digest)
123123
return;
124124

125-
Sha1Initialise(&context);
125+
Sha256Initialise(&context);
126126

127127
/* Process in blocks of UINT32_MAX bytes */
128128
remaining_size = digest->hashbuf_size;
129129
ptr = digest->hashbuf;
130130
while (remaining_size > UINT32_MAX) {
131-
Sha1Update(&context, ptr, UINT32_MAX);
131+
Sha256Update(&context, ptr, UINT32_MAX);
132132
remaining_size -= UINT32_MAX;
133133
ptr += UINT32_MAX;
134134
}
135-
Sha1Update(&context, ptr, remaining_size);
135+
Sha256Update(&context, ptr, remaining_size);
136136

137-
Sha1Finalise(&context, (SHA1_HASH *)digest->digest);
137+
Sha256Finalise(&context, (SHA256_HASH *)digest->digest);
138138
free(digest->hashbuf);
139139
digest->hashbuf = NULL;
140140
}

libselinux/src/selinux_restorecon.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#include "callbacks.h"
3838
#include "selinux_internal.h"
3939
#include "label_file.h"
40-
#include "sha1.h"
40+
#include "sha256.h"
4141

4242
#define STAR_COUNT 1024
4343

@@ -304,7 +304,7 @@ static uint64_t exclude_non_seclabel_mounts(void)
304304
static int add_xattr_entry(const char *directory, bool delete_nonmatch,
305305
bool delete_all)
306306
{
307-
char *sha1_buf = NULL;
307+
char *sha256_buf = NULL;
308308
size_t i, digest_len = 0;
309309
int rc;
310310
enum digest_result digest_result;
@@ -328,15 +328,15 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch,
328328
}
329329

330330
/* Convert entry to a hex encoded string. */
331-
sha1_buf = malloc(digest_len * 2 + 1);
332-
if (!sha1_buf) {
331+
sha256_buf = malloc(digest_len * 2 + 1);
332+
if (!sha256_buf) {
333333
free(xattr_digest);
334334
free(calculated_digest);
335335
goto oom;
336336
}
337337

338338
for (i = 0; i < digest_len; i++)
339-
sprintf((&sha1_buf[i * 2]), "%02x", xattr_digest[i]);
339+
sprintf((&sha256_buf[i * 2]), "%02x", xattr_digest[i]);
340340

341341
digest_result = match ? MATCH : NOMATCH;
342342

@@ -356,23 +356,23 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch,
356356
/* Now add entries to link list. */
357357
new_entry = malloc(sizeof(struct dir_xattr));
358358
if (!new_entry) {
359-
free(sha1_buf);
359+
free(sha256_buf);
360360
goto oom;
361361
}
362362
new_entry->next = NULL;
363363

364364
new_entry->directory = strdup(directory);
365365
if (!new_entry->directory) {
366366
free(new_entry);
367-
free(sha1_buf);
367+
free(sha256_buf);
368368
goto oom;
369369
}
370370

371-
new_entry->digest = strdup(sha1_buf);
371+
new_entry->digest = strdup(sha256_buf);
372372
if (!new_entry->digest) {
373373
free(new_entry->directory);
374374
free(new_entry);
375-
free(sha1_buf);
375+
free(sha256_buf);
376376
goto oom;
377377
}
378378

@@ -386,7 +386,7 @@ static int add_xattr_entry(const char *directory, bool delete_nonmatch,
386386
dir_xattr_last = new_entry;
387387
}
388388

389-
free(sha1_buf);
389+
free(sha256_buf);
390390
return 0;
391391

392392
oom:
@@ -776,7 +776,7 @@ static int restorecon_sb(const char *pathname, const struct stat *sb,
776776

777777
struct dir_hash_node {
778778
char *path;
779-
uint8_t digest[SHA1_HASH_SIZE];
779+
uint8_t digest[SHA256_HASH_SIZE];
780780
struct dir_hash_node *next;
781781
};
782782
/*
@@ -1282,7 +1282,7 @@ static int selinux_restorecon_common(const char *pathname_orig,
12821282
if (setxattr(current->path,
12831283
RESTORECON_PARTIAL_MATCH_DIGEST,
12841284
current->digest,
1285-
SHA1_HASH_SIZE, 0) < 0) {
1285+
SHA256_HASH_SIZE, 0) < 0) {
12861286
selinux_log(SELINUX_ERROR,
12871287
"setxattr failed: %s: %m\n",
12881288
current->path);

0 commit comments

Comments
 (0)