xref: /linux/security/selinux/ss/context.c (revision 0526b56cbc3c489642bd6a5fe4b718dea7ef0ee8)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Implementations of the security context functions.
4  *
5  * Author: Ondrej Mosnacek <omosnacek@gmail.com>
6  * Copyright (C) 2020 Red Hat, Inc.
7  */
8 
9 #include <linux/jhash.h>
10 
11 #include "context.h"
12 #include "mls.h"
13 
14 u32 context_compute_hash(const struct context *c)
15 {
16 	u32 hash = 0;
17 
18 	/*
19 	 * If a context is invalid, it will always be represented by a
20 	 * context struct with only the len & str set (and vice versa)
21 	 * under a given policy. Since context structs from different
22 	 * policies should never meet, it is safe to hash valid and
23 	 * invalid contexts differently. The context_cmp() function
24 	 * already operates under the same assumption.
25 	 */
26 	if (c->len)
27 		return full_name_hash(NULL, c->str, c->len);
28 
29 	hash = jhash_3words(c->user, c->role, c->type, hash);
30 	hash = mls_range_hash(&c->range, hash);
31 	return hash;
32 }
33