Blame view
net/ax25/ax25_uid.c
4.32 KB
2874c5fd2
|
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4c
|
2 |
/* |
1da177e4c
|
3 4 5 |
* * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk) */ |
4fc268d24
|
6 7 |
#include <linux/capability.h> |
1da177e4c
|
8 9 10 11 12 |
#include <linux/errno.h> #include <linux/types.h> #include <linux/socket.h> #include <linux/in.h> #include <linux/kernel.h> |
1da177e4c
|
13 14 15 16 17 |
#include <linux/timer.h> #include <linux/string.h> #include <linux/sockios.h> #include <linux/net.h> #include <linux/spinlock.h> |
5a0e3ad6a
|
18 |
#include <linux/slab.h> |
1da177e4c
|
19 20 21 22 23 24 |
#include <net/ax25.h> #include <linux/inet.h> #include <linux/netdevice.h> #include <linux/if_arp.h> #include <linux/skbuff.h> #include <net/sock.h> |
7c0f6ba68
|
25 |
#include <linux/uaccess.h> |
1da177e4c
|
26 27 28 |
#include <linux/fcntl.h> #include <linux/mm.h> #include <linux/interrupt.h> |
01d7dd0e9
|
29 |
#include <linux/list.h> |
1da177e4c
|
30 31 32 33 |
#include <linux/notifier.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> #include <linux/stat.h> |
1da177e4c
|
34 |
#include <linux/sysctl.h> |
bc3b2d7fb
|
35 |
#include <linux/export.h> |
1da177e4c
|
36 37 38 39 40 41 |
#include <net/ip.h> #include <net/arp.h> /* * Callsign/UID mapper. This is in kernel space for security on multi-amateur machines. */ |
f16f3026d
|
42 |
static HLIST_HEAD(ax25_uid_list); |
1da177e4c
|
43 |
static DEFINE_RWLOCK(ax25_uid_lock); |
f16f3026d
|
44 |
int ax25_uid_policy; |
1da177e4c
|
45 |
|
70868eace
|
46 |
EXPORT_SYMBOL(ax25_uid_policy); |
d13fda856
|
47 |
ax25_uid_assoc *ax25_findbyuid(kuid_t uid) |
1da177e4c
|
48 |
{ |
01d7dd0e9
|
49 |
ax25_uid_assoc *ax25_uid, *res = NULL; |
1da177e4c
|
50 51 |
read_lock(&ax25_uid_lock); |
b67bfe0d4
|
52 |
ax25_uid_for_each(ax25_uid, &ax25_uid_list) { |
d13fda856
|
53 |
if (uid_eq(ax25_uid->uid, uid)) { |
01d7dd0e9
|
54 55 |
ax25_uid_hold(ax25_uid); res = ax25_uid; |
1da177e4c
|
56 57 58 59 |
break; } } read_unlock(&ax25_uid_lock); |
01d7dd0e9
|
60 |
return res; |
1da177e4c
|
61 |
} |
70868eace
|
62 |
EXPORT_SYMBOL(ax25_findbyuid); |
1da177e4c
|
63 64 |
int ax25_uid_ioctl(int cmd, struct sockaddr_ax25 *sax) { |
01d7dd0e9
|
65 |
ax25_uid_assoc *ax25_uid; |
01d7dd0e9
|
66 |
ax25_uid_assoc *user; |
1da177e4c
|
67 68 69 70 71 72 |
unsigned long res; switch (cmd) { case SIOCAX25GETUID: res = -ENOENT; read_lock(&ax25_uid_lock); |
b67bfe0d4
|
73 |
ax25_uid_for_each(ax25_uid, &ax25_uid_list) { |
1da177e4c
|
74 |
if (ax25cmp(&sax->sax25_call, &ax25_uid->call) == 0) { |
d13fda856
|
75 |
res = from_kuid_munged(current_user_ns(), ax25_uid->uid); |
1da177e4c
|
76 77 78 79 80 81 82 83 |
break; } } read_unlock(&ax25_uid_lock); return res; case SIOCAX25ADDUID: |
d13fda856
|
84 85 |
{ kuid_t sax25_kuid; |
1da177e4c
|
86 87 |
if (!capable(CAP_NET_ADMIN)) return -EPERM; |
d13fda856
|
88 89 90 91 |
sax25_kuid = make_kuid(current_user_ns(), sax->sax25_uid); if (!uid_valid(sax25_kuid)) return -EINVAL; user = ax25_findbyuid(sax25_kuid); |
01d7dd0e9
|
92 93 |
if (user) { ax25_uid_put(user); |
1da177e4c
|
94 |
return -EEXIST; |
01d7dd0e9
|
95 |
} |
1da177e4c
|
96 97 98 99 |
if (sax->sax25_uid == 0) return -EINVAL; if ((ax25_uid = kmalloc(sizeof(*ax25_uid), GFP_KERNEL)) == NULL) return -ENOMEM; |
07f2282fc
|
100 |
refcount_set(&ax25_uid->refcount, 1); |
d13fda856
|
101 |
ax25_uid->uid = sax25_kuid; |
1da177e4c
|
102 103 104 |
ax25_uid->call = sax->sax25_call; write_lock(&ax25_uid_lock); |
01d7dd0e9
|
105 |
hlist_add_head(&ax25_uid->uid_node, &ax25_uid_list); |
1da177e4c
|
106 107 108 |
write_unlock(&ax25_uid_lock); return 0; |
d13fda856
|
109 |
} |
1da177e4c
|
110 111 112 |
case SIOCAX25DELUID: if (!capable(CAP_NET_ADMIN)) return -EPERM; |
01d7dd0e9
|
113 |
ax25_uid = NULL; |
1da177e4c
|
114 |
write_lock(&ax25_uid_lock); |
b67bfe0d4
|
115 |
ax25_uid_for_each(ax25_uid, &ax25_uid_list) { |
01d7dd0e9
|
116 |
if (ax25cmp(&sax->sax25_call, &ax25_uid->call) == 0) |
1da177e4c
|
117 |
break; |
1da177e4c
|
118 119 120 121 122 |
} if (ax25_uid == NULL) { write_unlock(&ax25_uid_lock); return -ENOENT; } |
01d7dd0e9
|
123 124 |
hlist_del_init(&ax25_uid->uid_node); ax25_uid_put(ax25_uid); |
1da177e4c
|
125 |
write_unlock(&ax25_uid_lock); |
01d7dd0e9
|
126 |
return 0; |
1da177e4c
|
127 128 129 130 131 132 133 134 135 136 137 |
default: return -EINVAL; } return -EINVAL; /*NOTREACHED */ } #ifdef CONFIG_PROC_FS static void *ax25_uid_seq_start(struct seq_file *seq, loff_t *pos) |
f16f3026d
|
138 |
__acquires(ax25_uid_lock) |
1da177e4c
|
139 |
{ |
1da177e4c
|
140 |
read_lock(&ax25_uid_lock); |
b512f3d84
|
141 |
return seq_hlist_start_head(&ax25_uid_list, *pos); |
1da177e4c
|
142 143 144 145 |
} static void *ax25_uid_seq_next(struct seq_file *seq, void *v, loff_t *pos) { |
b512f3d84
|
146 |
return seq_hlist_next(v, &ax25_uid_list, pos); |
1da177e4c
|
147 148 149 |
} static void ax25_uid_seq_stop(struct seq_file *seq, void *v) |
f16f3026d
|
150 |
__releases(ax25_uid_lock) |
1da177e4c
|
151 152 153 154 155 156 |
{ read_unlock(&ax25_uid_lock); } static int ax25_uid_seq_show(struct seq_file *seq, void *v) { |
f75268cd6
|
157 |
char buf[11]; |
1da177e4c
|
158 159 160 161 |
if (v == SEQ_START_TOKEN) seq_printf(seq, "Policy: %d ", ax25_uid_policy); else { |
b512f3d84
|
162 |
struct ax25_uid_assoc *pt; |
1da177e4c
|
163 |
|
b512f3d84
|
164 |
pt = hlist_entry(v, struct ax25_uid_assoc, uid_node); |
d13fda856
|
165 166 167 168 |
seq_printf(seq, "%6d %s ", from_kuid_munged(seq_user_ns(seq), pt->uid), ax2asc(buf, &pt->call)); |
1da177e4c
|
169 170 171 |
} return 0; } |
fddda2b7b
|
172 |
const struct seq_operations ax25_uid_seqops = { |
1da177e4c
|
173 174 175 176 177 |
.start = ax25_uid_seq_start, .next = ax25_uid_seq_next, .stop = ax25_uid_seq_stop, .show = ax25_uid_seq_show, }; |
1da177e4c
|
178 179 180 181 182 183 184 |
#endif /* * Free all memory associated with UID/Callsign structures. */ void __exit ax25_uid_free(void) { |
01d7dd0e9
|
185 |
ax25_uid_assoc *ax25_uid; |
1da177e4c
|
186 187 |
write_lock(&ax25_uid_lock); |
ae1b6a31b
|
188 |
again: |
b67bfe0d4
|
189 |
ax25_uid_for_each(ax25_uid, &ax25_uid_list) { |
01d7dd0e9
|
190 191 |
hlist_del_init(&ax25_uid->uid_node); ax25_uid_put(ax25_uid); |
ae1b6a31b
|
192 |
goto again; |
1da177e4c
|
193 |
} |
1da177e4c
|
194 195 |
write_unlock(&ax25_uid_lock); } |