Skip to content

Commit 519f05d

Browse files
shadichyaviraxp
andauthored
kernel: Refactor selinux/selinux.c (#2881)
Signed-off-by: shadichy <[email protected]> Co-authored-by: Wang Han <[email protected]>
1 parent 0e346e6 commit 519f05d

File tree

2 files changed

+23
-60
lines changed

2 files changed

+23
-60
lines changed

kernel/selinux/Makefile

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@ obj-y += selinux.o
22
obj-y += sepolicy.o
33
obj-y += rules.o
44

5-
ifeq ($(shell grep -q " current_sid(void)" $(srctree)/security/selinux/include/objsec.h; echo $$?),0)
6-
ccflags-y += -DKSU_COMPAT_HAS_CURRENT_SID
7-
endif
8-
9-
ifeq ($(shell grep -q "struct selinux_state " $(srctree)/security/selinux/include/security.h; echo $$?),0)
10-
ccflags-y += -DKSU_COMPAT_HAS_SELINUX_STATE
11-
endif
12-
135
ccflags-y += -Wno-strict-prototypes -Wno-int-conversion
146
ccflags-y += -Wno-declaration-after-statement -Wno-unused-function
157
ccflags-y += -I$(srctree)/security/selinux -I$(srctree)/security/selinux/include

kernel/selinux/selinux.c

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,6 @@ void setup_selinux(const char *domain)
4242
pr_err("transive domain failed.\n");
4343
return;
4444
}
45-
46-
/* we didn't need this now, we have change selinux rules when boot!
47-
if (!is_domain_permissive) {
48-
if (set_domain_permissive() == 0) {
49-
is_domain_permissive = true;
50-
}
51-
}*/
5245
}
5346

5447
void setenforce(bool enforce)
@@ -73,27 +66,28 @@ bool getenforce()
7366
#endif
7467
}
7568

76-
#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0)) && \
77-
!defined(KSU_COMPAT_HAS_CURRENT_SID)
78-
/*
79-
* get the subjective security ID of the current task
80-
*/
81-
static inline u32 current_sid(void)
82-
{
83-
const struct task_security_struct *tsec = current_security();
69+
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 14, 0)
70+
struct lsm_context {
71+
char *context;
72+
u32 len;
73+
};
8474

85-
return tsec->sid;
75+
static int __security_secid_to_secctx(u32 secid, struct lsm_context *cp)
76+
{
77+
return security_secid_to_secctx(secid, &cp->context, &cp->len);
78+
}
79+
static void __security_release_secctx(struct lsm_context *cp)
80+
{
81+
return security_release_secctx(cp->context, cp->len);
8682
}
83+
#else
84+
#define __security_secid_to_secctx security_secid_to_secctx
85+
#define __security_release_secctx security_release_secctx
8786
#endif
8887

8988
bool is_task_ksu_domain(const struct cred* cred)
9089
{
91-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 14, 0)
92-
struct lsm_context ctx;
93-
#else
94-
char *domain;
95-
u32 seclen;
96-
#endif
90+
struct lsm_context ctx;
9791
bool result;
9892
if (!cred) {
9993
return false;
@@ -102,21 +96,12 @@ bool is_task_ksu_domain(const struct cred* cred)
10296
if (!tsec) {
10397
return false;
10498
}
105-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 14, 0)
106-
int err = security_secid_to_secctx(tsec->sid, &ctx);
107-
#else
108-
int err = security_secid_to_secctx(tsec->sid, &domain, &seclen);
109-
#endif
99+
int err = __security_secid_to_secctx(tsec->sid, &ctx);
110100
if (err) {
111101
return false;
112102
}
113-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 14, 0)
114-
result = strncmp(KERNEL_SU_DOMAIN, ctx.context, ctx.len) == 0;
115-
security_release_secctx(&ctx);
116-
#else
117-
result = strncmp(KERNEL_SU_DOMAIN, domain, seclen) == 0;
118-
security_release_secctx(domain, seclen);
119-
#endif
103+
result = strncmp(KERNEL_SU_DOMAIN, ctx.context, ctx.len) == 0;
104+
__security_release_secctx(&ctx);
120105
return result;
121106
}
122107

@@ -135,28 +120,14 @@ bool is_zygote(const struct cred* cred)
135120
if (!tsec) {
136121
return false;
137122
}
138-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 14, 0)
139-
struct lsm_context ctx;
140-
#else
141-
char *domain;
142-
u32 seclen;
143-
#endif
123+
struct lsm_context ctx;
144124
bool result;
145-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 14, 0)
146-
int err = security_secid_to_secctx(tsec->sid, &ctx);
147-
#else
148-
int err = security_secid_to_secctx(tsec->sid, &domain, &seclen);
149-
#endif
125+
int err = __security_secid_to_secctx(tsec->sid, &ctx);
150126
if (err) {
151127
return false;
152128
}
153-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 14, 0)
154-
result = strncmp("u:r:zygote:s0", ctx.context, ctx.len) == 0;
155-
security_release_secctx(&ctx);
156-
#else
157-
result = strncmp("u:r:zygote:s0", domain, seclen) == 0;
158-
security_release_secctx(domain, seclen);
159-
#endif
129+
result = strncmp("u:r:zygote:s0", ctx.context, ctx.len) == 0;
130+
__security_release_secctx(&ctx);
160131
return result;
161132
}
162133

0 commit comments

Comments
 (0)