Skip to content

Commit abb0f43

Browse files
cgzonespcmoore
authored andcommitted
selinux: use u32 as bit position type in ebitmap code
The extensible bitmap supports bit positions up to U32_MAX due to the type of the member highbit being u32. Use u32 consistently as the type for bit positions to announce to callers what range of values is supported. Signed-off-by: Christian Göttsche <[email protected]> [PM: merge fuzz, subject line tweak] Signed-off-by: Paul Moore <[email protected]>
1 parent 32db469 commit abb0f43

File tree

2 files changed

+34
-35
lines changed

2 files changed

+34
-35
lines changed

security/selinux/ss/ebitmap.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "ebitmap.h"
2222
#include "policydb.h"
2323

24-
#define BITS_PER_U64 (sizeof(u64) * 8)
24+
#define BITS_PER_U64 ((u32)(sizeof(u64) * 8))
2525

2626
static struct kmem_cache *ebitmap_node_cachep __ro_after_init;
2727

@@ -79,7 +79,8 @@ int ebitmap_and(struct ebitmap *dst, const struct ebitmap *e1,
7979
const struct ebitmap *e2)
8080
{
8181
struct ebitmap_node *n;
82-
int bit, rc;
82+
u32 bit;
83+
int rc;
8384

8485
ebitmap_init(dst);
8586

@@ -256,7 +257,7 @@ int ebitmap_contains(const struct ebitmap *e1, const struct ebitmap *e2,
256257
return 1;
257258
}
258259

259-
int ebitmap_get_bit(const struct ebitmap *e, unsigned long bit)
260+
int ebitmap_get_bit(const struct ebitmap *e, u32 bit)
260261
{
261262
const struct ebitmap_node *n;
262263

@@ -273,7 +274,7 @@ int ebitmap_get_bit(const struct ebitmap *e, unsigned long bit)
273274
return 0;
274275
}
275276

276-
int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value)
277+
int ebitmap_set_bit(struct ebitmap *e, u32 bit, int value)
277278
{
278279
struct ebitmap_node *n, *prev, *new;
279280

@@ -284,7 +285,7 @@ int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value)
284285
if (value) {
285286
ebitmap_node_set_bit(n, bit);
286287
} else {
287-
unsigned int s;
288+
u32 s;
288289

289290
ebitmap_node_clr_bit(n, bit);
290291

@@ -362,12 +363,12 @@ void ebitmap_destroy(struct ebitmap *e)
362363
int ebitmap_read(struct ebitmap *e, void *fp)
363364
{
364365
struct ebitmap_node *n = NULL;
365-
u32 mapunit, count, startbit, index;
366+
u32 mapunit, count, startbit, index, i;
366367
__le32 ebitmap_start;
367368
u64 map;
368369
__le64 mapbits;
369370
__le32 buf[3];
370-
int rc, i;
371+
int rc;
371372

372373
ebitmap_init(e);
373374

@@ -381,7 +382,7 @@ int ebitmap_read(struct ebitmap *e, void *fp)
381382

382383
if (mapunit != BITS_PER_U64) {
383384
pr_err("SELinux: ebitmap: map size %u does not "
384-
"match my size %zd (high bit was %u)\n",
385+
"match my size %u (high bit was %u)\n",
385386
mapunit, BITS_PER_U64, e->highbit);
386387
goto bad;
387388
}
@@ -480,19 +481,20 @@ int ebitmap_read(struct ebitmap *e, void *fp)
480481
int ebitmap_write(const struct ebitmap *e, void *fp)
481482
{
482483
struct ebitmap_node *n;
483-
u32 count;
484+
u32 bit, count, last_bit, last_startbit;
484485
__le32 buf[3];
485486
u64 map;
486-
int bit, last_bit, last_startbit, rc;
487+
int rc;
487488

488489
buf[0] = cpu_to_le32(BITS_PER_U64);
489490

490491
count = 0;
491492
last_bit = 0;
492-
last_startbit = -1;
493+
last_startbit = U32_MAX;
493494
ebitmap_for_each_positive_bit(e, n, bit)
494495
{
495-
if (rounddown(bit, (int)BITS_PER_U64) > last_startbit) {
496+
if (last_startbit == U32_MAX ||
497+
rounddown(bit, BITS_PER_U64) > last_startbit) {
496498
count++;
497499
last_startbit = rounddown(bit, BITS_PER_U64);
498500
}
@@ -506,10 +508,11 @@ int ebitmap_write(const struct ebitmap *e, void *fp)
506508
return rc;
507509

508510
map = 0;
509-
last_startbit = INT_MIN;
511+
last_startbit = U32_MAX;
510512
ebitmap_for_each_positive_bit(e, n, bit)
511513
{
512-
if (rounddown(bit, (int)BITS_PER_U64) > last_startbit) {
514+
if (last_startbit == U32_MAX ||
515+
rounddown(bit, BITS_PER_U64) > last_startbit) {
513516
__le64 buf64[1];
514517

515518
/* this is the very first bit */

security/selinux/ss/ebitmap.h

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ struct ebitmap {
4646

4747
#define ebitmap_length(e) ((e)->highbit)
4848

49-
static inline unsigned int ebitmap_start_positive(const struct ebitmap *e,
50-
struct ebitmap_node **n)
49+
static inline u32 ebitmap_start_positive(const struct ebitmap *e,
50+
struct ebitmap_node **n)
5151
{
52-
unsigned int ofs;
52+
u32 ofs;
5353

5454
for (*n = e->node; *n; *n = (*n)->next) {
5555
ofs = find_first_bit((*n)->maps, EBITMAP_SIZE);
@@ -64,11 +64,10 @@ static inline void ebitmap_init(struct ebitmap *e)
6464
memset(e, 0, sizeof(*e));
6565
}
6666

67-
static inline unsigned int ebitmap_next_positive(const struct ebitmap *e,
68-
struct ebitmap_node **n,
69-
unsigned int bit)
67+
static inline u32 ebitmap_next_positive(const struct ebitmap *e,
68+
struct ebitmap_node **n, u32 bit)
7069
{
71-
unsigned int ofs;
70+
u32 ofs;
7271

7372
ofs = find_next_bit((*n)->maps, EBITMAP_SIZE, bit - (*n)->startbit + 1);
7473
if (ofs < EBITMAP_SIZE)
@@ -87,33 +86,30 @@ static inline unsigned int ebitmap_next_positive(const struct ebitmap *e,
8786
#define EBITMAP_NODE_OFFSET(node, bit) \
8887
(((bit) - (node)->startbit) % EBITMAP_UNIT_SIZE)
8988

90-
static inline int ebitmap_node_get_bit(const struct ebitmap_node *n,
91-
unsigned int bit)
89+
static inline int ebitmap_node_get_bit(const struct ebitmap_node *n, u32 bit)
9290
{
93-
unsigned int index = EBITMAP_NODE_INDEX(n, bit);
94-
unsigned int ofs = EBITMAP_NODE_OFFSET(n, bit);
91+
u32 index = EBITMAP_NODE_INDEX(n, bit);
92+
u32 ofs = EBITMAP_NODE_OFFSET(n, bit);
9593

9694
BUG_ON(index >= EBITMAP_UNIT_NUMS);
9795
if ((n->maps[index] & (EBITMAP_BIT << ofs)))
9896
return 1;
9997
return 0;
10098
}
10199

102-
static inline void ebitmap_node_set_bit(struct ebitmap_node *n,
103-
unsigned int bit)
100+
static inline void ebitmap_node_set_bit(struct ebitmap_node *n, u32 bit)
104101
{
105-
unsigned int index = EBITMAP_NODE_INDEX(n, bit);
106-
unsigned int ofs = EBITMAP_NODE_OFFSET(n, bit);
102+
u32 index = EBITMAP_NODE_INDEX(n, bit);
103+
u32 ofs = EBITMAP_NODE_OFFSET(n, bit);
107104

108105
BUG_ON(index >= EBITMAP_UNIT_NUMS);
109106
n->maps[index] |= (EBITMAP_BIT << ofs);
110107
}
111108

112-
static inline void ebitmap_node_clr_bit(struct ebitmap_node *n,
113-
unsigned int bit)
109+
static inline void ebitmap_node_clr_bit(struct ebitmap_node *n, u32 bit)
114110
{
115-
unsigned int index = EBITMAP_NODE_INDEX(n, bit);
116-
unsigned int ofs = EBITMAP_NODE_OFFSET(n, bit);
111+
u32 index = EBITMAP_NODE_INDEX(n, bit);
112+
u32 ofs = EBITMAP_NODE_OFFSET(n, bit);
117113

118114
BUG_ON(index >= EBITMAP_UNIT_NUMS);
119115
n->maps[index] &= ~(EBITMAP_BIT << ofs);
@@ -130,8 +126,8 @@ int ebitmap_and(struct ebitmap *dst, const struct ebitmap *e1,
130126
const struct ebitmap *e2);
131127
int ebitmap_contains(const struct ebitmap *e1, const struct ebitmap *e2,
132128
u32 last_e2bit);
133-
int ebitmap_get_bit(const struct ebitmap *e, unsigned long bit);
134-
int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value);
129+
int ebitmap_get_bit(const struct ebitmap *e, u32 bit);
130+
int ebitmap_set_bit(struct ebitmap *e, u32 bit, int value);
135131
void ebitmap_destroy(struct ebitmap *e);
136132
int ebitmap_read(struct ebitmap *e, void *fp);
137133
int ebitmap_write(const struct ebitmap *e, void *fp);

0 commit comments

Comments
 (0)