|
| 1 | +From 915004f403cb25fadb207ddfdbe6a2f43bd44fac Mon Sep 17 00:00:00 2001 |
| 2 | +From: =?utf8?q?P=C3=A1draig=20Brady?= < [email protected]> |
| 3 | +Date: Fri, 17 Jan 2025 17:29:34 +0000 |
| 4 | +Subject: [PATCH] ls: fix crash with --context |
| 5 | + |
| 6 | +* src/ls.c (main): Flag that we need to stat() |
| 7 | +if we're going to get security context (call file_has_aclinfo_cache). |
| 8 | +(file_has_aclinfo_cache): Be defensive and only lookup the device |
| 9 | +for the file if the stat has been performed. |
| 10 | +(has_capability_cache): Likewise. |
| 11 | +* tests/ls/selinux-segfault.sh: Add a test case. |
| 12 | +* NEWS: Mention the bug fix. |
| 13 | +Reported by Bruno Haible. |
| 14 | +--- |
| 15 | + NEWS | 5 +++++ |
| 16 | + src/ls.c | 10 +++++----- |
| 17 | + tests/ls/selinux-segfault.sh | 3 +++ |
| 18 | + 3 files changed, 13 insertions(+), 5 deletions(-) |
| 19 | + |
| 20 | +diff --git a/NEWS b/NEWS |
| 21 | +index 9be61e4c4..c9ba5f196 100644 |
| 22 | +--- a/NEWS |
| 23 | ++++ b/NEWS |
| 24 | +@@ -2,6 +2,11 @@ GNU coreutils NEWS -*- outline -*- |
| 25 | + |
| 26 | + * Noteworthy changes in release ?.? (????-??-??) [?] |
| 27 | + |
| 28 | ++** Bug fixes |
| 29 | ++ |
| 30 | ++ `ls -Z dir` would crash. |
| 31 | ++ [bug introduced in coreutils-9.6] |
| 32 | ++ |
| 33 | + |
| 34 | + * Noteworthy changes in release 9.6 (2025-01-17) [stable] |
| 35 | + |
| 36 | +diff --git a/src/ls.c b/src/ls.c |
| 37 | +index 321536021..f67167f16 100644 |
| 38 | +--- a/src/ls.c |
| 39 | ++++ b/src/ls.c |
| 40 | +@@ -1768,7 +1768,7 @@ main (int argc, char **argv) |
| 41 | + |
| 42 | + format_needs_stat = ((sort_type == sort_time) | (sort_type == sort_size) |
| 43 | + | (format == long_format) |
| 44 | +- | print_block_size | print_hyperlink); |
| 45 | ++ | print_block_size | print_hyperlink | print_scontext); |
| 46 | + format_needs_type = ((! format_needs_stat) |
| 47 | + & (recursive | print_with_color | print_scontext |
| 48 | + | directories_first |
| 49 | +@@ -3309,7 +3309,7 @@ file_has_aclinfo_cache (char const *file, struct fileinfo *f, |
| 50 | + static int unsupported_scontext_err; |
| 51 | + static dev_t unsupported_device; |
| 52 | + |
| 53 | +- if (f->stat.st_dev == unsupported_device) |
| 54 | ++ if (f->stat_ok && f->stat.st_dev == unsupported_device) |
| 55 | + { |
| 56 | + ai->buf = ai->u.__gl_acl_ch; |
| 57 | + ai->size = 0; |
| 58 | +@@ -3322,7 +3322,7 @@ file_has_aclinfo_cache (char const *file, struct fileinfo *f, |
| 59 | + errno = 0; |
| 60 | + int n = file_has_aclinfo (file, ai, flags); |
| 61 | + int err = errno; |
| 62 | +- if (n <= 0 && !acl_errno_valid (err)) |
| 63 | ++ if (f->stat_ok && n <= 0 && !acl_errno_valid (err)) |
| 64 | + { |
| 65 | + unsupported_return = n; |
| 66 | + unsupported_scontext = ai->scontext; |
| 67 | +@@ -3342,14 +3342,14 @@ has_capability_cache (char const *file, struct fileinfo *f) |
| 68 | + found that has_capability fails indicating lack of support. */ |
| 69 | + static dev_t unsupported_device; |
| 70 | + |
| 71 | +- if (f->stat.st_dev == unsupported_device) |
| 72 | ++ if (f->stat_ok && f->stat.st_dev == unsupported_device) |
| 73 | + { |
| 74 | + errno = ENOTSUP; |
| 75 | + return 0; |
| 76 | + } |
| 77 | + |
| 78 | + bool b = has_capability (file); |
| 79 | +- if ( !b && !acl_errno_valid (errno)) |
| 80 | ++ if (f->stat_ok && !b && !acl_errno_valid (errno)) |
| 81 | + unsupported_device = f->stat.st_dev; |
| 82 | + return b; |
| 83 | + } |
| 84 | +diff --git a/tests/ls/selinux-segfault.sh b/tests/ls/selinux-segfault.sh |
| 85 | +index 11623acb3..1cac2b5fc 100755 |
| 86 | +--- a/tests/ls/selinux-segfault.sh |
| 87 | ++++ b/tests/ls/selinux-segfault.sh |
| 88 | +@@ -30,4 +30,7 @@ mkdir sedir || framework_failure_ |
| 89 | + ln -sf missing sedir/broken || framework_failure_ |
| 90 | + returns_ 1 ls -L -R -Z -m sedir > out || fail=1 |
| 91 | + |
| 92 | ++# ls 9.6 would segfault with the following |
| 93 | ++ls -Z . > out || fail=1 |
| 94 | ++ |
| 95 | + Exit $fail |
| 96 | +-- |
| 97 | +2.17.1 |
| 98 | + |
0 commit comments