Skip to content

Commit 0897a52

Browse files
authored
libselinux: Support SELinux policy tree at /usr/etc/selinux. (microsoft#14750)
Signed-off-by: Chris PeBenito <[email protected]>
1 parent bc998bd commit 0897a52

File tree

6 files changed

+104
-14
lines changed

6 files changed

+104
-14
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
diff --git a/libselinux/src/init.c b/libselinux/src/init.c
2+
index 542c979b..acba93f6 100644
3+
--- a/libselinux/src/init.c
4+
+++ b/libselinux/src/init.c
5+
@@ -149,7 +149,7 @@ static void init_lib(void)
6+
selinux_page_size = sysconf(_SC_PAGE_SIZE);
7+
init_selinuxmnt();
8+
#ifndef ANDROID
9+
- has_selinux_config = (access(SELINUXCONFIG, F_OK) == 0);
10+
+ has_selinux_config = (access(selinux_config_path(), F_OK) == 0);
11+
#endif
12+
}
13+
14+
diff --git a/libselinux/src/selinux_config.c b/libselinux/src/selinux_config.c
15+
index 1d8cfb71..79deb6fc 100644
16+
--- a/libselinux/src/selinux_config.c
17+
+++ b/libselinux/src/selinux_config.c
18+
@@ -89,7 +89,7 @@ static const uint16_t file_path_suffixes_idx[NEL] = {
19+
int selinux_getenforcemode(int *enforce)
20+
{
21+
int ret = -1;
22+
- FILE *cfg = fopen(SELINUXCONFIG, "re");
23+
+ FILE *cfg = fopen(selinux_config_path(), "re");
24+
if (cfg) {
25+
char *buf;
26+
char *tag;
27+
@@ -153,7 +153,6 @@ static int setpolicytype(const char *type)
28+
}
29+
30+
static char *selinux_policyroot = NULL;
31+
-static const char *selinux_rootpath = SELINUXDIR;
32+
33+
static void init_selinux_config(void)
34+
{
35+
@@ -166,7 +165,7 @@ static void init_selinux_config(void)
36+
if (selinux_policyroot)
37+
return;
38+
39+
- fp = fopen(SELINUXCONFIG, "re");
40+
+ fp = fopen(selinux_config_path(), "re");
41+
if (fp) {
42+
__fsetlocking(fp, FSETLOCKING_BYCALLER);
43+
while ((len = getline(&line_buf, &line_len, fp)) > 0) {
44+
@@ -228,7 +227,7 @@ static void init_selinux_config(void)
45+
if (!selinux_policytype && setpolicytype(SELINUXDEFAULT) != 0)
46+
return;
47+
48+
- if (asprintf(&selinux_policyroot, "%s%s", SELINUXDIR, selinux_policytype) == -1)
49+
+ if (asprintf(&selinux_policyroot, "%s%s", selinux_path(), selinux_policytype) == -1)
50+
return;
51+
52+
for (i = 0; i < NEL; i++)
53+
@@ -312,7 +311,13 @@ int selinux_set_policy_root(const char *path)
54+
55+
const char *selinux_path(void)
56+
{
57+
- return selinux_rootpath;
58+
+ return access(SELINUXDIR_RO, F_OK) == 0 ? SELINUXDIR_RO : SELINUXDIR_RW;
59+
+}
60+
+
61+
+
62+
+const char *selinux_config_path(void)
63+
+{
64+
+ return access(SELINUXDIR_RO, F_OK) == 0 ? SELINUXCONFIG_RO : SELINUXCONFIG_RW;
65+
}
66+
67+
68+
diff --git a/libselinux/src/selinux_internal.h b/libselinux/src/selinux_internal.h
69+
index af69ff04..0ff78e04 100644
70+
--- a/libselinux/src/selinux_internal.h
71+
+++ b/libselinux/src/selinux_internal.h
72+
@@ -89,8 +89,11 @@ extern int selinux_page_size ;
73+
pthread_cond_wait \
74+
)
75+
76+
-#define SELINUXDIR "/etc/selinux/"
77+
-#define SELINUXCONFIG SELINUXDIR "config"
78+
+#define SELINUXDIR_RW "/etc/selinux/"
79+
+#define SELINUXDIR_RO "/usr/etc/selinux/"
80+
+#define SELINUXCONFIG_RO SELINUXDIR_RO "config"
81+
+#define SELINUXCONFIG_RW SELINUXDIR_RW "config"
82+
+const char *selinux_config_path(void);
83+
84+
extern int has_selinux_config ;
85+

SPECS/libselinux/libselinux.spec

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
Summary: SELinux library and simple utilities
22
Name: libselinux
33
Version: 3.6
4-
Release: 3%{?dist}
4+
Release: 4%{?dist}
55
License: Public Domain
66
Vendor: Microsoft Corporation
77
Distribution: Azure Linux
88
Group: System Environment/Libraries
99
URL: https://github.com/SELinuxProject/selinux/wiki
1010
Source0: https://github.com/SELinuxProject/selinux/releases/download/%{version}/%{name}-%{version}.tar.gz
11+
Patch1: libselinux-usr-etc-selinux-path.patch
1112
BuildRequires: libsepol-devel
1213
BuildRequires: pcre2-devel
1314
BuildRequires: swig
@@ -64,7 +65,7 @@ The libselinux-python package contains the python3 bindings for developing
6465
SELinux applications.
6566

6667
%prep
67-
%autosetup
68+
%autosetup -p2
6869

6970
%build
7071
export USE_PCRE2="y"
@@ -109,6 +110,10 @@ echo "d /run/setrans 0755 root root" > %{buildroot}/%{_libdir}/tmpfiles.d/libsel
109110
%{python3_sitelib}/*
110111

111112
%changelog
113+
* Tue Sep 30 2025 Chris PeBenito <[email protected]> - 3.6-4
114+
- Support SELinux policy tree at /usr/etc/selinux. This supports a
115+
dm-verity protected policy in a read-only /usr.
116+
112117
* Wed Apr 03 2024 Betty Lakes <[email protected]> - 3.6-3
113118
- Move to pcre2
114119

toolkit/resources/manifests/package/pkggen_core_aarch64.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ python3-libs-3.12.9-5.azl3.aarch64.rpm
250250
python3-setuptools-69.0.3-5.azl3.noarch.rpm
251251
python3-pygments-2.7.4-2.azl3.noarch.rpm
252252
which-2.21-8.azl3.aarch64.rpm
253-
libselinux-3.6-3.azl3.aarch64.rpm
253+
libselinux-3.6-4.azl3.aarch64.rpm
254254
slang-2.3.3-1.azl3.aarch64.rpm
255255
newt-0.52.23-1.azl3.aarch64.rpm
256256
newt-lang-0.52.23-1.azl3.aarch64.rpm

toolkit/resources/manifests/package/pkggen_core_x86_64.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ python3-libs-3.12.9-5.azl3.x86_64.rpm
250250
python3-setuptools-69.0.3-5.azl3.noarch.rpm
251251
python3-pygments-2.7.4-2.azl3.noarch.rpm
252252
which-2.21-8.azl3.x86_64.rpm
253-
libselinux-3.6-3.azl3.x86_64.rpm
253+
libselinux-3.6-4.azl3.x86_64.rpm
254254
slang-2.3.3-1.azl3.x86_64.rpm
255255
newt-0.52.23-1.azl3.x86_64.rpm
256256
newt-lang-0.52.23-1.azl3.x86_64.rpm

toolkit/resources/manifests/package/toolchain_aarch64.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,11 @@ libpipeline-debuginfo-1.5.7-1.azl3.aarch64.rpm
217217
libpipeline-devel-1.5.7-1.azl3.aarch64.rpm
218218
libpkgconf-2.0.2-1.azl3.aarch64.rpm
219219
libpkgconf-devel-2.0.2-1.azl3.aarch64.rpm
220-
libselinux-3.6-3.azl3.aarch64.rpm
221-
libselinux-debuginfo-3.6-3.azl3.aarch64.rpm
222-
libselinux-devel-3.6-3.azl3.aarch64.rpm
223-
libselinux-python3-3.6-3.azl3.aarch64.rpm
224-
libselinux-utils-3.6-3.azl3.aarch64.rpm
220+
libselinux-3.6-4.azl3.aarch64.rpm
221+
libselinux-debuginfo-3.6-4.azl3.aarch64.rpm
222+
libselinux-devel-3.6-4.azl3.aarch64.rpm
223+
libselinux-python3-3.6-4.azl3.aarch64.rpm
224+
libselinux-utils-3.6-4.azl3.aarch64.rpm
225225
libsepol-3.6-2.azl3.aarch64.rpm
226226
libsepol-debuginfo-3.6-2.azl3.aarch64.rpm
227227
libsepol-devel-3.6-2.azl3.aarch64.rpm

toolkit/resources/manifests/package/toolchain_x86_64.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,11 @@ libpipeline-debuginfo-1.5.7-1.azl3.x86_64.rpm
225225
libpipeline-devel-1.5.7-1.azl3.x86_64.rpm
226226
libpkgconf-2.0.2-1.azl3.x86_64.rpm
227227
libpkgconf-devel-2.0.2-1.azl3.x86_64.rpm
228-
libselinux-3.6-3.azl3.x86_64.rpm
229-
libselinux-debuginfo-3.6-3.azl3.x86_64.rpm
230-
libselinux-devel-3.6-3.azl3.x86_64.rpm
231-
libselinux-python3-3.6-3.azl3.x86_64.rpm
232-
libselinux-utils-3.6-3.azl3.x86_64.rpm
228+
libselinux-3.6-4.azl3.x86_64.rpm
229+
libselinux-debuginfo-3.6-4.azl3.x86_64.rpm
230+
libselinux-devel-3.6-4.azl3.x86_64.rpm
231+
libselinux-python3-3.6-4.azl3.x86_64.rpm
232+
libselinux-utils-3.6-4.azl3.x86_64.rpm
233233
libsepol-3.6-2.azl3.x86_64.rpm
234234
libsepol-debuginfo-3.6-2.azl3.x86_64.rpm
235235
libsepol-devel-3.6-2.azl3.x86_64.rpm

0 commit comments

Comments
 (0)