blob: 241e4680ecb1a642b83169df499f2241d8a9e7e7 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
5 */
Randy Dunlap4fc268d2006-01-11 12:17:47 -08006
7#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/errno.h>
9#include <linux/types.h>
10#include <linux/socket.h>
11#include <linux/in.h>
12#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/timer.h>
14#include <linux/string.h>
15#include <linux/sockios.h>
16#include <linux/net.h>
17#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <net/ax25.h>
20#include <linux/inet.h>
21#include <linux/netdevice.h>
22#include <linux/if_arp.h>
23#include <linux/skbuff.h>
24#include <net/sock.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080025#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/fcntl.h>
27#include <linux/mm.h>
28#include <linux/interrupt.h>
Ralf Baechle01d7dd02005-08-23 10:11:45 -070029#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/notifier.h>
31#include <linux/proc_fs.h>
32#include <linux/seq_file.h>
33#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/sysctl.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040035#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <net/ip.h>
37#include <net/arp.h>
38
39/*
40 * Callsign/UID mapper. This is in kernel space for security on multi-amateur machines.
41 */
42
Eric Dumazetf16f3022008-01-13 22:29:41 -080043static HLIST_HEAD(ax25_uid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static DEFINE_RWLOCK(ax25_uid_lock);
45
Eric Dumazetf16f3022008-01-13 22:29:41 -080046int ax25_uid_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Ralf Baechle70868ea2006-05-03 23:25:17 -070048EXPORT_SYMBOL(ax25_uid_policy);
49
Eric W. Biedermand13fda82012-05-24 12:55:00 -060050ax25_uid_assoc *ax25_findbyuid(kuid_t uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Ralf Baechle01d7dd02005-08-23 10:11:45 -070052 ax25_uid_assoc *ax25_uid, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 read_lock(&ax25_uid_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -080055 ax25_uid_for_each(ax25_uid, &ax25_uid_list) {
Eric W. Biedermand13fda82012-05-24 12:55:00 -060056 if (uid_eq(ax25_uid->uid, uid)) {
Ralf Baechle01d7dd02005-08-23 10:11:45 -070057 ax25_uid_hold(ax25_uid);
58 res = ax25_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 break;
60 }
61 }
62 read_unlock(&ax25_uid_lock);
63
Ralf Baechle01d7dd02005-08-23 10:11:45 -070064 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
Ralf Baechle70868ea2006-05-03 23:25:17 -070067EXPORT_SYMBOL(ax25_findbyuid);
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069int ax25_uid_ioctl(int cmd, struct sockaddr_ax25 *sax)
70{
Ralf Baechle01d7dd02005-08-23 10:11:45 -070071 ax25_uid_assoc *ax25_uid;
Ralf Baechle01d7dd02005-08-23 10:11:45 -070072 ax25_uid_assoc *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 unsigned long res;
74
75 switch (cmd) {
76 case SIOCAX25GETUID:
77 res = -ENOENT;
78 read_lock(&ax25_uid_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -080079 ax25_uid_for_each(ax25_uid, &ax25_uid_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (ax25cmp(&sax->sax25_call, &ax25_uid->call) == 0) {
Eric W. Biedermand13fda82012-05-24 12:55:00 -060081 res = from_kuid_munged(current_user_ns(), ax25_uid->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 break;
83 }
84 }
85 read_unlock(&ax25_uid_lock);
86
87 return res;
88
89 case SIOCAX25ADDUID:
Eric W. Biedermand13fda82012-05-24 12:55:00 -060090 {
91 kuid_t sax25_kuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (!capable(CAP_NET_ADMIN))
93 return -EPERM;
Eric W. Biedermand13fda82012-05-24 12:55:00 -060094 sax25_kuid = make_kuid(current_user_ns(), sax->sax25_uid);
95 if (!uid_valid(sax25_kuid))
96 return -EINVAL;
97 user = ax25_findbyuid(sax25_kuid);
Ralf Baechle01d7dd02005-08-23 10:11:45 -070098 if (user) {
99 ax25_uid_put(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return -EEXIST;
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (sax->sax25_uid == 0)
103 return -EINVAL;
104 if ((ax25_uid = kmalloc(sizeof(*ax25_uid), GFP_KERNEL)) == NULL)
105 return -ENOMEM;
106
Reshetova, Elena07f22822017-07-04 15:53:29 +0300107 refcount_set(&ax25_uid->refcount, 1);
Eric W. Biedermand13fda82012-05-24 12:55:00 -0600108 ax25_uid->uid = sax25_kuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 ax25_uid->call = sax->sax25_call;
110
111 write_lock(&ax25_uid_lock);
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700112 hlist_add_head(&ax25_uid->uid_node, &ax25_uid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 write_unlock(&ax25_uid_lock);
114
115 return 0;
Eric W. Biedermand13fda82012-05-24 12:55:00 -0600116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 case SIOCAX25DELUID:
118 if (!capable(CAP_NET_ADMIN))
119 return -EPERM;
120
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700121 ax25_uid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 write_lock(&ax25_uid_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800123 ax25_uid_for_each(ax25_uid, &ax25_uid_list) {
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700124 if (ax25cmp(&sax->sax25_call, &ax25_uid->call) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127 if (ax25_uid == NULL) {
128 write_unlock(&ax25_uid_lock);
129 return -ENOENT;
130 }
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700131 hlist_del_init(&ax25_uid->uid_node);
132 ax25_uid_put(ax25_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 write_unlock(&ax25_uid_lock);
134
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 default:
138 return -EINVAL;
139 }
140
141 return -EINVAL; /*NOTREACHED */
142}
143
144#ifdef CONFIG_PROC_FS
145
146static void *ax25_uid_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetf16f3022008-01-13 22:29:41 -0800147 __acquires(ax25_uid_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 read_lock(&ax25_uid_lock);
Li Zefanb512f3d2010-02-08 23:19:59 +0000150 return seq_hlist_start_head(&ax25_uid_list, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153static void *ax25_uid_seq_next(struct seq_file *seq, void *v, loff_t *pos)
154{
Li Zefanb512f3d2010-02-08 23:19:59 +0000155 return seq_hlist_next(v, &ax25_uid_list, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158static void ax25_uid_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetf16f3022008-01-13 22:29:41 -0800159 __releases(ax25_uid_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 read_unlock(&ax25_uid_lock);
162}
163
164static int ax25_uid_seq_show(struct seq_file *seq, void *v)
165{
Ralf Baechlef75268c2005-09-06 15:49:39 -0700166 char buf[11];
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (v == SEQ_START_TOKEN)
169 seq_printf(seq, "Policy: %d\n", ax25_uid_policy);
170 else {
Li Zefanb512f3d2010-02-08 23:19:59 +0000171 struct ax25_uid_assoc *pt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Li Zefanb512f3d2010-02-08 23:19:59 +0000173 pt = hlist_entry(v, struct ax25_uid_assoc, uid_node);
Eric W. Biedermand13fda82012-05-24 12:55:00 -0600174 seq_printf(seq, "%6d %s\n",
175 from_kuid_munged(seq_user_ns(seq), pt->uid),
176 ax2asc(buf, &pt->call));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
178 return 0;
179}
180
Christoph Hellwigfddda2b2018-04-13 19:44:18 +0200181const struct seq_operations ax25_uid_seqops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 .start = ax25_uid_seq_start,
183 .next = ax25_uid_seq_next,
184 .stop = ax25_uid_seq_stop,
185 .show = ax25_uid_seq_show,
186};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#endif
188
189/*
190 * Free all memory associated with UID/Callsign structures.
191 */
192void __exit ax25_uid_free(void)
193{
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700194 ax25_uid_assoc *ax25_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 write_lock(&ax25_uid_lock);
Pavel Emelyanovae1b6a32008-04-12 18:33:50 -0700197again:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800198 ax25_uid_for_each(ax25_uid, &ax25_uid_list) {
Ralf Baechle01d7dd02005-08-23 10:11:45 -0700199 hlist_del_init(&ax25_uid->uid_node);
200 ax25_uid_put(ax25_uid);
Pavel Emelyanovae1b6a32008-04-12 18:33:50 -0700201 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 write_unlock(&ax25_uid_lock);
204}