summaryrefstree
path: root/kernel/security/proca/proca_table.c
blob: f05c1f830e9b98dfe6eb969c83fa67837933a26c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * PROCA task descriptors table
 *
 * Copyright (C) 2018 Samsung Electronics, Inc.
 * Hryhorii Tur, <hryhorii.tur@partner.samsung.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include "proca_table.h"

#include <linux/hashtable.h>
#include <linux/string.h>

void proca_table_init(struct proca_table *table)
{
	memset(table, 0, sizeof(*table));

	spin_lock_init(&table->pid_map_lock);
	hash_init(table->pid_map);

	spin_lock_init(&table->app_name_map_lock);
	hash_init(table->app_name_map);

	table->hash_tables_shift = PROCA_TASKS_TABLE_SHIFT;
}

/*
 * Following hash functions and constants were taken from 4.9.59 kernel
 * in order to simplify porting to new devices.
 */

#define GOLDEN_RATIO_32 0x61C88647

static inline u32 proca_hash_32(u32 val)
{
	return val * GOLDEN_RATIO_32;
}

/* Hash courtesy of the R5 hash in reiserfs modulo sign bits */
#define proca_init_name_hash(salt)		(unsigned long)(salt)

/* partial hash update function. Assume roughly 4 bits per character */
static inline unsigned long
proca_partial_name_hash(unsigned long c, unsigned long prevhash)
{
	return (prevhash + (c << 4) + (c >> 4)) * 11;
}

/*
 * Finally: cut down the number of bits to a int value (and try to avoid
 * losing bits).  This also has the property (wanted by the dcache)
 * that the msbits make a good hash table index.
 */
static inline unsigned long proca_end_name_hash(unsigned long hash)
{
	return proca_hash_32((unsigned int)hash);
}

static unsigned long calculate_app_name_hash(struct proca_table *table,
					     const char *app_name,
					     size_t app_name_size)
{
	size_t i;
	unsigned long hash = proca_init_name_hash(0);

	if (!app_name)
		return proca_end_name_hash(hash);

	for (i = 0; i < app_name_size; ++i)
		hash = proca_partial_name_hash(app_name[i], hash);
	return proca_end_name_hash(hash) % (1 << table->hash_tables_shift);
}

static unsigned long calculate_pid_hash(struct proca_table *table, pid_t pid)
{
	return proca_hash_32(pid) >> (32 - table->hash_tables_shift);
}

void proca_table_add_task_descr(struct proca_table *table,
				struct proca_task_descr *descr)
{
	unsigned long hash_key;
	unsigned long irqsave_flags;

	hash_key = calculate_pid_hash(table, descr->task->pid);
	spin_lock_irqsave(&table->pid_map_lock, irqsave_flags);
	hlist_add_head(&descr->pid_map_node,
		       &table->pid_map[hash_key]);
	spin_unlock_irqrestore(&table->pid_map_lock, irqsave_flags);

	if (descr->proca_identity.certificate) {
		hash_key = calculate_app_name_hash(table,
			descr->proca_identity.parsed_cert.app_name,
			descr->proca_identity.parsed_cert.app_name_size);
		spin_lock_irqsave(&table->app_name_map_lock, irqsave_flags);
		hlist_add_head(&descr->app_name_map_node,
			&table->app_name_map[hash_key]);
		spin_unlock_irqrestore(
			&table->app_name_map_lock, irqsave_flags);
	}
}

void proca_table_remove_task_descr(struct proca_table *table,
				struct proca_task_descr *descr)
{
	unsigned long irqsave_flags;

	if (!descr)
		return;

	spin_lock_irqsave(&table->pid_map_lock, irqsave_flags);
	hash_del(&descr->pid_map_node);
	spin_unlock_irqrestore(&table->pid_map_lock, irqsave_flags);

	spin_lock_irqsave(&table->app_name_map_lock, irqsave_flags);
	hash_del(&descr->app_name_map_node);
	spin_unlock_irqrestore(&table->app_name_map_lock, irqsave_flags);
}

struct proca_task_descr *proca_table_get_by_task(
					struct proca_table *table,
					const struct task_struct *task)
{
	struct proca_task_descr *descr;
	struct proca_task_descr *target_task_descr = NULL;
	unsigned long hash_key;
	unsigned long irqsave_flags;

	hash_key = calculate_pid_hash(table, task->pid);

	spin_lock_irqsave(&table->pid_map_lock, irqsave_flags);
	hlist_for_each_entry(descr, &table->pid_map[hash_key], pid_map_node) {
		if (task == descr->task) {
			target_task_descr = descr;
			break;
		}
	}
	spin_unlock_irqrestore(&table->pid_map_lock, irqsave_flags);

	return target_task_descr;
}

struct proca_task_descr *proca_table_remove_by_task(
				struct proca_table *table,
				const struct task_struct *task)
{
	struct proca_task_descr *target_task_descr = NULL;

	target_task_descr = proca_table_get_by_task(table, task);
	proca_table_remove_task_descr(table, target_task_descr);

	return target_task_descr;
}