Skip to content

Commit 652bb9b

Browse files
committed
SELinux: Use dentry name in new object labeling
Currently SELinux has rules which label new objects according to 3 criteria. The label of the process creating the object, the label of the parent directory, and the type of object (reg, dir, char, block, etc.) This patch adds a 4th criteria, the dentry name, thus we can distinguish between creating a file in an etc_t directory called shadow and one called motd. There is no file globbing, regex parsing, or anything mystical. Either the policy exactly (strcmp) matches the dentry name of the object or it doesn't. This patch has no changes from today if policy does not implement the new rules. Signed-off-by: Eric Paris <eparis@redhat.com>
1 parent 2a7dba3 commit 652bb9b

6 files changed

Lines changed: 197 additions & 37 deletions

File tree

‎security/selinux/hooks.c‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,10 +1301,8 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent
13011301

13021302
/* Try to obtain a transition SID. */
13031303
isec->sclass = inode_mode_to_security_class(inode->i_mode);
1304-
rc = security_transition_sid(isec->task_sid,
1305-
sbsec->sid,
1306-
isec->sclass,
1307-
&sid);
1304+
rc = security_transition_sid(isec->task_sid, sbsec->sid,
1305+
isec->sclass, NULL, &sid);
13081306
if (rc)
13091307
goto out_unlock;
13101308
isec->sid = sid;
@@ -1579,7 +1577,7 @@ static int may_create(struct inode *dir,
15791577
return rc;
15801578

15811579
if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
1582-
rc = security_transition_sid(sid, dsec->sid, tclass, &newsid);
1580+
rc = security_transition_sid(sid, dsec->sid, tclass, NULL, &newsid);
15831581
if (rc)
15841582
return rc;
15851583
}
@@ -2061,7 +2059,8 @@ static int selinux_bprm_set_creds(struct linux_binprm *bprm)
20612059
} else {
20622060
/* Check for a default transition on this program. */
20632061
rc = security_transition_sid(old_tsec->sid, isec->sid,
2064-
SECCLASS_PROCESS, &new_tsec->sid);
2062+
SECCLASS_PROCESS, NULL,
2063+
&new_tsec->sid);
20652064
if (rc)
20662065
return rc;
20672066
}
@@ -2532,7 +2531,7 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
25322531
else if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
25332532
rc = security_transition_sid(sid, dsec->sid,
25342533
inode_mode_to_security_class(inode->i_mode),
2535-
&newsid);
2534+
qstr, &newsid);
25362535
if (rc) {
25372536
printk(KERN_WARNING "%s: "
25382537
"security_transition_sid failed, rc=%d (dev=%s "
@@ -4845,7 +4844,7 @@ static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
48454844
* message queue this message will be stored in
48464845
*/
48474846
rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
4848-
&msec->sid);
4847+
NULL, &msec->sid);
48494848
if (rc)
48504849
return rc;
48514850
}

‎security/selinux/include/security.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#ifndef _SELINUX_SECURITY_H_
99
#define _SELINUX_SECURITY_H_
1010

11+
#include <linux/dcache.h>
1112
#include <linux/magic.h>
1213
#include <linux/types.h>
1314
#include "flask.h"
@@ -28,13 +29,14 @@
2829
#define POLICYDB_VERSION_POLCAP 22
2930
#define POLICYDB_VERSION_PERMISSIVE 23
3031
#define POLICYDB_VERSION_BOUNDARY 24
32+
#define POLICYDB_VERSION_FILENAME_TRANS 25
3133

3234
/* Range of policy versions we understand*/
3335
#define POLICYDB_VERSION_MIN POLICYDB_VERSION_BASE
3436
#ifdef CONFIG_SECURITY_SELINUX_POLICYDB_VERSION_MAX
3537
#define POLICYDB_VERSION_MAX CONFIG_SECURITY_SELINUX_POLICYDB_VERSION_MAX_VALUE
3638
#else
37-
#define POLICYDB_VERSION_MAX POLICYDB_VERSION_BOUNDARY
39+
#define POLICYDB_VERSION_MAX POLICYDB_VERSION_FILENAME_TRANS
3840
#endif
3941

4042
/* Mask for just the mount related flags */
@@ -106,8 +108,8 @@ void security_compute_av(u32 ssid, u32 tsid,
106108
void security_compute_av_user(u32 ssid, u32 tsid,
107109
u16 tclass, struct av_decision *avd);
108110

109-
int security_transition_sid(u32 ssid, u32 tsid,
110-
u16 tclass, u32 *out_sid);
111+
int security_transition_sid(u32 ssid, u32 tsid, u16 tclass,
112+
const struct qstr *qstr, u32 *out_sid);
111113

112114
int security_transition_sid_user(u32 ssid, u32 tsid,
113115
u16 tclass, u32 *out_sid);

‎security/selinux/ss/avtab.h‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
* Copyright (C) 2003 Tresys Technology, LLC
1616
* This program is free software; you can redistribute it and/or modify
17-
* it under the terms of the GNU General Public License as published by
17+
* it under the terms of the GNU General Public License as published by
1818
* the Free Software Foundation, version 2.
1919
*
2020
* Updated: Yuichi Nakamura <ynakam@hitachisoft.jp>
@@ -27,16 +27,16 @@ struct avtab_key {
2727
u16 source_type; /* source type */
2828
u16 target_type; /* target type */
2929
u16 target_class; /* target object class */
30-
#define AVTAB_ALLOWED 1
31-
#define AVTAB_AUDITALLOW 2
32-
#define AVTAB_AUDITDENY 4
33-
#define AVTAB_AV (AVTAB_ALLOWED | AVTAB_AUDITALLOW | AVTAB_AUDITDENY)
34-
#define AVTAB_TRANSITION 16
35-
#define AVTAB_MEMBER 32
36-
#define AVTAB_CHANGE 64
37-
#define AVTAB_TYPE (AVTAB_TRANSITION | AVTAB_MEMBER | AVTAB_CHANGE)
38-
#define AVTAB_ENABLED_OLD 0x80000000 /* reserved for used in cond_avtab */
39-
#define AVTAB_ENABLED 0x8000 /* reserved for used in cond_avtab */
30+
#define AVTAB_ALLOWED 0x0001
31+
#define AVTAB_AUDITALLOW 0x0002
32+
#define AVTAB_AUDITDENY 0x0004
33+
#define AVTAB_AV (AVTAB_ALLOWED | AVTAB_AUDITALLOW | AVTAB_AUDITDENY)
34+
#define AVTAB_TRANSITION 0x0010
35+
#define AVTAB_MEMBER 0x0020
36+
#define AVTAB_CHANGE 0x0040
37+
#define AVTAB_TYPE (AVTAB_TRANSITION | AVTAB_MEMBER | AVTAB_CHANGE)
38+
#define AVTAB_ENABLED_OLD 0x80000000 /* reserved for used in cond_avtab */
39+
#define AVTAB_ENABLED 0x8000 /* reserved for used in cond_avtab */
4040
u16 specified; /* what field is specified */
4141
};
4242

‎security/selinux/ss/policydb.c‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ static struct policydb_compat_info policydb_compat[] = {
123123
.sym_num = SYM_NUM,
124124
.ocon_num = OCON_NUM,
125125
},
126+
{
127+
.version = POLICYDB_VERSION_FILENAME_TRANS,
128+
.sym_num = SYM_NUM,
129+
.ocon_num = OCON_NUM,
130+
},
126131
};
127132

128133
static struct policydb_compat_info *policydb_lookup_compat(int version)
@@ -704,6 +709,7 @@ void policydb_destroy(struct policydb *p)
704709
int i;
705710
struct role_allow *ra, *lra = NULL;
706711
struct role_trans *tr, *ltr = NULL;
712+
struct filename_trans *ft, *nft;
707713

708714
for (i = 0; i < SYM_NUM; i++) {
709715
cond_resched();
@@ -781,6 +787,15 @@ void policydb_destroy(struct policydb *p)
781787
}
782788
flex_array_free(p->type_attr_map_array);
783789
}
790+
791+
ft = p->filename_trans;
792+
while (ft) {
793+
nft = ft->next;
794+
kfree(ft->name);
795+
kfree(ft);
796+
ft = nft;
797+
}
798+
784799
ebitmap_destroy(&p->policycaps);
785800
ebitmap_destroy(&p->permissive_map);
786801

@@ -1788,6 +1803,76 @@ static int range_read(struct policydb *p, void *fp)
17881803
return rc;
17891804
}
17901805

1806+
static int filename_trans_read(struct policydb *p, void *fp)
1807+
{
1808+
struct filename_trans *ft, *last;
1809+
u32 nel, len;
1810+
char *name;
1811+
__le32 buf[4];
1812+
int rc, i;
1813+
1814+
if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
1815+
return 0;
1816+
1817+
rc = next_entry(buf, fp, sizeof(u32));
1818+
if (rc)
1819+
goto out;
1820+
nel = le32_to_cpu(buf[0]);
1821+
1822+
printk(KERN_ERR "%s: nel=%d\n", __func__, nel);
1823+
1824+
last = p->filename_trans;
1825+
while (last && last->next)
1826+
last = last->next;
1827+
1828+
for (i = 0; i < nel; i++) {
1829+
rc = -ENOMEM;
1830+
ft = kzalloc(sizeof(*ft), GFP_KERNEL);
1831+
if (!ft)
1832+
goto out;
1833+
1834+
/* add it to the tail of the list */
1835+
if (!last)
1836+
p->filename_trans = ft;
1837+
else
1838+
last->next = ft;
1839+
last = ft;
1840+
1841+
/* length of the path component string */
1842+
rc = next_entry(buf, fp, sizeof(u32));
1843+
if (rc)
1844+
goto out;
1845+
len = le32_to_cpu(buf[0]);
1846+
1847+
rc = -ENOMEM;
1848+
name = kmalloc(len + 1, GFP_KERNEL);
1849+
if (!name)
1850+
goto out;
1851+
1852+
ft->name = name;
1853+
1854+
/* path component string */
1855+
rc = next_entry(name, fp, len);
1856+
if (rc)
1857+
goto out;
1858+
name[len] = 0;
1859+
1860+
printk(KERN_ERR "%s: ft=%p ft->name=%p ft->name=%s\n", __func__, ft, ft->name, ft->name);
1861+
1862+
rc = next_entry(buf, fp, sizeof(u32) * 4);
1863+
if (rc)
1864+
goto out;
1865+
1866+
ft->stype = le32_to_cpu(buf[0]);
1867+
ft->ttype = le32_to_cpu(buf[1]);
1868+
ft->tclass = le32_to_cpu(buf[2]);
1869+
ft->otype = le32_to_cpu(buf[3]);
1870+
}
1871+
rc = 0;
1872+
out:
1873+
return rc;
1874+
}
1875+
17911876
static int genfs_read(struct policydb *p, void *fp)
17921877
{
17931878
int i, j, rc;
@@ -2251,6 +2336,10 @@ int policydb_read(struct policydb *p, void *fp)
22512336
lra = ra;
22522337
}
22532338

2339+
rc = filename_trans_read(p, fp);
2340+
if (rc)
2341+
goto bad;
2342+
22542343
rc = policydb_index(p);
22552344
if (rc)
22562345
goto bad;
@@ -3025,6 +3114,43 @@ static int range_write(struct policydb *p, void *fp)
30253114
return 0;
30263115
}
30273116

3117+
static int filename_trans_write(struct policydb *p, void *fp)
3118+
{
3119+
struct filename_trans *ft;
3120+
u32 len, nel = 0;
3121+
__le32 buf[4];
3122+
int rc;
3123+
3124+
for (ft = p->filename_trans; ft; ft = ft->next)
3125+
nel++;
3126+
3127+
buf[0] = cpu_to_le32(nel);
3128+
rc = put_entry(buf, sizeof(u32), 1, fp);
3129+
if (rc)
3130+
return rc;
3131+
3132+
for (ft = p->filename_trans; ft; ft = ft->next) {
3133+
len = strlen(ft->name);
3134+
buf[0] = cpu_to_le32(len);
3135+
rc = put_entry(buf, sizeof(u32), 1, fp);
3136+
if (rc)
3137+
return rc;
3138+
3139+
rc = put_entry(ft->name, sizeof(char), len, fp);
3140+
if (rc)
3141+
return rc;
3142+
3143+
buf[0] = ft->stype;
3144+
buf[1] = ft->ttype;
3145+
buf[2] = ft->tclass;
3146+
buf[3] = ft->otype;
3147+
3148+
rc = put_entry(buf, sizeof(u32), 4, fp);
3149+
if (rc)
3150+
return rc;
3151+
}
3152+
return 0;
3153+
}
30283154
/*
30293155
* Write the configuration data in a policy database
30303156
* structure to a policy database binary representation
@@ -3135,6 +3261,10 @@ int policydb_write(struct policydb *p, void *fp)
31353261
if (rc)
31363262
return rc;
31373263

3264+
rc = filename_trans_write(p, fp);
3265+
if (rc)
3266+
return rc;
3267+
31383268
rc = ocontext_write(p, info, fp);
31393269
if (rc)
31403270
return rc;

‎security/selinux/ss/policydb.h‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ struct role_trans {
7777
struct role_trans *next;
7878
};
7979

80+
struct filename_trans {
81+
struct filename_trans *next;
82+
u32 stype; /* current process */
83+
u32 ttype; /* parent dir context */
84+
u16 tclass; /* class of new object */
85+
const char *name; /* last path component */
86+
u32 otype; /* expected of new object */
87+
};
88+
8089
struct role_allow {
8190
u32 role; /* current role */
8291
u32 new_role; /* new role */
@@ -217,6 +226,9 @@ struct policydb {
217226
/* role transitions */
218227
struct role_trans *role_tr;
219228

229+
/* file transitions with the last path component */
230+
struct filename_trans *filename_trans;
231+
220232
/* bools indexed by (value - 1) */
221233
struct cond_bool_datum **bool_val_to_struct;
222234
/* type enforcement conditional access vectors and transitions */
@@ -302,7 +314,7 @@ static inline int next_entry(void *buf, struct policy_file *fp, size_t bytes)
302314
return 0;
303315
}
304316

305-
static inline int put_entry(void *buf, size_t bytes, int num, struct policy_file *fp)
317+
static inline int put_entry(const void *buf, size_t bytes, int num, struct policy_file *fp)
306318
{
307319
size_t len = bytes * num;
308320

0 commit comments

Comments
 (0)