Skip to content

Commit 929126e

Browse files
Hongru Zhangpcmoore
authored andcommitted
selinux: Move avtab_hash() to a shared location for future reuse
This is a preparation patch, no functional change. Signed-off-by: Hongru Zhang <zhanghongru@xiaomi.com> Signed-off-by: Paul Moore <paul@paul-moore.com>
1 parent 641e021 commit 929126e

2 files changed

Lines changed: 47 additions & 40 deletions

File tree

security/selinux/include/hash.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
3+
#ifndef _SELINUX_HASH_H_
4+
#define _SELINUX_HASH_H_
5+
6+
/* Based on MurmurHash3, written by Austin Appleby and placed in the
7+
* public domain.
8+
*/
9+
static inline u32 avtab_hash(const struct avtab_key *keyp, u32 mask)
10+
{
11+
static const u32 c1 = 0xcc9e2d51;
12+
static const u32 c2 = 0x1b873593;
13+
static const u32 r1 = 15;
14+
static const u32 r2 = 13;
15+
static const u32 m = 5;
16+
static const u32 n = 0xe6546b64;
17+
18+
u32 hash = 0;
19+
20+
#define mix(input) \
21+
do { \
22+
u32 v = input; \
23+
v *= c1; \
24+
v = (v << r1) | (v >> (32 - r1)); \
25+
v *= c2; \
26+
hash ^= v; \
27+
hash = (hash << r2) | (hash >> (32 - r2)); \
28+
hash = hash * m + n; \
29+
} while (0)
30+
31+
mix(keyp->target_class);
32+
mix(keyp->target_type);
33+
mix(keyp->source_type);
34+
35+
#undef mix
36+
37+
hash ^= hash >> 16;
38+
hash *= 0x85ebca6b;
39+
hash ^= hash >> 13;
40+
hash *= 0xc2b2ae35;
41+
hash ^= hash >> 16;
42+
43+
return hash & mask;
44+
}
45+
46+
#endif /* _SELINUX_HASH_H_ */

security/selinux/ss/avtab.c

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,50 +20,11 @@
2020
#include <linux/errno.h>
2121
#include "avtab.h"
2222
#include "policydb.h"
23+
#include "hash.h"
2324

2425
static struct kmem_cache *avtab_node_cachep __ro_after_init;
2526
static struct kmem_cache *avtab_xperms_cachep __ro_after_init;
2627

27-
/* Based on MurmurHash3, written by Austin Appleby and placed in the
28-
* public domain.
29-
*/
30-
static inline u32 avtab_hash(const struct avtab_key *keyp, u32 mask)
31-
{
32-
static const u32 c1 = 0xcc9e2d51;
33-
static const u32 c2 = 0x1b873593;
34-
static const u32 r1 = 15;
35-
static const u32 r2 = 13;
36-
static const u32 m = 5;
37-
static const u32 n = 0xe6546b64;
38-
39-
u32 hash = 0;
40-
41-
#define mix(input) \
42-
do { \
43-
u32 v = input; \
44-
v *= c1; \
45-
v = (v << r1) | (v >> (32 - r1)); \
46-
v *= c2; \
47-
hash ^= v; \
48-
hash = (hash << r2) | (hash >> (32 - r2)); \
49-
hash = hash * m + n; \
50-
} while (0)
51-
52-
mix(keyp->target_class);
53-
mix(keyp->target_type);
54-
mix(keyp->source_type);
55-
56-
#undef mix
57-
58-
hash ^= hash >> 16;
59-
hash *= 0x85ebca6b;
60-
hash ^= hash >> 13;
61-
hash *= 0xc2b2ae35;
62-
hash ^= hash >> 16;
63-
64-
return hash & mask;
65-
}
66-
6728
static struct avtab_node *avtab_insert_node(struct avtab *h,
6829
struct avtab_node **dst,
6930
const struct avtab_key *key,

0 commit comments

Comments
 (0)