Skip to content

Commit 414c776

Browse files
Yi Zhaorpurdie
authored andcommitted
coreutils: fix segfault for ls --context
Backport a patch to fix crash for ls --context when enable selinux: root@qemux86-64:~# ls -Z /home Segmentation fault (core dumped) Signed-off-by: Yi Zhao <yi.zhao@windriver.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
1 parent e6ff5f4 commit 414c776

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
From 43a63408630e5064317823702518099f0ea652dd Mon Sep 17 00:00:00 2001
2+
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <P@draigBrady.com>
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+
Upstream-Status: Backport
16+
[https://git.savannah.gnu.org/cgit/coreutils.git/commit/?id=915004f403cb25fadb207ddfdbe6a2f43bd44fac]
17+
18+
Signed-off-by: Yi Zhao <yi.zhao@windriver.com>
19+
---
20+
NEWS | 3 +++
21+
src/ls.c | 10 +++++-----
22+
tests/ls/selinux-segfault.sh | 3 +++
23+
3 files changed, 11 insertions(+), 5 deletions(-)
24+
25+
diff --git a/NEWS b/NEWS
26+
index 3799f75..65867f9 100644
27+
--- a/NEWS
28+
+++ b/NEWS
29+
@@ -4,6 +4,9 @@ GNU coreutils NEWS -*- outline -*-
30+
31+
** Bug fixes
32+
33+
+ `ls -Z dir` would crash.
34+
+ [bug introduced in coreutils-9.6]
35+
+
36+
cp fixes support for --update=none-fail, which would have been
37+
rejected as an invalid option.
38+
[bug introduced in coreutils-9.5]
39+
diff --git a/src/ls.c b/src/ls.c
40+
index 3215360..f67167f 100644
41+
--- a/src/ls.c
42+
+++ b/src/ls.c
43+
@@ -1768,7 +1768,7 @@ main (int argc, char **argv)
44+
45+
format_needs_stat = ((sort_type == sort_time) | (sort_type == sort_size)
46+
| (format == long_format)
47+
- | print_block_size | print_hyperlink);
48+
+ | print_block_size | print_hyperlink | print_scontext);
49+
format_needs_type = ((! format_needs_stat)
50+
& (recursive | print_with_color | print_scontext
51+
| directories_first
52+
@@ -3309,7 +3309,7 @@ file_has_aclinfo_cache (char const *file, struct fileinfo *f,
53+
static int unsupported_scontext_err;
54+
static dev_t unsupported_device;
55+
56+
- if (f->stat.st_dev == unsupported_device)
57+
+ if (f->stat_ok && f->stat.st_dev == unsupported_device)
58+
{
59+
ai->buf = ai->u.__gl_acl_ch;
60+
ai->size = 0;
61+
@@ -3322,7 +3322,7 @@ file_has_aclinfo_cache (char const *file, struct fileinfo *f,
62+
errno = 0;
63+
int n = file_has_aclinfo (file, ai, flags);
64+
int err = errno;
65+
- if (n <= 0 && !acl_errno_valid (err))
66+
+ if (f->stat_ok && n <= 0 && !acl_errno_valid (err))
67+
{
68+
unsupported_return = n;
69+
unsupported_scontext = ai->scontext;
70+
@@ -3342,14 +3342,14 @@ has_capability_cache (char const *file, struct fileinfo *f)
71+
found that has_capability fails indicating lack of support. */
72+
static dev_t unsupported_device;
73+
74+
- if (f->stat.st_dev == unsupported_device)
75+
+ if (f->stat_ok && f->stat.st_dev == unsupported_device)
76+
{
77+
errno = ENOTSUP;
78+
return 0;
79+
}
80+
81+
bool b = has_capability (file);
82+
- if ( !b && !acl_errno_valid (errno))
83+
+ if (f->stat_ok && !b && !acl_errno_valid (errno))
84+
unsupported_device = f->stat.st_dev;
85+
return b;
86+
}
87+
diff --git a/tests/ls/selinux-segfault.sh b/tests/ls/selinux-segfault.sh
88+
index 11623ac..1cac2b5 100755
89+
--- a/tests/ls/selinux-segfault.sh
90+
+++ b/tests/ls/selinux-segfault.sh
91+
@@ -30,4 +30,7 @@ mkdir sedir || framework_failure_
92+
ln -sf missing sedir/broken || framework_failure_
93+
returns_ 1 ls -L -R -Z -m sedir > out || fail=1
94+
95+
+# ls 9.6 would segfault with the following
96+
+ls -Z . > out || fail=1
97+
+
98+
Exit $fail
99+
--
100+
2.34.1
101+

meta/recipes-core/coreutils/coreutils_9.6.bb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ SRC_URI = "${GNU_MIRROR}/coreutils/${BP}.tar.xz \
1717
file://remove-usr-local-lib-from-m4.patch \
1818
file://0001-local.mk-fix-cross-compiling-problem.patch \
1919
file://intermittent-testfailure.patch \
20+
file://0001-ls-fix-crash-with-context.patch \
2021
file://run-ptest \
2122
"
2223
SRC_URI[sha256sum] = "7a0124327b398fd9eb1a6abde583389821422c744ffa10734b24f557610d3283"

0 commit comments

Comments
 (0)