Skip to content

Commit 3bb56b2

Browse files
pcmoorexjamesmorris
authored andcommitted
SELinux: Add a capabilities bitmap to SELinux policy version 22
Add a new policy capabilities bitmap to SELinux policy version 22. This bitmap will enable the security server to query the policy to determine which features it supports. Signed-off-by: Paul Moore <paul.moore@hp.com> Signed-off-by: James Morris <jmorris@namei.org>
1 parent 224dfbd commit 3bb56b2

6 files changed

Lines changed: 185 additions & 8 deletions

File tree

‎security/selinux/Kconfig‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ config SECURITY_SELINUX_POLICYDB_VERSION_MAX
145145
config SECURITY_SELINUX_POLICYDB_VERSION_MAX_VALUE
146146
int "NSA SELinux maximum supported policy format version value"
147147
depends on SECURITY_SELINUX_POLICYDB_VERSION_MAX
148-
range 15 21
148+
range 15 22
149149
default 19
150150
help
151151
This option sets the value for the maximum policy format version

‎security/selinux/include/security.h‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,34 @@
2525
#define POLICYDB_VERSION_MLS 19
2626
#define POLICYDB_VERSION_AVTAB 20
2727
#define POLICYDB_VERSION_RANGETRANS 21
28+
#define POLICYDB_VERSION_POLCAP 22
2829

2930
/* Range of policy versions we understand*/
3031
#define POLICYDB_VERSION_MIN POLICYDB_VERSION_BASE
3132
#ifdef CONFIG_SECURITY_SELINUX_POLICYDB_VERSION_MAX
3233
#define POLICYDB_VERSION_MAX CONFIG_SECURITY_SELINUX_POLICYDB_VERSION_MAX_VALUE
3334
#else
34-
#define POLICYDB_VERSION_MAX POLICYDB_VERSION_RANGETRANS
35+
#define POLICYDB_VERSION_MAX POLICYDB_VERSION_POLCAP
3536
#endif
3637

3738
struct netlbl_lsm_secattr;
3839

3940
extern int selinux_enabled;
4041
extern int selinux_mls_enabled;
4142

43+
/* Policy capabilities */
44+
enum {
45+
POLICYDB_CAPABILITY_NETPEER,
46+
__POLICYDB_CAPABILITY_MAX
47+
};
48+
#define POLICYDB_CAPABILITY_MAX (__POLICYDB_CAPABILITY_MAX - 1)
49+
50+
extern int selinux_policycap_netpeer;
51+
4252
int security_load_policy(void * data, size_t len);
4353

54+
int security_policycap_supported(unsigned int req_cap);
55+
4456
#define SEL_VEC_MAX 32
4557
struct av_decision {
4658
u32 allowed;
@@ -91,6 +103,7 @@ int security_get_classes(char ***classes, int *nclasses);
91103
int security_get_permissions(char *class, char ***perms, int *nperms);
92104
int security_get_reject_unknown(void);
93105
int security_get_allow_unknown(void);
106+
int security_get_policycaps(int *len, int **values);
94107

95108
#define SECURITY_FS_USE_XATTR 1 /* use xattr */
96109
#define SECURITY_FS_USE_TRANS 2 /* use transition SIDs, e.g. devpts/tmpfs */

‎security/selinux/selinuxfs.c‎

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
*
33
* Added conditional policy language extensions
44
*
5+
* Updated: Hewlett-Packard <paul.moore@hp.com>
6+
*
7+
* Added support for the policy capability bitmap
8+
*
9+
* Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
510
* Copyright (C) 2003 - 2004 Tresys Technology, LLC
611
* Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
712
* This program is free software; you can redistribute it and/or modify
@@ -35,6 +40,11 @@
3540
#include "objsec.h"
3641
#include "conditional.h"
3742

43+
/* Policy capability filenames */
44+
static char *policycap_names[] = {
45+
"network_peer_controls"
46+
};
47+
3848
unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
3949

4050
#ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT
@@ -72,6 +82,9 @@ static int *bool_pending_values = NULL;
7282
static struct dentry *class_dir = NULL;
7383
static unsigned long last_class_ino;
7484

85+
/* global data for policy capabilities */
86+
static struct dentry *policycap_dir = NULL;
87+
7588
extern void selnl_notify_setenforce(int val);
7689

7790
/* Check whether a task is allowed to use a security operation. */
@@ -111,10 +124,11 @@ enum sel_inos {
111124

112125
static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
113126

114-
#define SEL_INITCON_INO_OFFSET 0x01000000
115-
#define SEL_BOOL_INO_OFFSET 0x02000000
116-
#define SEL_CLASS_INO_OFFSET 0x04000000
117-
#define SEL_INO_MASK 0x00ffffff
127+
#define SEL_INITCON_INO_OFFSET 0x01000000
128+
#define SEL_BOOL_INO_OFFSET 0x02000000
129+
#define SEL_CLASS_INO_OFFSET 0x04000000
130+
#define SEL_POLICYCAP_INO_OFFSET 0x08000000
131+
#define SEL_INO_MASK 0x00ffffff
118132

119133
#define TMPBUFLEN 12
120134
static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
@@ -263,6 +277,7 @@ static const struct file_operations sel_policyvers_ops = {
263277
/* declaration for sel_write_load */
264278
static int sel_make_bools(void);
265279
static int sel_make_classes(void);
280+
static int sel_make_policycap(void);
266281

267282
/* declaration for sel_make_class_dirs */
268283
static int sel_make_dir(struct inode *dir, struct dentry *dentry,
@@ -323,6 +338,12 @@ static ssize_t sel_write_load(struct file * file, const char __user * buf,
323338
}
324339

325340
ret = sel_make_classes();
341+
if (ret) {
342+
length = ret;
343+
goto out1;
344+
}
345+
346+
ret = sel_make_policycap();
326347
if (ret)
327348
length = ret;
328349
else
@@ -1399,6 +1420,24 @@ static const struct file_operations sel_perm_ops = {
13991420
.read = sel_read_perm,
14001421
};
14011422

1423+
static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1424+
size_t count, loff_t *ppos)
1425+
{
1426+
int value;
1427+
char tmpbuf[TMPBUFLEN];
1428+
ssize_t length;
1429+
unsigned long i_ino = file->f_path.dentry->d_inode->i_ino;
1430+
1431+
value = security_policycap_supported(i_ino & SEL_INO_MASK);
1432+
length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1433+
1434+
return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1435+
}
1436+
1437+
static const struct file_operations sel_policycap_ops = {
1438+
.read = sel_read_policycap,
1439+
};
1440+
14021441
static int sel_make_perm_files(char *objclass, int classvalue,
14031442
struct dentry *dir)
14041443
{
@@ -1545,6 +1584,36 @@ static int sel_make_classes(void)
15451584
return rc;
15461585
}
15471586

1587+
static int sel_make_policycap(void)
1588+
{
1589+
unsigned int iter;
1590+
struct dentry *dentry = NULL;
1591+
struct inode *inode = NULL;
1592+
1593+
sel_remove_entries(policycap_dir);
1594+
1595+
for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1596+
if (iter < ARRAY_SIZE(policycap_names))
1597+
dentry = d_alloc_name(policycap_dir,
1598+
policycap_names[iter]);
1599+
else
1600+
dentry = d_alloc_name(policycap_dir, "unknown");
1601+
1602+
if (dentry == NULL)
1603+
return -ENOMEM;
1604+
1605+
inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
1606+
if (inode == NULL)
1607+
return -ENOMEM;
1608+
1609+
inode->i_fop = &sel_policycap_ops;
1610+
inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1611+
d_add(dentry, inode);
1612+
}
1613+
1614+
return 0;
1615+
}
1616+
15481617
static int sel_make_dir(struct inode *dir, struct dentry *dentry,
15491618
unsigned long *ino)
15501619
{
@@ -1673,6 +1742,18 @@ static int sel_fill_super(struct super_block * sb, void * data, int silent)
16731742

16741743
class_dir = dentry;
16751744

1745+
dentry = d_alloc_name(sb->s_root, "policy_capabilities");
1746+
if (!dentry) {
1747+
ret = -ENOMEM;
1748+
goto err;
1749+
}
1750+
1751+
ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1752+
if (ret)
1753+
goto err;
1754+
1755+
policycap_dir = dentry;
1756+
16761757
out:
16771758
return ret;
16781759
err:

‎security/selinux/ss/policydb.c‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
*
1414
* Added conditional policy language extensions
1515
*
16+
* Updated: Hewlett-Packard <paul.moore@hp.com>
17+
*
18+
* Added support for the policy capability bitmap
19+
*
20+
* Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
1621
* Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
1722
* Copyright (C) 2003 - 2004 Tresys Technology, LLC
1823
* This program is free software; you can redistribute it and/or modify
@@ -102,6 +107,11 @@ static struct policydb_compat_info policydb_compat[] = {
102107
.sym_num = SYM_NUM,
103108
.ocon_num = OCON_NUM,
104109
},
110+
{
111+
.version = POLICYDB_VERSION_POLCAP,
112+
.sym_num = SYM_NUM,
113+
.ocon_num = OCON_NUM,
114+
}
105115
};
106116

107117
static struct policydb_compat_info *policydb_lookup_compat(int version)
@@ -183,6 +193,8 @@ static int policydb_init(struct policydb *p)
183193
if (rc)
184194
goto out_free_symtab;
185195

196+
ebitmap_init(&p->policycaps);
197+
186198
out:
187199
return rc;
188200

@@ -673,8 +685,8 @@ void policydb_destroy(struct policydb *p)
673685
ebitmap_destroy(&p->type_attr_map[i]);
674686
}
675687
kfree(p->type_attr_map);
676-
677688
kfree(p->undefined_perms);
689+
ebitmap_destroy(&p->policycaps);
678690

679691
return;
680692
}
@@ -1554,6 +1566,10 @@ int policydb_read(struct policydb *p, void *fp)
15541566
p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
15551567
p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
15561568

1569+
if (p->policyvers >= POLICYDB_VERSION_POLCAP &&
1570+
ebitmap_read(&p->policycaps, fp) != 0)
1571+
goto bad;
1572+
15571573
info = policydb_lookup_compat(p->policyvers);
15581574
if (!info) {
15591575
printk(KERN_ERR "security: unable to find policy compat info "

‎security/selinux/ss/policydb.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ struct policydb {
241241
/* type -> attribute reverse mapping */
242242
struct ebitmap *type_attr_map;
243243

244+
struct ebitmap policycaps;
245+
244246
unsigned int policyvers;
245247

246248
unsigned int reject_unknown : 1;

‎security/selinux/ss/services.c‎

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
* Updated: Hewlett-Packard <paul.moore@hp.com>
1717
*
1818
* Added support for NetLabel
19+
* Added support for the policy capability bitmap
1920
*
2021
* Updated: Chad Sellers <csellers@tresys.com>
2122
*
2223
* Added validation of kernel classes and permissions
2324
*
24-
* Copyright (C) 2006 Hewlett-Packard Development Company, L.P.
25+
* Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
2526
* Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
2627
* Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
2728
* Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
@@ -59,6 +60,8 @@
5960
extern void selnl_notify_policyload(u32 seqno);
6061
unsigned int policydb_loaded_version;
6162

63+
int selinux_policycap_netpeer;
64+
6265
/*
6366
* This is declared in avc.c
6467
*/
@@ -1299,6 +1302,12 @@ static int convert_context(u32 key,
12991302
goto out;
13001303
}
13011304

1305+
static void security_load_policycaps(void)
1306+
{
1307+
selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
1308+
POLICYDB_CAPABILITY_NETPEER);
1309+
}
1310+
13021311
extern void selinux_complete_init(void);
13031312
static int security_preserve_bools(struct policydb *p);
13041313

@@ -1346,6 +1355,7 @@ int security_load_policy(void *data, size_t len)
13461355
avtab_cache_destroy();
13471356
return -EINVAL;
13481357
}
1358+
security_load_policycaps();
13491359
policydb_loaded_version = policydb.policyvers;
13501360
ss_initialized = 1;
13511361
seqno = ++latest_granting;
@@ -1404,6 +1414,7 @@ int security_load_policy(void *data, size_t len)
14041414
POLICY_WRLOCK;
14051415
memcpy(&policydb, &newpolicydb, sizeof policydb);
14061416
sidtab_set(&sidtab, &newsidtab);
1417+
security_load_policycaps();
14071418
seqno = ++latest_granting;
14081419
policydb_loaded_version = policydb.policyvers;
14091420
POLICY_WRUNLOCK;
@@ -2148,6 +2159,60 @@ int security_get_allow_unknown(void)
21482159
return policydb.allow_unknown;
21492160
}
21502161

2162+
/**
2163+
* security_get_policycaps - Query the loaded policy for its capabilities
2164+
* @len: the number of capability bits
2165+
* @values: the capability bit array
2166+
*
2167+
* Description:
2168+
* Get an array of the policy capabilities in @values where each entry in
2169+
* @values is either true (1) or false (0) depending the policy's support of
2170+
* that feature. The policy capabilities are defined by the
2171+
* POLICYDB_CAPABILITY_* enums. The size of the array is stored in @len and it
2172+
* is up to the caller to free the array in @values. Returns zero on success,
2173+
* negative values on failure.
2174+
*
2175+
*/
2176+
int security_get_policycaps(int *len, int **values)
2177+
{
2178+
int rc = -ENOMEM;
2179+
unsigned int iter;
2180+
2181+
POLICY_RDLOCK;
2182+
2183+
*values = kcalloc(POLICYDB_CAPABILITY_MAX, sizeof(int), GFP_ATOMIC);
2184+
if (*values == NULL)
2185+
goto out;
2186+
for (iter = 0; iter < POLICYDB_CAPABILITY_MAX; iter++)
2187+
(*values)[iter] = ebitmap_get_bit(&policydb.policycaps, iter);
2188+
*len = POLICYDB_CAPABILITY_MAX;
2189+
2190+
out:
2191+
POLICY_RDUNLOCK;
2192+
return rc;
2193+
}
2194+
2195+
/**
2196+
* security_policycap_supported - Check for a specific policy capability
2197+
* @req_cap: capability
2198+
*
2199+
* Description:
2200+
* This function queries the currently loaded policy to see if it supports the
2201+
* capability specified by @req_cap. Returns true (1) if the capability is
2202+
* supported, false (0) if it isn't supported.
2203+
*
2204+
*/
2205+
int security_policycap_supported(unsigned int req_cap)
2206+
{
2207+
int rc;
2208+
2209+
POLICY_RDLOCK;
2210+
rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
2211+
POLICY_RDUNLOCK;
2212+
2213+
return rc;
2214+
}
2215+
21512216
struct selinux_audit_rule {
21522217
u32 au_seqno;
21532218
struct context au_ctxt;

0 commit comments

Comments
 (0)