Skip to content

Commit 49e917d

Browse files
committed
Merge tag 'selinux-pr-20200803' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux
Pull selinux updates from Paul Moore: "Beyond the usual smattering of bug fixes, we've got three small improvements worth highlighting: - improved SELinux policy symbol table performance due to a reworking of the insert and search functions - allow reading of SELinux labels before the policy is loaded, allowing for some more "exotic" initramfs approaches - improved checking an error reporting about process class/permissions during SELinux policy load" * tag 'selinux-pr-20200803' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux: selinux: complete the inlining of hashtab functions selinux: prepare for inlining of hashtab functions selinux: specialize symtab insert and search functions selinux: Fix spelling mistakes in the comments selinux: fixed a checkpatch warning with the sizeof macro selinux: log error messages on required process class / permissions scripts/selinux/mdp: fix initial SID handling selinux: allow reading labels before policy is loaded
2 parents 9ecc6ea + 54b27f9 commit 49e917d

File tree

15 files changed

+258
-166
lines changed

15 files changed

+258
-166
lines changed

scripts/selinux/mdp/mdp.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,14 @@ int main(int argc, char *argv[])
6767

6868
initial_sid_to_string_len = sizeof(initial_sid_to_string) / sizeof (char *);
6969
/* print out the sids */
70-
for (i = 1; i < initial_sid_to_string_len; i++)
71-
fprintf(fout, "sid %s\n", initial_sid_to_string[i]);
70+
for (i = 1; i < initial_sid_to_string_len; i++) {
71+
const char *name = initial_sid_to_string[i];
72+
73+
if (name)
74+
fprintf(fout, "sid %s\n", name);
75+
else
76+
fprintf(fout, "sid unused%d\n", i);
77+
}
7278
fprintf(fout, "\n");
7379

7480
/* print out the class permissions */
@@ -126,9 +132,16 @@ int main(int argc, char *argv[])
126132
#define OBJUSERROLETYPE "user_u:object_r:base_t"
127133

128134
/* default sids */
129-
for (i = 1; i < initial_sid_to_string_len; i++)
130-
fprintf(fout, "sid %s " SUBJUSERROLETYPE "%s\n",
131-
initial_sid_to_string[i], mls ? ":" SYSTEMLOW : "");
135+
for (i = 1; i < initial_sid_to_string_len; i++) {
136+
const char *name = initial_sid_to_string[i];
137+
138+
if (name)
139+
fprintf(fout, "sid %s ", name);
140+
else
141+
fprintf(fout, "sid unused%d\n", i);
142+
fprintf(fout, SUBJUSERROLETYPE "%s\n",
143+
mls ? ":" SYSTEMLOW : "");
144+
}
132145
fprintf(fout, "\n");
133146

134147
#define FS_USE(behavior, fstype) \

security/selinux/hooks.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3332,7 +3332,12 @@ static int selinux_inode_getsecurity(struct inode *inode, const char *name, void
33323332
char *context = NULL;
33333333
struct inode_security_struct *isec;
33343334

3335-
if (strcmp(name, XATTR_SELINUX_SUFFIX))
3335+
/*
3336+
* If we're not initialized yet, then we can't validate contexts, so
3337+
* just let vfs_getxattr fall back to using the on-disk xattr.
3338+
*/
3339+
if (!selinux_initialized(&selinux_state) ||
3340+
strcmp(name, XATTR_SELINUX_SUFFIX))
33363341
return -EOPNOTSUPP;
33373342

33383343
/*

security/selinux/netif.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ static void sel_netif_destroy(struct sel_netif *netif)
124124
* @sid: interface SID
125125
*
126126
* Description:
127-
* This function determines the SID of a network interface by quering the
127+
* This function determines the SID of a network interface by querying the
128128
* security policy. The result is added to the network interface table to
129129
* speedup future queries. Returns zero on success, negative values on
130130
* failure.

security/selinux/netnode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ static void sel_netnode_insert(struct sel_netnode *node)
181181
* @sid: node SID
182182
*
183183
* Description:
184-
* This function determines the SID of a network address by quering the
184+
* This function determines the SID of a network address by querying the
185185
* security policy. The result is added to the network address table to
186186
* speedup future queries. Returns zero on success, negative values on
187187
* failure.

security/selinux/netport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ static void sel_netport_insert(struct sel_netport *port)
130130
* @sid: port SID
131131
*
132132
* Description:
133-
* This function determines the SID of a network port by quering the security
133+
* This function determines the SID of a network port by querying the security
134134
* policy. The result is added to the network port table to speedup future
135135
* queries. Returns zero on success, negative values on failure.
136136
*

security/selinux/ss/conditional.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ static int bool_isvalid(struct cond_bool_datum *b)
203203
return 1;
204204
}
205205

206-
int cond_read_bool(struct policydb *p, struct hashtab *h, void *fp)
206+
int cond_read_bool(struct policydb *p, struct symtab *s, void *fp)
207207
{
208208
char *key = NULL;
209209
struct cond_bool_datum *booldatum;
@@ -215,7 +215,7 @@ int cond_read_bool(struct policydb *p, struct hashtab *h, void *fp)
215215
if (!booldatum)
216216
return -ENOMEM;
217217

218-
rc = next_entry(buf, fp, sizeof buf);
218+
rc = next_entry(buf, fp, sizeof(buf));
219219
if (rc)
220220
goto err;
221221

@@ -238,7 +238,7 @@ int cond_read_bool(struct policydb *p, struct hashtab *h, void *fp)
238238
if (rc)
239239
goto err;
240240
key[len] = '\0';
241-
rc = hashtab_insert(h, key, booldatum);
241+
rc = symtab_insert(s, key, booldatum);
242242
if (rc)
243243
goto err;
244244

@@ -416,7 +416,7 @@ int cond_read_list(struct policydb *p, void *fp)
416416
u32 i, len;
417417
int rc;
418418

419-
rc = next_entry(buf, fp, sizeof buf);
419+
rc = next_entry(buf, fp, sizeof(buf));
420420
if (rc)
421421
return rc;
422422

security/selinux/ss/conditional.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ int cond_destroy_bool(void *key, void *datum, void *p);
6969

7070
int cond_index_bool(void *key, void *datum, void *datap);
7171

72-
int cond_read_bool(struct policydb *p, struct hashtab *h, void *fp);
72+
int cond_read_bool(struct policydb *p, struct symtab *s, void *fp);
7373
int cond_read_list(struct policydb *p, void *fp);
7474
int cond_write_bool(void *key, void *datum, void *ptr);
7575
int cond_write_list(struct policydb *p, void *fp);

security/selinux/ss/hashtab.c

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <linux/kernel.h>
88
#include <linux/slab.h>
99
#include <linux/errno.h>
10-
#include <linux/sched.h>
1110
#include "hashtab.h"
1211

1312
static struct kmem_cache *hashtab_node_cachep;
@@ -29,80 +28,34 @@ static u32 hashtab_compute_size(u32 nel)
2928
return nel == 0 ? 0 : roundup_pow_of_two(nel);
3029
}
3130

32-
int hashtab_init(struct hashtab *h,
33-
u32 (*hash_value)(struct hashtab *h, const void *key),
34-
int (*keycmp)(struct hashtab *h, const void *key1,
35-
const void *key2),
36-
u32 nel_hint)
31+
int hashtab_init(struct hashtab *h, u32 nel_hint)
3732
{
3833
h->size = hashtab_compute_size(nel_hint);
3934
h->nel = 0;
40-
h->hash_value = hash_value;
41-
h->keycmp = keycmp;
4235
if (!h->size)
4336
return 0;
4437

4538
h->htable = kcalloc(h->size, sizeof(*h->htable), GFP_KERNEL);
4639
return h->htable ? 0 : -ENOMEM;
4740
}
4841

49-
int hashtab_insert(struct hashtab *h, void *key, void *datum)
42+
int __hashtab_insert(struct hashtab *h, struct hashtab_node **dst,
43+
void *key, void *datum)
5044
{
51-
u32 hvalue;
52-
struct hashtab_node *prev, *cur, *newnode;
53-
54-
cond_resched();
55-
56-
if (!h->size || h->nel == HASHTAB_MAX_NODES)
57-
return -EINVAL;
58-
59-
hvalue = h->hash_value(h, key);
60-
prev = NULL;
61-
cur = h->htable[hvalue];
62-
while (cur && h->keycmp(h, key, cur->key) > 0) {
63-
prev = cur;
64-
cur = cur->next;
65-
}
66-
67-
if (cur && (h->keycmp(h, key, cur->key) == 0))
68-
return -EEXIST;
45+
struct hashtab_node *newnode;
6946

7047
newnode = kmem_cache_zalloc(hashtab_node_cachep, GFP_KERNEL);
7148
if (!newnode)
7249
return -ENOMEM;
7350
newnode->key = key;
7451
newnode->datum = datum;
75-
if (prev) {
76-
newnode->next = prev->next;
77-
prev->next = newnode;
78-
} else {
79-
newnode->next = h->htable[hvalue];
80-
h->htable[hvalue] = newnode;
81-
}
52+
newnode->next = *dst;
53+
*dst = newnode;
8254

8355
h->nel++;
8456
return 0;
8557
}
8658

87-
void *hashtab_search(struct hashtab *h, const void *key)
88-
{
89-
u32 hvalue;
90-
struct hashtab_node *cur;
91-
92-
if (!h->size)
93-
return NULL;
94-
95-
hvalue = h->hash_value(h, key);
96-
cur = h->htable[hvalue];
97-
while (cur && h->keycmp(h, key, cur->key) > 0)
98-
cur = cur->next;
99-
100-
if (!cur || (h->keycmp(h, key, cur->key) != 0))
101-
return NULL;
102-
103-
return cur->datum;
104-
}
105-
10659
void hashtab_destroy(struct hashtab *h)
10760
{
10861
u32 i;

security/selinux/ss/hashtab.h

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,17 @@
1111
#ifndef _SS_HASHTAB_H_
1212
#define _SS_HASHTAB_H_
1313

14-
#define HASHTAB_MAX_NODES 0xffffffff
14+
#include <linux/types.h>
15+
#include <linux/errno.h>
16+
#include <linux/sched.h>
17+
18+
#define HASHTAB_MAX_NODES U32_MAX
19+
20+
struct hashtab_key_params {
21+
u32 (*hash)(const void *key); /* hash function */
22+
int (*cmp)(const void *key1, const void *key2);
23+
/* key comparison function */
24+
};
1525

1626
struct hashtab_node {
1727
void *key;
@@ -23,10 +33,6 @@ struct hashtab {
2333
struct hashtab_node **htable; /* hash table */
2434
u32 size; /* number of slots in hash table */
2535
u32 nel; /* number of elements in hash table */
26-
u32 (*hash_value)(struct hashtab *h, const void *key);
27-
/* hash function */
28-
int (*keycmp)(struct hashtab *h, const void *key1, const void *key2);
29-
/* key comparison function */
3036
};
3137

3238
struct hashtab_info {
@@ -39,11 +45,10 @@ struct hashtab_info {
3945
*
4046
* Returns -ENOMEM if insufficient space is available or 0 otherwise.
4147
*/
42-
int hashtab_init(struct hashtab *h,
43-
u32 (*hash_value)(struct hashtab *h, const void *key),
44-
int (*keycmp)(struct hashtab *h, const void *key1,
45-
const void *key2),
46-
u32 nel_hint);
48+
int hashtab_init(struct hashtab *h, u32 nel_hint);
49+
50+
int __hashtab_insert(struct hashtab *h, struct hashtab_node **dst,
51+
void *key, void *datum);
4752

4853
/*
4954
* Inserts the specified (key, datum) pair into the specified hash table.
@@ -53,15 +58,63 @@ int hashtab_init(struct hashtab *h,
5358
* -EINVAL for general errors or
5459
0 otherwise.
5560
*/
56-
int hashtab_insert(struct hashtab *h, void *k, void *d);
61+
static inline int hashtab_insert(struct hashtab *h, void *key, void *datum,
62+
struct hashtab_key_params key_params)
63+
{
64+
u32 hvalue;
65+
struct hashtab_node *prev, *cur;
66+
67+
cond_resched();
68+
69+
if (!h->size || h->nel == HASHTAB_MAX_NODES)
70+
return -EINVAL;
71+
72+
hvalue = key_params.hash(key) & (h->size - 1);
73+
prev = NULL;
74+
cur = h->htable[hvalue];
75+
while (cur) {
76+
int cmp = key_params.cmp(key, cur->key);
77+
78+
if (cmp == 0)
79+
return -EEXIST;
80+
if (cmp < 0)
81+
break;
82+
prev = cur;
83+
cur = cur->next;
84+
}
85+
86+
return __hashtab_insert(h, prev ? &prev->next : &h->htable[hvalue],
87+
key, datum);
88+
}
5789

5890
/*
5991
* Searches for the entry with the specified key in the hash table.
6092
*
6193
* Returns NULL if no entry has the specified key or
6294
* the datum of the entry otherwise.
6395
*/
64-
void *hashtab_search(struct hashtab *h, const void *k);
96+
static inline void *hashtab_search(struct hashtab *h, const void *key,
97+
struct hashtab_key_params key_params)
98+
{
99+
u32 hvalue;
100+
struct hashtab_node *cur;
101+
102+
if (!h->size)
103+
return NULL;
104+
105+
hvalue = key_params.hash(key) & (h->size - 1);
106+
cur = h->htable[hvalue];
107+
while (cur) {
108+
int cmp = key_params.cmp(key, cur->key);
109+
110+
if (cmp == 0)
111+
return cur->datum;
112+
if (cmp < 0)
113+
break;
114+
cur = cur->next;
115+
}
116+
return NULL;
117+
}
65118

66119
/*
67120
* Destroys the specified hash table.

0 commit comments

Comments
 (0)