Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk) |
| 5 | */ |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 6 | |
| 7 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #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 Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 25 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/fcntl.h> |
| 27 | #include <linux/mm.h> |
| 28 | #include <linux/interrupt.h> |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 29 | #include <linux/list.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/notifier.h> |
| 31 | #include <linux/proc_fs.h> |
| 32 | #include <linux/seq_file.h> |
| 33 | #include <linux/stat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/sysctl.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 35 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #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 Dumazet | f16f302 | 2008-01-13 22:29:41 -0800 | [diff] [blame] | 43 | static HLIST_HEAD(ax25_uid_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | static DEFINE_RWLOCK(ax25_uid_lock); |
| 45 | |
Eric Dumazet | f16f302 | 2008-01-13 22:29:41 -0800 | [diff] [blame] | 46 | int ax25_uid_policy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Ralf Baechle | 70868ea | 2006-05-03 23:25:17 -0700 | [diff] [blame] | 48 | EXPORT_SYMBOL(ax25_uid_policy); |
| 49 | |
Eric W. Biederman | d13fda8 | 2012-05-24 12:55:00 -0600 | [diff] [blame] | 50 | ax25_uid_assoc *ax25_findbyuid(kuid_t uid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 52 | ax25_uid_assoc *ax25_uid, *res = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | read_lock(&ax25_uid_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 55 | ax25_uid_for_each(ax25_uid, &ax25_uid_list) { |
Eric W. Biederman | d13fda8 | 2012-05-24 12:55:00 -0600 | [diff] [blame] | 56 | if (uid_eq(ax25_uid->uid, uid)) { |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 57 | ax25_uid_hold(ax25_uid); |
| 58 | res = ax25_uid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | break; |
| 60 | } |
| 61 | } |
| 62 | read_unlock(&ax25_uid_lock); |
| 63 | |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 64 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Ralf Baechle | 70868ea | 2006-05-03 23:25:17 -0700 | [diff] [blame] | 67 | EXPORT_SYMBOL(ax25_findbyuid); |
| 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | int ax25_uid_ioctl(int cmd, struct sockaddr_ax25 *sax) |
| 70 | { |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 71 | ax25_uid_assoc *ax25_uid; |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 72 | ax25_uid_assoc *user; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | unsigned long res; |
| 74 | |
| 75 | switch (cmd) { |
| 76 | case SIOCAX25GETUID: |
| 77 | res = -ENOENT; |
| 78 | read_lock(&ax25_uid_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 79 | ax25_uid_for_each(ax25_uid, &ax25_uid_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | if (ax25cmp(&sax->sax25_call, &ax25_uid->call) == 0) { |
Eric W. Biederman | d13fda8 | 2012-05-24 12:55:00 -0600 | [diff] [blame] | 81 | res = from_kuid_munged(current_user_ns(), ax25_uid->uid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | break; |
| 83 | } |
| 84 | } |
| 85 | read_unlock(&ax25_uid_lock); |
| 86 | |
| 87 | return res; |
| 88 | |
| 89 | case SIOCAX25ADDUID: |
Eric W. Biederman | d13fda8 | 2012-05-24 12:55:00 -0600 | [diff] [blame] | 90 | { |
| 91 | kuid_t sax25_kuid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | if (!capable(CAP_NET_ADMIN)) |
| 93 | return -EPERM; |
Eric W. Biederman | d13fda8 | 2012-05-24 12:55:00 -0600 | [diff] [blame] | 94 | 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 Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 98 | if (user) { |
| 99 | ax25_uid_put(user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | return -EEXIST; |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | if (sax->sax25_uid == 0) |
| 103 | return -EINVAL; |
| 104 | if ((ax25_uid = kmalloc(sizeof(*ax25_uid), GFP_KERNEL)) == NULL) |
| 105 | return -ENOMEM; |
| 106 | |
Reshetova, Elena | 07f2282 | 2017-07-04 15:53:29 +0300 | [diff] [blame] | 107 | refcount_set(&ax25_uid->refcount, 1); |
Eric W. Biederman | d13fda8 | 2012-05-24 12:55:00 -0600 | [diff] [blame] | 108 | ax25_uid->uid = sax25_kuid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | ax25_uid->call = sax->sax25_call; |
| 110 | |
| 111 | write_lock(&ax25_uid_lock); |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 112 | hlist_add_head(&ax25_uid->uid_node, &ax25_uid_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | write_unlock(&ax25_uid_lock); |
| 114 | |
| 115 | return 0; |
Eric W. Biederman | d13fda8 | 2012-05-24 12:55:00 -0600 | [diff] [blame] | 116 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | case SIOCAX25DELUID: |
| 118 | if (!capable(CAP_NET_ADMIN)) |
| 119 | return -EPERM; |
| 120 | |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 121 | ax25_uid = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | write_lock(&ax25_uid_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 123 | ax25_uid_for_each(ax25_uid, &ax25_uid_list) { |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 124 | if (ax25cmp(&sax->sax25_call, &ax25_uid->call) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | if (ax25_uid == NULL) { |
| 128 | write_unlock(&ax25_uid_lock); |
| 129 | return -ENOENT; |
| 130 | } |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 131 | hlist_del_init(&ax25_uid->uid_node); |
| 132 | ax25_uid_put(ax25_uid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | write_unlock(&ax25_uid_lock); |
| 134 | |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 135 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | default: |
| 138 | return -EINVAL; |
| 139 | } |
| 140 | |
| 141 | return -EINVAL; /*NOTREACHED */ |
| 142 | } |
| 143 | |
| 144 | #ifdef CONFIG_PROC_FS |
| 145 | |
| 146 | static void *ax25_uid_seq_start(struct seq_file *seq, loff_t *pos) |
Eric Dumazet | f16f302 | 2008-01-13 22:29:41 -0800 | [diff] [blame] | 147 | __acquires(ax25_uid_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | read_lock(&ax25_uid_lock); |
Li Zefan | b512f3d | 2010-02-08 23:19:59 +0000 | [diff] [blame] | 150 | return seq_hlist_start_head(&ax25_uid_list, *pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static void *ax25_uid_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 154 | { |
Li Zefan | b512f3d | 2010-02-08 23:19:59 +0000 | [diff] [blame] | 155 | return seq_hlist_next(v, &ax25_uid_list, pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static void ax25_uid_seq_stop(struct seq_file *seq, void *v) |
Eric Dumazet | f16f302 | 2008-01-13 22:29:41 -0800 | [diff] [blame] | 159 | __releases(ax25_uid_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | { |
| 161 | read_unlock(&ax25_uid_lock); |
| 162 | } |
| 163 | |
| 164 | static int ax25_uid_seq_show(struct seq_file *seq, void *v) |
| 165 | { |
Ralf Baechle | f75268c | 2005-09-06 15:49:39 -0700 | [diff] [blame] | 166 | char buf[11]; |
| 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | if (v == SEQ_START_TOKEN) |
| 169 | seq_printf(seq, "Policy: %d\n", ax25_uid_policy); |
| 170 | else { |
Li Zefan | b512f3d | 2010-02-08 23:19:59 +0000 | [diff] [blame] | 171 | struct ax25_uid_assoc *pt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Li Zefan | b512f3d | 2010-02-08 23:19:59 +0000 | [diff] [blame] | 173 | pt = hlist_entry(v, struct ax25_uid_assoc, uid_node); |
Eric W. Biederman | d13fda8 | 2012-05-24 12:55:00 -0600 | [diff] [blame] | 174 | seq_printf(seq, "%6d %s\n", |
| 175 | from_kuid_munged(seq_user_ns(seq), pt->uid), |
| 176 | ax2asc(buf, &pt->call)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
| 178 | return 0; |
| 179 | } |
| 180 | |
Christoph Hellwig | fddda2b | 2018-04-13 19:44:18 +0200 | [diff] [blame] | 181 | const struct seq_operations ax25_uid_seqops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | .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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | #endif |
| 188 | |
| 189 | /* |
| 190 | * Free all memory associated with UID/Callsign structures. |
| 191 | */ |
| 192 | void __exit ax25_uid_free(void) |
| 193 | { |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 194 | ax25_uid_assoc *ax25_uid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
| 196 | write_lock(&ax25_uid_lock); |
Pavel Emelyanov | ae1b6a3 | 2008-04-12 18:33:50 -0700 | [diff] [blame] | 197 | again: |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 198 | ax25_uid_for_each(ax25_uid, &ax25_uid_list) { |
Ralf Baechle | 01d7dd0 | 2005-08-23 10:11:45 -0700 | [diff] [blame] | 199 | hlist_del_init(&ax25_uid->uid_node); |
| 200 | ax25_uid_put(ax25_uid); |
Pavel Emelyanov | ae1b6a3 | 2008-04-12 18:33:50 -0700 | [diff] [blame] | 201 | goto again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | write_unlock(&ax25_uid_lock); |
| 204 | } |