Skip to content

Commit 8908a6d

Browse files
committed
Do not count hashes for non regular files
Signed-off-by: Radovan Sroka <rsroka@redhat.com>
1 parent 9486d5c commit 8908a6d

File tree

4 files changed

+30
-27
lines changed

4 files changed

+30
-27
lines changed

src/cli/file-cli.c

+1-9
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,11 @@ static int ftw_add_list_append(const char *fpath,
6767
*/
6868
static int add_list_load_path(const char *path)
6969
{
70-
int fd = open(path, O_RDONLY);
71-
if (fd < 0) {
72-
msg(LOG_ERR, "Cannot open %s", path);
73-
return 1;
74-
}
75-
7670
struct stat sb;
77-
if (fstat(fd, &sb)) {
71+
if (stat(path, &sb)) {
7872
msg(LOG_ERR, "Cannot stat %s", path);
79-
close(fd);
8073
return 1;
8174
}
82-
close(fd);
8375

8476
if (S_ISDIR(sb.st_mode))
8577
nftw(path, &ftw_add_list_append, FTW_NOPENFD, FTW_FLAGS);

src/library/file.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* Boston, MA 02110-1335, USA.
2020
*
2121
* Authors:
22+
* Radovan Sroka <rsroka@redhat.com>
2223
* Steve Grubb <sgrubb@redhat.com>
2324
*/
2425

@@ -440,17 +441,15 @@ static ssize_t safe_read(int fd, char *buf, size_t size)
440441
* If a size of 0 is passed, it will return a NULL pointer.
441442
* If there is an error with mmap, it will also return a NULL pointer.
442443
*/
443-
static const char *degenerate_hash_sha = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
444-
static const char *degenerate_hash_md5 = "d41d8cd98f00b204e9800998ecf8427e";
445444
char *get_hash_from_fd2(int fd, size_t size, const int is_sha)
446445
{
447446
unsigned char *mapped;
448447
char *digest = NULL;
449448

450449
if (size == 0) {
451450
if (is_sha)
452-
return strdup(degenerate_hash_sha);
453-
return strdup(degenerate_hash_md5);
451+
return strdup(DEGENERATE_HASH_SHA);
452+
return strdup(DEGENERATE_HASH_MD5);
454453
}
455454

456455
mapped = mmap(0, size, PROT_READ, MAP_PRIVATE|MAP_POPULATE, fd, 0);

src/library/file.h

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* Boston, MA 02110-1335, USA.
2020
*
2121
* Authors:
22+
* Radovan Sroka <rsroka@redhat.com>
2223
* Steve Grubb <sgrubb@redhat.com>
2324
*/
2425

@@ -43,6 +44,9 @@ struct file_info
4344
#define SHA256_LEN 32
4445
#define SHA512_LEN 64
4546

47+
#define DEGENERATE_HASH_SHA "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
48+
#define DEGENERATE_HASH_MD5 "d41d8cd98f00b204e9800998ecf8427e"
49+
4650
void file_init(void);
4751
void file_close(void);
4852
struct file_info *stat_file_entry(int fd) __attr_dealloc_free;

src/library/trust-file.c

+22-14
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,34 @@ int _count;
7474
*/
7575
static char *make_data_string(const char *path)
7676
{
77-
int fd = open(path, O_RDONLY);
78-
if (fd < 0) {
79-
msg(LOG_ERR, "Cannot open %s", path);
80-
return NULL;
81-
}
82-
8377
// Get the size
8478
struct stat sb;
85-
if (fstat(fd, &sb)) {
79+
if (stat(path, &sb)) {
8680
msg(LOG_ERR, "Cannot stat %s", path);
87-
close(fd);
8881
return NULL;
8982
}
9083

91-
// Get the hash
92-
char *hash = get_hash_from_fd2(fd, sb.st_size, 1);
93-
close(fd);
94-
if (!hash) {
95-
msg(LOG_ERR, "Cannot hash %s", path);
96-
return NULL;
84+
char *hash = NULL;
85+
if (S_ISREG(sb.st_mode)) {
86+
// Get the hash
87+
88+
int fd = open(path, O_RDONLY);
89+
if (fd < 0) {
90+
msg(LOG_ERR, "Cannot open %s", path);
91+
return NULL;
92+
}
93+
94+
hash = get_hash_from_fd2(fd, sb.st_size, 1);
95+
96+
close(fd);
97+
98+
if (!hash) {
99+
msg(LOG_ERR, "Cannot hash %s", path);
100+
return NULL;
101+
}
102+
103+
} else {
104+
hash = strdup(DEGENERATE_HASH_SHA);
97105
}
98106

99107
char *line;

0 commit comments

Comments
 (0)