Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Simplified MAC Kernel (smack) security module |
| 4 | * |
| 5 | * This file contains the smack hook function implementations. |
| 6 | * |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 7 | * Authors: |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 8 | * Casey Schaufler <casey@schaufler-ca.com> |
Jarkko Sakkinen | 84088ba | 2011-10-07 09:27:53 +0300 | [diff] [blame] | 9 | * Jarkko Sakkinen <jarkko.sakkinen@intel.com> |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com> |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 12 | * Copyright (C) 2009 Hewlett-Packard Development Company, L.P. |
Paul Moore | 82c21bf | 2011-08-01 11:10:33 +0000 | [diff] [blame] | 13 | * Paul Moore <paul@paul-moore.com> |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 14 | * Copyright (C) 2010 Nokia Corporation |
Jarkko Sakkinen | 84088ba | 2011-10-07 09:27:53 +0300 | [diff] [blame] | 15 | * Copyright (C) 2011 Intel Corporation. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <linux/xattr.h> |
| 19 | #include <linux/pagemap.h> |
| 20 | #include <linux/mount.h> |
| 21 | #include <linux/stat.h> |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 22 | #include <linux/kd.h> |
| 23 | #include <asm/ioctls.h> |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 24 | #include <linux/ip.h> |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 25 | #include <linux/tcp.h> |
| 26 | #include <linux/udp.h> |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 27 | #include <linux/dccp.h> |
Piotr Sawicki | d66a8ac | 2018-07-19 11:47:31 +0200 | [diff] [blame] | 28 | #include <linux/icmpv6.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 30 | #include <linux/mutex.h> |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 31 | #include <net/cipso_ipv4.h> |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 32 | #include <net/ip.h> |
| 33 | #include <net/ipv6.h> |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 34 | #include <linux/audit.h> |
Nick Black | 1fd7317d | 2009-09-22 16:43:33 -0700 | [diff] [blame] | 35 | #include <linux/magic.h> |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 36 | #include <linux/dcache.h> |
Jarkko Sakkinen | 16014d8 | 2011-10-14 13:16:24 +0300 | [diff] [blame] | 37 | #include <linux/personality.h> |
Al Viro | 4040153 | 2012-02-13 03:58:52 +0000 | [diff] [blame] | 38 | #include <linux/msg.h> |
| 39 | #include <linux/shm.h> |
| 40 | #include <linux/binfmts.h> |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 41 | #include <linux/parser.h> |
David Howells | 2febd25 | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 42 | #include <linux/fs_context.h> |
| 43 | #include <linux/fs_parser.h> |
David Howells | a8478a6 | 2020-01-14 17:07:13 +0000 | [diff] [blame] | 44 | #include <linux/watch_queue.h> |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 45 | #include "smack.h" |
| 46 | |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 47 | #define TRANS_TRUE "TRUE" |
| 48 | #define TRANS_TRUE_SIZE 4 |
| 49 | |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 50 | #define SMK_CONNECTING 0 |
| 51 | #define SMK_RECEIVING 1 |
| 52 | #define SMK_SENDING 2 |
| 53 | |
Sebastian Andrzej Siewior | 222a96b | 2021-09-23 19:05:03 +0200 | [diff] [blame] | 54 | #ifdef SMACK_IPV6_PORT_LABELING |
Arnd Bergmann | 00720f0 | 2020-04-08 21:04:31 +0200 | [diff] [blame] | 55 | static DEFINE_MUTEX(smack_ipv6_lock); |
Geliang Tang | 8b549ef | 2015-09-27 23:10:25 +0800 | [diff] [blame] | 56 | static LIST_HEAD(smk_ipv6_port_list); |
Sebastian Andrzej Siewior | 222a96b | 2021-09-23 19:05:03 +0200 | [diff] [blame] | 57 | #endif |
Casey Schaufler | 4e328b0 | 2019-04-02 11:37:12 -0700 | [diff] [blame] | 58 | struct kmem_cache *smack_rule_cache; |
Austin Kim | bfc3cac | 2021-06-29 14:41:44 +0100 | [diff] [blame] | 59 | int smack_enabled __initdata; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 60 | |
Al Viro | c3300aa | 2018-12-16 01:52:24 -0500 | [diff] [blame] | 61 | #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s} |
| 62 | static struct { |
| 63 | const char *name; |
| 64 | int len; |
| 65 | int opt; |
| 66 | } smk_mount_opts[] = { |
Casey Schaufler | 6e7739f | 2019-05-31 11:53:33 +0100 | [diff] [blame] | 67 | {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault}, |
Al Viro | c3300aa | 2018-12-16 01:52:24 -0500 | [diff] [blame] | 68 | A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute) |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 69 | }; |
Al Viro | c3300aa | 2018-12-16 01:52:24 -0500 | [diff] [blame] | 70 | #undef A |
| 71 | |
| 72 | static int match_opt_prefix(char *s, int l, char **arg) |
| 73 | { |
| 74 | int i; |
| 75 | |
| 76 | for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) { |
| 77 | size_t len = smk_mount_opts[i].len; |
| 78 | if (len > l || memcmp(s, smk_mount_opts[i].name, len)) |
| 79 | continue; |
| 80 | if (len == l || s[len] != '=') |
| 81 | continue; |
| 82 | *arg = s + len + 1; |
| 83 | return smk_mount_opts[i].opt; |
| 84 | } |
| 85 | return Opt_error; |
| 86 | } |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 87 | |
Casey Schaufler | 3d04c92 | 2015-08-12 11:56:02 -0700 | [diff] [blame] | 88 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP |
| 89 | static char *smk_bu_mess[] = { |
| 90 | "Bringup Error", /* Unused */ |
| 91 | "Bringup", /* SMACK_BRINGUP_ALLOW */ |
| 92 | "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */ |
| 93 | "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */ |
| 94 | }; |
| 95 | |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 96 | static void smk_bu_mode(int mode, char *s) |
| 97 | { |
| 98 | int i = 0; |
| 99 | |
| 100 | if (mode & MAY_READ) |
| 101 | s[i++] = 'r'; |
| 102 | if (mode & MAY_WRITE) |
| 103 | s[i++] = 'w'; |
| 104 | if (mode & MAY_EXEC) |
| 105 | s[i++] = 'x'; |
| 106 | if (mode & MAY_APPEND) |
| 107 | s[i++] = 'a'; |
| 108 | if (mode & MAY_TRANSMUTE) |
| 109 | s[i++] = 't'; |
| 110 | if (mode & MAY_LOCK) |
| 111 | s[i++] = 'l'; |
| 112 | if (i == 0) |
| 113 | s[i++] = '-'; |
| 114 | s[i] = '\0'; |
| 115 | } |
| 116 | #endif |
| 117 | |
| 118 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 119 | static int smk_bu_note(char *note, struct smack_known *sskp, |
| 120 | struct smack_known *oskp, int mode, int rc) |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 121 | { |
| 122 | char acc[SMK_NUM_ACCESS_TYPE + 1]; |
| 123 | |
| 124 | if (rc <= 0) |
| 125 | return rc; |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 126 | if (rc > SMACK_UNCONFINED_OBJECT) |
| 127 | rc = 0; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 128 | |
| 129 | smk_bu_mode(mode, acc); |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 130 | pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc], |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 131 | sskp->smk_known, oskp->smk_known, acc, note); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 132 | return 0; |
| 133 | } |
| 134 | #else |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 135 | #define smk_bu_note(note, sskp, oskp, mode, RC) (RC) |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 136 | #endif |
| 137 | |
| 138 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 139 | static int smk_bu_current(char *note, struct smack_known *oskp, |
| 140 | int mode, int rc) |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 141 | { |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 142 | struct task_smack *tsp = smack_cred(current_cred()); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 143 | char acc[SMK_NUM_ACCESS_TYPE + 1]; |
| 144 | |
| 145 | if (rc <= 0) |
| 146 | return rc; |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 147 | if (rc > SMACK_UNCONFINED_OBJECT) |
| 148 | rc = 0; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 149 | |
| 150 | smk_bu_mode(mode, acc); |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 151 | pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc], |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 152 | tsp->smk_task->smk_known, oskp->smk_known, |
| 153 | acc, current->comm, note); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | #else |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 157 | #define smk_bu_current(note, oskp, mode, RC) (RC) |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 158 | #endif |
| 159 | |
| 160 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP |
| 161 | static int smk_bu_task(struct task_struct *otp, int mode, int rc) |
| 162 | { |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 163 | struct task_smack *tsp = smack_cred(current_cred()); |
Paul Moore | 1fb057d | 2021-02-19 15:04:58 -0500 | [diff] [blame] | 164 | struct smack_known *smk_task = smk_of_task_struct_obj(otp); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 165 | char acc[SMK_NUM_ACCESS_TYPE + 1]; |
| 166 | |
| 167 | if (rc <= 0) |
| 168 | return rc; |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 169 | if (rc > SMACK_UNCONFINED_OBJECT) |
| 170 | rc = 0; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 171 | |
| 172 | smk_bu_mode(mode, acc); |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 173 | pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc], |
Andrey Ryabinin | 6d1cff2 | 2015-01-13 18:52:40 +0300 | [diff] [blame] | 174 | tsp->smk_task->smk_known, smk_task->smk_known, acc, |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 175 | current->comm, otp->comm); |
| 176 | return 0; |
| 177 | } |
| 178 | #else |
| 179 | #define smk_bu_task(otp, mode, RC) (RC) |
| 180 | #endif |
| 181 | |
| 182 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP |
| 183 | static int smk_bu_inode(struct inode *inode, int mode, int rc) |
| 184 | { |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 185 | struct task_smack *tsp = smack_cred(current_cred()); |
Casey Schaufler | fb4021b | 2018-11-12 12:43:01 -0800 | [diff] [blame] | 186 | struct inode_smack *isp = smack_inode(inode); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 187 | char acc[SMK_NUM_ACCESS_TYPE + 1]; |
| 188 | |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 189 | if (isp->smk_flags & SMK_INODE_IMPURE) |
| 190 | pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n", |
| 191 | inode->i_sb->s_id, inode->i_ino, current->comm); |
| 192 | |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 193 | if (rc <= 0) |
| 194 | return rc; |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 195 | if (rc > SMACK_UNCONFINED_OBJECT) |
| 196 | rc = 0; |
| 197 | if (rc == SMACK_UNCONFINED_SUBJECT && |
| 198 | (mode & (MAY_WRITE | MAY_APPEND))) |
| 199 | isp->smk_flags |= SMK_INODE_IMPURE; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 200 | |
| 201 | smk_bu_mode(mode, acc); |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 202 | |
| 203 | pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc], |
| 204 | tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc, |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 205 | inode->i_sb->s_id, inode->i_ino, current->comm); |
| 206 | return 0; |
| 207 | } |
| 208 | #else |
| 209 | #define smk_bu_inode(inode, mode, RC) (RC) |
| 210 | #endif |
| 211 | |
| 212 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP |
| 213 | static int smk_bu_file(struct file *file, int mode, int rc) |
| 214 | { |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 215 | struct task_smack *tsp = smack_cred(current_cred()); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 216 | struct smack_known *sskp = tsp->smk_task; |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 217 | struct inode *inode = file_inode(file); |
Casey Schaufler | fb4021b | 2018-11-12 12:43:01 -0800 | [diff] [blame] | 218 | struct inode_smack *isp = smack_inode(inode); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 219 | char acc[SMK_NUM_ACCESS_TYPE + 1]; |
| 220 | |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 221 | if (isp->smk_flags & SMK_INODE_IMPURE) |
| 222 | pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n", |
| 223 | inode->i_sb->s_id, inode->i_ino, current->comm); |
| 224 | |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 225 | if (rc <= 0) |
| 226 | return rc; |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 227 | if (rc > SMACK_UNCONFINED_OBJECT) |
| 228 | rc = 0; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 229 | |
| 230 | smk_bu_mode(mode, acc); |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 231 | pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc], |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 232 | sskp->smk_known, smk_of_inode(inode)->smk_known, acc, |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 233 | inode->i_sb->s_id, inode->i_ino, file, |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 234 | current->comm); |
| 235 | return 0; |
| 236 | } |
| 237 | #else |
| 238 | #define smk_bu_file(file, mode, RC) (RC) |
| 239 | #endif |
| 240 | |
| 241 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP |
| 242 | static int smk_bu_credfile(const struct cred *cred, struct file *file, |
| 243 | int mode, int rc) |
| 244 | { |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 245 | struct task_smack *tsp = smack_cred(cred); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 246 | struct smack_known *sskp = tsp->smk_task; |
Al Viro | 4506309 | 2016-12-04 18:24:56 -0500 | [diff] [blame] | 247 | struct inode *inode = file_inode(file); |
Casey Schaufler | fb4021b | 2018-11-12 12:43:01 -0800 | [diff] [blame] | 248 | struct inode_smack *isp = smack_inode(inode); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 249 | char acc[SMK_NUM_ACCESS_TYPE + 1]; |
| 250 | |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 251 | if (isp->smk_flags & SMK_INODE_IMPURE) |
| 252 | pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n", |
| 253 | inode->i_sb->s_id, inode->i_ino, current->comm); |
| 254 | |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 255 | if (rc <= 0) |
| 256 | return rc; |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 257 | if (rc > SMACK_UNCONFINED_OBJECT) |
| 258 | rc = 0; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 259 | |
| 260 | smk_bu_mode(mode, acc); |
Casey Schaufler | bf4b2fe | 2015-03-21 18:26:40 -0700 | [diff] [blame] | 261 | pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc], |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 262 | sskp->smk_known, smk_of_inode(inode)->smk_known, acc, |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 263 | inode->i_sb->s_id, inode->i_ino, file, |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 264 | current->comm); |
| 265 | return 0; |
| 266 | } |
| 267 | #else |
| 268 | #define smk_bu_credfile(cred, file, mode, RC) (RC) |
| 269 | #endif |
| 270 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 271 | /** |
| 272 | * smk_fetch - Fetch the smack label from a file. |
Lukasz Pawelczyk | 1a28979 | 2014-11-26 15:31:06 +0100 | [diff] [blame] | 273 | * @name: type of the label (attribute) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 274 | * @ip: a pointer to the inode |
| 275 | * @dp: a pointer to the dentry |
| 276 | * |
Lukasz Pawelczyk | e774ad6 | 2015-04-20 17:12:54 +0200 | [diff] [blame] | 277 | * Returns a pointer to the master list entry for the Smack label, |
| 278 | * NULL if there was no label to fetch, or an error code. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 279 | */ |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 280 | static struct smack_known *smk_fetch(const char *name, struct inode *ip, |
| 281 | struct dentry *dp) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 282 | { |
| 283 | int rc; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 284 | char *buffer; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 285 | struct smack_known *skp = NULL; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 286 | |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 287 | if (!(ip->i_opflags & IOP_XATTR)) |
Lukasz Pawelczyk | e774ad6 | 2015-04-20 17:12:54 +0200 | [diff] [blame] | 288 | return ERR_PTR(-EOPNOTSUPP); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 289 | |
Eric Biggers | e5bfad3 | 2019-08-21 22:54:41 -0700 | [diff] [blame] | 290 | buffer = kzalloc(SMK_LONGLABEL, GFP_NOFS); |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 291 | if (buffer == NULL) |
Lukasz Pawelczyk | e774ad6 | 2015-04-20 17:12:54 +0200 | [diff] [blame] | 292 | return ERR_PTR(-ENOMEM); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 293 | |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 294 | rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL); |
Lukasz Pawelczyk | e774ad6 | 2015-04-20 17:12:54 +0200 | [diff] [blame] | 295 | if (rc < 0) |
| 296 | skp = ERR_PTR(rc); |
| 297 | else if (rc == 0) |
| 298 | skp = NULL; |
| 299 | else |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 300 | skp = smk_import_entry(buffer, rc); |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 301 | |
| 302 | kfree(buffer); |
| 303 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 304 | return skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /** |
Casey Schaufler | afb1cbe3 | 2018-09-21 17:19:29 -0700 | [diff] [blame] | 308 | * init_inode_smack - initialize an inode security blob |
luanshi | a1a07f2 | 2019-07-05 10:35:20 +0800 | [diff] [blame] | 309 | * @inode: inode to extract the info from |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 310 | * @skp: a pointer to the Smack label entry to use in the blob |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 311 | * |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 312 | */ |
Casey Schaufler | afb1cbe3 | 2018-09-21 17:19:29 -0700 | [diff] [blame] | 313 | static void init_inode_smack(struct inode *inode, struct smack_known *skp) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 314 | { |
Casey Schaufler | afb1cbe3 | 2018-09-21 17:19:29 -0700 | [diff] [blame] | 315 | struct inode_smack *isp = smack_inode(inode); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 316 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 317 | isp->smk_inode = skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 318 | isp->smk_flags = 0; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 321 | /** |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 322 | * init_task_smack - initialize a task security blob |
| 323 | * @tsp: blob to initialize |
Lukasz Pawelczyk | 1a28979 | 2014-11-26 15:31:06 +0100 | [diff] [blame] | 324 | * @task: a pointer to the Smack label for the running task |
| 325 | * @forked: a pointer to the Smack label for the forked task |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 326 | * |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 327 | */ |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 328 | static void init_task_smack(struct task_smack *tsp, struct smack_known *task, |
| 329 | struct smack_known *forked) |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 330 | { |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 331 | tsp->smk_task = task; |
| 332 | tsp->smk_forked = forked; |
| 333 | INIT_LIST_HEAD(&tsp->smk_rules); |
Zbigniew Jasinski | 38416e5 | 2015-10-19 18:23:53 +0200 | [diff] [blame] | 334 | INIT_LIST_HEAD(&tsp->smk_relabel); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 335 | mutex_init(&tsp->smk_rules_lock); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /** |
| 339 | * smk_copy_rules - copy a rule set |
Lukasz Pawelczyk | 1a28979 | 2014-11-26 15:31:06 +0100 | [diff] [blame] | 340 | * @nhead: new rules header pointer |
| 341 | * @ohead: old rules header pointer |
| 342 | * @gfp: type of the memory for the allocation |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 343 | * |
| 344 | * Returns 0 on success, -ENOMEM on error |
| 345 | */ |
| 346 | static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead, |
| 347 | gfp_t gfp) |
| 348 | { |
| 349 | struct smack_rule *nrp; |
| 350 | struct smack_rule *orp; |
| 351 | int rc = 0; |
| 352 | |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 353 | list_for_each_entry_rcu(orp, ohead, list) { |
Casey Schaufler | 4e328b0 | 2019-04-02 11:37:12 -0700 | [diff] [blame] | 354 | nrp = kmem_cache_zalloc(smack_rule_cache, gfp); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 355 | if (nrp == NULL) { |
| 356 | rc = -ENOMEM; |
| 357 | break; |
| 358 | } |
| 359 | *nrp = *orp; |
| 360 | list_add_rcu(&nrp->list, nhead); |
| 361 | } |
| 362 | return rc; |
| 363 | } |
| 364 | |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 365 | /** |
Zbigniew Jasinski | 38416e5 | 2015-10-19 18:23:53 +0200 | [diff] [blame] | 366 | * smk_copy_relabel - copy smk_relabel labels list |
| 367 | * @nhead: new rules header pointer |
| 368 | * @ohead: old rules header pointer |
| 369 | * @gfp: type of the memory for the allocation |
| 370 | * |
| 371 | * Returns 0 on success, -ENOMEM on error |
| 372 | */ |
| 373 | static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead, |
| 374 | gfp_t gfp) |
| 375 | { |
| 376 | struct smack_known_list_elem *nklep; |
| 377 | struct smack_known_list_elem *oklep; |
| 378 | |
Zbigniew Jasinski | 38416e5 | 2015-10-19 18:23:53 +0200 | [diff] [blame] | 379 | list_for_each_entry(oklep, ohead, list) { |
| 380 | nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp); |
| 381 | if (nklep == NULL) { |
| 382 | smk_destroy_label_list(nhead); |
| 383 | return -ENOMEM; |
| 384 | } |
| 385 | nklep->smk_label = oklep->smk_label; |
| 386 | list_add(&nklep->list, nhead); |
| 387 | } |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | /** |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 393 | * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_* |
Casey Schaufler | b57d020 | 2021-10-13 14:56:43 -0700 | [diff] [blame] | 394 | * @mode: input mode in form of PTRACE_MODE_* |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 395 | * |
| 396 | * Returns a converted MAY_* mode usable by smack rules |
| 397 | */ |
| 398 | static inline unsigned int smk_ptrace_mode(unsigned int mode) |
| 399 | { |
Jann Horn | 3dfb7d8 | 2016-01-20 15:00:01 -0800 | [diff] [blame] | 400 | if (mode & PTRACE_MODE_ATTACH) |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 401 | return MAY_READWRITE; |
Jann Horn | 3dfb7d8 | 2016-01-20 15:00:01 -0800 | [diff] [blame] | 402 | if (mode & PTRACE_MODE_READ) |
| 403 | return MAY_READ; |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * smk_ptrace_rule_check - helper for ptrace access |
| 410 | * @tracer: tracer process |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 411 | * @tracee_known: label entry of the process that's about to be traced |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 412 | * @mode: ptrace attachment mode (PTRACE_MODE_*) |
| 413 | * @func: name of the function that called us, used for audit |
| 414 | * |
| 415 | * Returns 0 on access granted, -error on error |
| 416 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 417 | static int smk_ptrace_rule_check(struct task_struct *tracer, |
| 418 | struct smack_known *tracee_known, |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 419 | unsigned int mode, const char *func) |
| 420 | { |
| 421 | int rc; |
| 422 | struct smk_audit_info ad, *saip = NULL; |
| 423 | struct task_smack *tsp; |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 424 | struct smack_known *tracer_known; |
Casey Schaufler | dcb569c | 2018-09-18 16:09:16 -0700 | [diff] [blame] | 425 | const struct cred *tracercred; |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 426 | |
| 427 | if ((mode & PTRACE_MODE_NOAUDIT) == 0) { |
| 428 | smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK); |
| 429 | smk_ad_setfield_u_tsk(&ad, tracer); |
| 430 | saip = &ad; |
| 431 | } |
| 432 | |
Andrey Ryabinin | 6d1cff2 | 2015-01-13 18:52:40 +0300 | [diff] [blame] | 433 | rcu_read_lock(); |
Casey Schaufler | dcb569c | 2018-09-18 16:09:16 -0700 | [diff] [blame] | 434 | tracercred = __task_cred(tracer); |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 435 | tsp = smack_cred(tracercred); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 436 | tracer_known = smk_of_task(tsp); |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 437 | |
Lukasz Pawelczyk | 6686781 | 2014-03-11 17:07:06 +0100 | [diff] [blame] | 438 | if ((mode & PTRACE_MODE_ATTACH) && |
| 439 | (smack_ptrace_rule == SMACK_PTRACE_EXACT || |
| 440 | smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 441 | if (tracer_known->smk_known == tracee_known->smk_known) |
Lukasz Pawelczyk | 6686781 | 2014-03-11 17:07:06 +0100 | [diff] [blame] | 442 | rc = 0; |
| 443 | else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN) |
| 444 | rc = -EACCES; |
Casey Schaufler | dcb569c | 2018-09-18 16:09:16 -0700 | [diff] [blame] | 445 | else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred)) |
Lukasz Pawelczyk | 6686781 | 2014-03-11 17:07:06 +0100 | [diff] [blame] | 446 | rc = 0; |
| 447 | else |
| 448 | rc = -EACCES; |
| 449 | |
| 450 | if (saip) |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 451 | smack_log(tracer_known->smk_known, |
| 452 | tracee_known->smk_known, |
| 453 | 0, rc, saip); |
Lukasz Pawelczyk | 6686781 | 2014-03-11 17:07:06 +0100 | [diff] [blame] | 454 | |
Andrey Ryabinin | 6d1cff2 | 2015-01-13 18:52:40 +0300 | [diff] [blame] | 455 | rcu_read_unlock(); |
Lukasz Pawelczyk | 6686781 | 2014-03-11 17:07:06 +0100 | [diff] [blame] | 456 | return rc; |
| 457 | } |
| 458 | |
| 459 | /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 460 | rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip); |
Andrey Ryabinin | 6d1cff2 | 2015-01-13 18:52:40 +0300 | [diff] [blame] | 461 | |
| 462 | rcu_read_unlock(); |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 463 | return rc; |
| 464 | } |
| 465 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 466 | /* |
| 467 | * LSM hooks. |
| 468 | * We he, that is fun! |
| 469 | */ |
| 470 | |
| 471 | /** |
Ingo Molnar | 9e48858 | 2009-05-07 19:26:19 +1000 | [diff] [blame] | 472 | * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 473 | * @ctp: child task pointer |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 474 | * @mode: ptrace attachment mode (PTRACE_MODE_*) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 475 | * |
| 476 | * Returns 0 if access is OK, an error code otherwise |
| 477 | * |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 478 | * Do the capability checks. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 479 | */ |
Ingo Molnar | 9e48858 | 2009-05-07 19:26:19 +1000 | [diff] [blame] | 480 | static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 481 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 482 | struct smack_known *skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 483 | |
Paul Moore | 1fb057d | 2021-02-19 15:04:58 -0500 | [diff] [blame] | 484 | skp = smk_of_task_struct_obj(ctp); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 485 | |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 486 | return smk_ptrace_rule_check(current, skp, mode, __func__); |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 487 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 488 | |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 489 | /** |
| 490 | * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME |
| 491 | * @ptp: parent task pointer |
| 492 | * |
| 493 | * Returns 0 if access is OK, an error code otherwise |
| 494 | * |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 495 | * Do the capability checks, and require PTRACE_MODE_ATTACH. |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 496 | */ |
| 497 | static int smack_ptrace_traceme(struct task_struct *ptp) |
| 498 | { |
| 499 | int rc; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 500 | struct smack_known *skp; |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 501 | |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 502 | skp = smk_of_task(smack_cred(current_cred())); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 503 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 504 | rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 505 | return rc; |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * smack_syslog - Smack approval on syslog |
luanshi | a1a07f2 | 2019-07-05 10:35:20 +0800 | [diff] [blame] | 510 | * @typefrom_file: unused |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 511 | * |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 512 | * Returns 0 on success, error code otherwise. |
| 513 | */ |
Eric Paris | 12b3052 | 2010-11-15 18:36:29 -0500 | [diff] [blame] | 514 | static int smack_syslog(int typefrom_file) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 515 | { |
Eric Paris | 12b3052 | 2010-11-15 18:36:29 -0500 | [diff] [blame] | 516 | int rc = 0; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 517 | struct smack_known *skp = smk_of_current(); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 518 | |
Casey Schaufler | 1880eff | 2012-06-05 15:28:30 -0700 | [diff] [blame] | 519 | if (smack_privileged(CAP_MAC_OVERRIDE)) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 520 | return 0; |
| 521 | |
Casey Schaufler | 24ea1b6 | 2013-12-30 09:38:00 -0800 | [diff] [blame] | 522 | if (smack_syslog_label != NULL && smack_syslog_label != skp) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 523 | rc = -EACCES; |
| 524 | |
| 525 | return rc; |
| 526 | } |
| 527 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 528 | /* |
| 529 | * Superblock Hooks. |
| 530 | */ |
| 531 | |
| 532 | /** |
| 533 | * smack_sb_alloc_security - allocate a superblock blob |
| 534 | * @sb: the superblock getting the blob |
| 535 | * |
| 536 | * Returns 0 on success or -ENOMEM on error. |
| 537 | */ |
| 538 | static int smack_sb_alloc_security(struct super_block *sb) |
| 539 | { |
Casey Schaufler | 1aea780 | 2021-04-22 17:41:15 +0200 | [diff] [blame] | 540 | struct superblock_smack *sbsp = smack_superblock(sb); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 541 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 542 | sbsp->smk_root = &smack_known_floor; |
| 543 | sbsp->smk_default = &smack_known_floor; |
| 544 | sbsp->smk_floor = &smack_known_floor; |
| 545 | sbsp->smk_hat = &smack_known_hat; |
Casey Schaufler | e830b39 | 2013-05-22 18:43:07 -0700 | [diff] [blame] | 546 | /* |
Seth Forshee | 9f50eda | 2015-09-23 15:16:06 -0500 | [diff] [blame] | 547 | * SMK_SB_INITIALIZED will be zero from kzalloc. |
Casey Schaufler | e830b39 | 2013-05-22 18:43:07 -0700 | [diff] [blame] | 548 | */ |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
Al Viro | 12085b1 | 2018-12-13 15:18:05 -0500 | [diff] [blame] | 553 | struct smack_mnt_opts { |
| 554 | const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute; |
| 555 | }; |
| 556 | |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 557 | static void smack_free_mnt_opts(void *mnt_opts) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 558 | { |
Al Viro | 12085b1 | 2018-12-13 15:18:05 -0500 | [diff] [blame] | 559 | struct smack_mnt_opts *opts = mnt_opts; |
| 560 | kfree(opts->fsdefault); |
| 561 | kfree(opts->fsfloor); |
| 562 | kfree(opts->fshat); |
| 563 | kfree(opts->fsroot); |
| 564 | kfree(opts->fstransmute); |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 565 | kfree(opts); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Al Viro | 55c0e5b | 2018-12-16 01:09:45 -0500 | [diff] [blame] | 568 | static int smack_add_opt(int token, const char *s, void **mnt_opts) |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 569 | { |
Al Viro | 55c0e5b | 2018-12-16 01:09:45 -0500 | [diff] [blame] | 570 | struct smack_mnt_opts *opts = *mnt_opts; |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 571 | |
Al Viro | 55c0e5b | 2018-12-16 01:09:45 -0500 | [diff] [blame] | 572 | if (!opts) { |
| 573 | opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL); |
| 574 | if (!opts) |
| 575 | return -ENOMEM; |
| 576 | *mnt_opts = opts; |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 577 | } |
Al Viro | 55c0e5b | 2018-12-16 01:09:45 -0500 | [diff] [blame] | 578 | if (!s) |
| 579 | return -ENOMEM; |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 580 | |
Al Viro | 55c0e5b | 2018-12-16 01:09:45 -0500 | [diff] [blame] | 581 | switch (token) { |
| 582 | case Opt_fsdefault: |
| 583 | if (opts->fsdefault) |
| 584 | goto out_opt_err; |
| 585 | opts->fsdefault = s; |
| 586 | break; |
| 587 | case Opt_fsfloor: |
| 588 | if (opts->fsfloor) |
| 589 | goto out_opt_err; |
| 590 | opts->fsfloor = s; |
| 591 | break; |
| 592 | case Opt_fshat: |
| 593 | if (opts->fshat) |
| 594 | goto out_opt_err; |
| 595 | opts->fshat = s; |
| 596 | break; |
| 597 | case Opt_fsroot: |
| 598 | if (opts->fsroot) |
| 599 | goto out_opt_err; |
| 600 | opts->fsroot = s; |
| 601 | break; |
| 602 | case Opt_fstransmute: |
| 603 | if (opts->fstransmute) |
| 604 | goto out_opt_err; |
| 605 | opts->fstransmute = s; |
| 606 | break; |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 607 | } |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 608 | return 0; |
| 609 | |
| 610 | out_opt_err: |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 611 | pr_warn("Smack: duplicate mount options\n"); |
Al Viro | 55c0e5b | 2018-12-16 01:09:45 -0500 | [diff] [blame] | 612 | return -EINVAL; |
| 613 | } |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 614 | |
Al Viro | 0b52075 | 2018-12-23 16:02:47 -0500 | [diff] [blame] | 615 | /** |
| 616 | * smack_fs_context_dup - Duplicate the security data on fs_context duplication |
| 617 | * @fc: The new filesystem context. |
| 618 | * @src_fc: The source filesystem context being duplicated. |
| 619 | * |
| 620 | * Returns 0 on success or -ENOMEM on error. |
| 621 | */ |
| 622 | static int smack_fs_context_dup(struct fs_context *fc, |
| 623 | struct fs_context *src_fc) |
| 624 | { |
| 625 | struct smack_mnt_opts *dst, *src = src_fc->security; |
| 626 | |
| 627 | if (!src) |
| 628 | return 0; |
| 629 | |
| 630 | fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL); |
| 631 | if (!fc->security) |
| 632 | return -ENOMEM; |
| 633 | dst = fc->security; |
| 634 | |
| 635 | if (src->fsdefault) { |
| 636 | dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL); |
| 637 | if (!dst->fsdefault) |
| 638 | return -ENOMEM; |
| 639 | } |
| 640 | if (src->fsfloor) { |
| 641 | dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL); |
| 642 | if (!dst->fsfloor) |
| 643 | return -ENOMEM; |
| 644 | } |
| 645 | if (src->fshat) { |
| 646 | dst->fshat = kstrdup(src->fshat, GFP_KERNEL); |
| 647 | if (!dst->fshat) |
| 648 | return -ENOMEM; |
| 649 | } |
| 650 | if (src->fsroot) { |
| 651 | dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL); |
| 652 | if (!dst->fsroot) |
| 653 | return -ENOMEM; |
| 654 | } |
| 655 | if (src->fstransmute) { |
| 656 | dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL); |
| 657 | if (!dst->fstransmute) |
| 658 | return -ENOMEM; |
| 659 | } |
| 660 | return 0; |
| 661 | } |
| 662 | |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 663 | static const struct fs_parameter_spec smack_fs_parameters[] = { |
Casey Schaufler | 6e7739f | 2019-05-31 11:53:33 +0100 | [diff] [blame] | 664 | fsparam_string("smackfsdef", Opt_fsdefault), |
| 665 | fsparam_string("smackfsdefault", Opt_fsdefault), |
| 666 | fsparam_string("smackfsfloor", Opt_fsfloor), |
| 667 | fsparam_string("smackfshat", Opt_fshat), |
| 668 | fsparam_string("smackfsroot", Opt_fsroot), |
| 669 | fsparam_string("smackfstransmute", Opt_fstransmute), |
David Howells | 2febd25 | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 670 | {} |
| 671 | }; |
| 672 | |
David Howells | 2febd25 | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 673 | /** |
| 674 | * smack_fs_context_parse_param - Parse a single mount parameter |
| 675 | * @fc: The new filesystem context being constructed. |
| 676 | * @param: The parameter. |
| 677 | * |
| 678 | * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on |
| 679 | * error. |
| 680 | */ |
| 681 | static int smack_fs_context_parse_param(struct fs_context *fc, |
| 682 | struct fs_parameter *param) |
| 683 | { |
| 684 | struct fs_parse_result result; |
| 685 | int opt, rc; |
| 686 | |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 687 | opt = fs_parse(fc, smack_fs_parameters, param, &result); |
David Howells | 2febd25 | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 688 | if (opt < 0) |
| 689 | return opt; |
| 690 | |
| 691 | rc = smack_add_opt(opt, param->string, &fc->security); |
| 692 | if (!rc) |
| 693 | param->string = NULL; |
| 694 | return rc; |
| 695 | } |
| 696 | |
Al Viro | d2497e1 | 2018-12-16 01:37:06 -0500 | [diff] [blame] | 697 | static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts) |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 698 | { |
Al Viro | d2497e1 | 2018-12-16 01:37:06 -0500 | [diff] [blame] | 699 | char *from = options, *to = options; |
| 700 | bool first = true; |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 701 | |
Al Viro | c3300aa | 2018-12-16 01:52:24 -0500 | [diff] [blame] | 702 | while (1) { |
| 703 | char *next = strchr(from, ','); |
| 704 | int token, len, rc; |
| 705 | char *arg = NULL; |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 706 | |
Al Viro | c3300aa | 2018-12-16 01:52:24 -0500 | [diff] [blame] | 707 | if (next) |
| 708 | len = next - from; |
| 709 | else |
| 710 | len = strlen(from); |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 711 | |
Al Viro | c3300aa | 2018-12-16 01:52:24 -0500 | [diff] [blame] | 712 | token = match_opt_prefix(from, len, &arg); |
Al Viro | d2497e1 | 2018-12-16 01:37:06 -0500 | [diff] [blame] | 713 | if (token != Opt_error) { |
| 714 | arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL); |
| 715 | rc = smack_add_opt(token, arg, mnt_opts); |
| 716 | if (unlikely(rc)) { |
| 717 | kfree(arg); |
| 718 | if (*mnt_opts) |
| 719 | smack_free_mnt_opts(*mnt_opts); |
| 720 | *mnt_opts = NULL; |
| 721 | return rc; |
| 722 | } |
| 723 | } else { |
| 724 | if (!first) { // copy with preceding comma |
| 725 | from--; |
| 726 | len++; |
| 727 | } |
| 728 | if (to != from) |
| 729 | memmove(to, from, len); |
| 730 | to += len; |
| 731 | first = false; |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 732 | } |
Al Viro | c3300aa | 2018-12-16 01:52:24 -0500 | [diff] [blame] | 733 | if (!from[len]) |
| 734 | break; |
| 735 | from += len + 1; |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 736 | } |
Al Viro | d2497e1 | 2018-12-16 01:37:06 -0500 | [diff] [blame] | 737 | *to = '\0'; |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 738 | return 0; |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | /** |
| 742 | * smack_set_mnt_opts - set Smack specific mount options |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 743 | * @sb: the file system superblock |
luanshi | a1a07f2 | 2019-07-05 10:35:20 +0800 | [diff] [blame] | 744 | * @mnt_opts: Smack mount options |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 745 | * @kern_flags: mount option from kernel space or user space |
| 746 | * @set_kern_flags: where to store converted mount opts |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 747 | * |
| 748 | * Returns 0 on success, an error code on failure |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 749 | * |
| 750 | * Allow filesystems with binary mount data to explicitly set Smack mount |
| 751 | * labels. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 752 | */ |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 753 | static int smack_set_mnt_opts(struct super_block *sb, |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 754 | void *mnt_opts, |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 755 | unsigned long kern_flags, |
| 756 | unsigned long *set_kern_flags) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 757 | { |
| 758 | struct dentry *root = sb->s_root; |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 759 | struct inode *inode = d_backing_inode(root); |
Casey Schaufler | 1aea780 | 2021-04-22 17:41:15 +0200 | [diff] [blame] | 760 | struct superblock_smack *sp = smack_superblock(sb); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 761 | struct inode_smack *isp; |
Casey Schaufler | 24ea1b6 | 2013-12-30 09:38:00 -0800 | [diff] [blame] | 762 | struct smack_known *skp; |
Al Viro | 12085b1 | 2018-12-13 15:18:05 -0500 | [diff] [blame] | 763 | struct smack_mnt_opts *opts = mnt_opts; |
| 764 | bool transmute = false; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 765 | |
Seth Forshee | 9f50eda | 2015-09-23 15:16:06 -0500 | [diff] [blame] | 766 | if (sp->smk_flags & SMK_SB_INITIALIZED) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 767 | return 0; |
Casey Schaufler | eb982cb | 2012-05-23 17:46:58 -0700 | [diff] [blame] | 768 | |
Casey Schaufler | afb1cbe3 | 2018-09-21 17:19:29 -0700 | [diff] [blame] | 769 | if (inode->i_security == NULL) { |
| 770 | int rc = lsm_inode_alloc(inode); |
| 771 | |
| 772 | if (rc) |
| 773 | return rc; |
| 774 | } |
| 775 | |
Himanshu Shukla | 2097f59 | 2016-11-10 16:19:52 +0530 | [diff] [blame] | 776 | if (!smack_privileged(CAP_MAC_ADMIN)) { |
| 777 | /* |
| 778 | * Unprivileged mounts don't get to specify Smack values. |
| 779 | */ |
Al Viro | 12085b1 | 2018-12-13 15:18:05 -0500 | [diff] [blame] | 780 | if (opts) |
Himanshu Shukla | 2097f59 | 2016-11-10 16:19:52 +0530 | [diff] [blame] | 781 | return -EPERM; |
| 782 | /* |
| 783 | * Unprivileged mounts get root and default from the caller. |
| 784 | */ |
| 785 | skp = smk_of_current(); |
| 786 | sp->smk_root = skp; |
| 787 | sp->smk_default = skp; |
| 788 | /* |
| 789 | * For a handful of fs types with no user-controlled |
| 790 | * backing store it's okay to trust security labels |
| 791 | * in the filesystem. The rest are untrusted. |
| 792 | */ |
| 793 | if (sb->s_user_ns != &init_user_ns && |
| 794 | sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC && |
| 795 | sb->s_magic != RAMFS_MAGIC) { |
Al Viro | 12085b1 | 2018-12-13 15:18:05 -0500 | [diff] [blame] | 796 | transmute = true; |
Himanshu Shukla | 2097f59 | 2016-11-10 16:19:52 +0530 | [diff] [blame] | 797 | sp->smk_flags |= SMK_SB_UNTRUSTED; |
| 798 | } |
| 799 | } |
| 800 | |
Seth Forshee | 9f50eda | 2015-09-23 15:16:06 -0500 | [diff] [blame] | 801 | sp->smk_flags |= SMK_SB_INITIALIZED; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 802 | |
Al Viro | 12085b1 | 2018-12-13 15:18:05 -0500 | [diff] [blame] | 803 | if (opts) { |
| 804 | if (opts->fsdefault) { |
| 805 | skp = smk_import_entry(opts->fsdefault, 0); |
Lukasz Pawelczyk | e774ad6 | 2015-04-20 17:12:54 +0200 | [diff] [blame] | 806 | if (IS_ERR(skp)) |
| 807 | return PTR_ERR(skp); |
| 808 | sp->smk_default = skp; |
Al Viro | 12085b1 | 2018-12-13 15:18:05 -0500 | [diff] [blame] | 809 | } |
| 810 | if (opts->fsfloor) { |
| 811 | skp = smk_import_entry(opts->fsfloor, 0); |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 812 | if (IS_ERR(skp)) |
| 813 | return PTR_ERR(skp); |
| 814 | sp->smk_floor = skp; |
Al Viro | 12085b1 | 2018-12-13 15:18:05 -0500 | [diff] [blame] | 815 | } |
| 816 | if (opts->fshat) { |
| 817 | skp = smk_import_entry(opts->fshat, 0); |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 818 | if (IS_ERR(skp)) |
| 819 | return PTR_ERR(skp); |
| 820 | sp->smk_hat = skp; |
Al Viro | 12085b1 | 2018-12-13 15:18:05 -0500 | [diff] [blame] | 821 | } |
| 822 | if (opts->fsroot) { |
| 823 | skp = smk_import_entry(opts->fsroot, 0); |
Lukasz Pawelczyk | e774ad6 | 2015-04-20 17:12:54 +0200 | [diff] [blame] | 824 | if (IS_ERR(skp)) |
| 825 | return PTR_ERR(skp); |
| 826 | sp->smk_root = skp; |
Al Viro | 12085b1 | 2018-12-13 15:18:05 -0500 | [diff] [blame] | 827 | } |
| 828 | if (opts->fstransmute) { |
| 829 | skp = smk_import_entry(opts->fstransmute, 0); |
Lukasz Pawelczyk | e774ad6 | 2015-04-20 17:12:54 +0200 | [diff] [blame] | 830 | if (IS_ERR(skp)) |
| 831 | return PTR_ERR(skp); |
| 832 | sp->smk_root = skp; |
Al Viro | 12085b1 | 2018-12-13 15:18:05 -0500 | [diff] [blame] | 833 | transmute = true; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | |
| 837 | /* |
| 838 | * Initialize the root inode. |
| 839 | */ |
Casey Schaufler | afb1cbe3 | 2018-09-21 17:19:29 -0700 | [diff] [blame] | 840 | init_inode_smack(inode, sp->smk_root); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 841 | |
Casey Schaufler | afb1cbe3 | 2018-09-21 17:19:29 -0700 | [diff] [blame] | 842 | if (transmute) { |
| 843 | isp = smack_inode(inode); |
Casey Schaufler | e830b39 | 2013-05-22 18:43:07 -0700 | [diff] [blame] | 844 | isp->smk_flags |= SMK_INODE_TRANSMUTE; |
Casey Schaufler | afb1cbe3 | 2018-09-21 17:19:29 -0700 | [diff] [blame] | 845 | } |
Casey Schaufler | e830b39 | 2013-05-22 18:43:07 -0700 | [diff] [blame] | 846 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | /** |
| 851 | * smack_sb_statfs - Smack check on statfs |
| 852 | * @dentry: identifies the file system in question |
| 853 | * |
| 854 | * Returns 0 if current can read the floor of the filesystem, |
| 855 | * and error code otherwise |
| 856 | */ |
| 857 | static int smack_sb_statfs(struct dentry *dentry) |
| 858 | { |
Casey Schaufler | 1aea780 | 2021-04-22 17:41:15 +0200 | [diff] [blame] | 859 | struct superblock_smack *sbp = smack_superblock(dentry->d_sb); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 860 | int rc; |
| 861 | struct smk_audit_info ad; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 862 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 863 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 864 | smk_ad_setfield_u_fs_path_dentry(&ad, dentry); |
| 865 | |
| 866 | rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 867 | rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 868 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 869 | } |
| 870 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 871 | /* |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 872 | * BPRM hooks |
| 873 | */ |
| 874 | |
Casey Schaufler | ce8a432 | 2011-09-29 18:21:01 -0700 | [diff] [blame] | 875 | /** |
Eric W. Biederman | b8bff59 | 2020-03-22 15:46:24 -0500 | [diff] [blame] | 876 | * smack_bprm_creds_for_exec - Update bprm->cred if needed for exec |
Casey Schaufler | ce8a432 | 2011-09-29 18:21:01 -0700 | [diff] [blame] | 877 | * @bprm: the exec information |
| 878 | * |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 879 | * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise |
Casey Schaufler | ce8a432 | 2011-09-29 18:21:01 -0700 | [diff] [blame] | 880 | */ |
Eric W. Biederman | b8bff59 | 2020-03-22 15:46:24 -0500 | [diff] [blame] | 881 | static int smack_bprm_creds_for_exec(struct linux_binprm *bprm) |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 882 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 883 | struct inode *inode = file_inode(bprm->file); |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 884 | struct task_smack *bsp = smack_cred(bprm->cred); |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 885 | struct inode_smack *isp; |
Seth Forshee | 809c02e | 2016-04-26 14:36:22 -0500 | [diff] [blame] | 886 | struct superblock_smack *sbsp; |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 887 | int rc; |
| 888 | |
Casey Schaufler | fb4021b | 2018-11-12 12:43:01 -0800 | [diff] [blame] | 889 | isp = smack_inode(inode); |
Jarkko Sakkinen | 84088ba | 2011-10-07 09:27:53 +0300 | [diff] [blame] | 890 | if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task) |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 891 | return 0; |
| 892 | |
Casey Schaufler | 1aea780 | 2021-04-22 17:41:15 +0200 | [diff] [blame] | 893 | sbsp = smack_superblock(inode->i_sb); |
Seth Forshee | 809c02e | 2016-04-26 14:36:22 -0500 | [diff] [blame] | 894 | if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) && |
| 895 | isp->smk_task != sbsp->smk_root) |
| 896 | return 0; |
| 897 | |
Eric W. Biederman | 9227dd2 | 2017-01-23 17:26:31 +1300 | [diff] [blame] | 898 | if (bprm->unsafe & LSM_UNSAFE_PTRACE) { |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 899 | struct task_struct *tracer; |
| 900 | rc = 0; |
| 901 | |
| 902 | rcu_read_lock(); |
| 903 | tracer = ptrace_parent(current); |
| 904 | if (likely(tracer != NULL)) |
| 905 | rc = smk_ptrace_rule_check(tracer, |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 906 | isp->smk_task, |
Lukasz Pawelczyk | 5663884 | 2014-03-11 17:07:05 +0100 | [diff] [blame] | 907 | PTRACE_MODE_ATTACH, |
| 908 | __func__); |
| 909 | rcu_read_unlock(); |
| 910 | |
| 911 | if (rc != 0) |
| 912 | return rc; |
Jann Horn | 3675f05 | 2019-07-04 20:44:44 +0200 | [diff] [blame] | 913 | } |
| 914 | if (bprm->unsafe & ~LSM_UNSAFE_PTRACE) |
Jarkko Sakkinen | 84088ba | 2011-10-07 09:27:53 +0300 | [diff] [blame] | 915 | return -EPERM; |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 916 | |
Jarkko Sakkinen | 84088ba | 2011-10-07 09:27:53 +0300 | [diff] [blame] | 917 | bsp->smk_task = isp->smk_task; |
| 918 | bprm->per_clear |= PER_CLEAR_ON_SETID; |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 919 | |
Kees Cook | ccbb6e1 | 2017-07-18 15:25:26 -0700 | [diff] [blame] | 920 | /* Decide if this is a secure exec. */ |
| 921 | if (bsp->smk_task != bsp->smk_forked) |
| 922 | bprm->secureexec = 1; |
| 923 | |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | /* |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 928 | * Inode hooks |
| 929 | */ |
| 930 | |
| 931 | /** |
| 932 | * smack_inode_alloc_security - allocate an inode blob |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 933 | * @inode: the inode in need of a blob |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 934 | * |
luanshi | a1a07f2 | 2019-07-05 10:35:20 +0800 | [diff] [blame] | 935 | * Returns 0 |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 936 | */ |
| 937 | static int smack_inode_alloc_security(struct inode *inode) |
| 938 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 939 | struct smack_known *skp = smk_of_current(); |
| 940 | |
Casey Schaufler | afb1cbe3 | 2018-09-21 17:19:29 -0700 | [diff] [blame] | 941 | init_inode_smack(inode, skp); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 942 | return 0; |
| 943 | } |
| 944 | |
| 945 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 946 | * smack_inode_init_security - copy out the smack from an inode |
Lukasz Pawelczyk | e95ef49 | 2014-08-29 17:02:53 +0200 | [diff] [blame] | 947 | * @inode: the newly created inode |
| 948 | * @dir: containing directory object |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 949 | * @qstr: unused |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 950 | * @name: where to put the attribute name |
| 951 | * @value: where to put the attribute value |
| 952 | * @len: where to put the length of the attribute |
| 953 | * |
| 954 | * Returns 0 if it all works out, -ENOMEM if there's no memory |
| 955 | */ |
| 956 | static int smack_inode_init_security(struct inode *inode, struct inode *dir, |
Tetsuo Handa | 9548906 | 2013-07-25 05:44:02 +0900 | [diff] [blame] | 957 | const struct qstr *qstr, const char **name, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 958 | void **value, size_t *len) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 959 | { |
Casey Schaufler | fb4021b | 2018-11-12 12:43:01 -0800 | [diff] [blame] | 960 | struct inode_smack *issp = smack_inode(inode); |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 961 | struct smack_known *skp = smk_of_current(); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 962 | struct smack_known *isp = smk_of_inode(inode); |
| 963 | struct smack_known *dsp = smk_of_inode(dir); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 964 | int may; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 965 | |
Tetsuo Handa | 9548906 | 2013-07-25 05:44:02 +0900 | [diff] [blame] | 966 | if (name) |
| 967 | *name = XATTR_SMACK_SUFFIX; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 968 | |
Lukasz Pawelczyk | 68390cc | 2014-11-26 15:31:07 +0100 | [diff] [blame] | 969 | if (value && len) { |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 970 | rcu_read_lock(); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 971 | may = smk_access_entry(skp->smk_known, dsp->smk_known, |
| 972 | &skp->smk_rules); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 973 | rcu_read_unlock(); |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 974 | |
| 975 | /* |
| 976 | * If the access rule allows transmutation and |
| 977 | * the directory requests transmutation then |
| 978 | * by all means transmute. |
Casey Schaufler | 2267b13 | 2012-03-13 19:14:19 -0700 | [diff] [blame] | 979 | * Mark the inode as changed. |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 980 | */ |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 981 | if (may > 0 && ((may & MAY_TRANSMUTE) != 0) && |
Casey Schaufler | 2267b13 | 2012-03-13 19:14:19 -0700 | [diff] [blame] | 982 | smk_inode_transmutable(dir)) { |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 983 | isp = dsp; |
Casey Schaufler | 2267b13 | 2012-03-13 19:14:19 -0700 | [diff] [blame] | 984 | issp->smk_flags |= SMK_INODE_CHANGED; |
| 985 | } |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 986 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 987 | *value = kstrdup(isp->smk_known, GFP_NOFS); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 988 | if (*value == NULL) |
| 989 | return -ENOMEM; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 990 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 991 | *len = strlen(isp->smk_known); |
Lukasz Pawelczyk | 68390cc | 2014-11-26 15:31:07 +0100 | [diff] [blame] | 992 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 993 | |
| 994 | return 0; |
| 995 | } |
| 996 | |
| 997 | /** |
| 998 | * smack_inode_link - Smack check on link |
| 999 | * @old_dentry: the existing object |
| 1000 | * @dir: unused |
| 1001 | * @new_dentry: the new object |
| 1002 | * |
| 1003 | * Returns 0 if access is permitted, an error code otherwise |
| 1004 | */ |
| 1005 | static int smack_inode_link(struct dentry *old_dentry, struct inode *dir, |
| 1006 | struct dentry *new_dentry) |
| 1007 | { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1008 | struct smack_known *isp; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1009 | struct smk_audit_info ad; |
| 1010 | int rc; |
| 1011 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1012 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1013 | smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1014 | |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1015 | isp = smk_of_inode(d_backing_inode(old_dentry)); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1016 | rc = smk_curacc(isp, MAY_WRITE, &ad); |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1017 | rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1018 | |
David Howells | 8802565 | 2015-01-29 12:02:32 +0000 | [diff] [blame] | 1019 | if (rc == 0 && d_is_positive(new_dentry)) { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1020 | isp = smk_of_inode(d_backing_inode(new_dentry)); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1021 | smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry); |
| 1022 | rc = smk_curacc(isp, MAY_WRITE, &ad); |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1023 | rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | return rc; |
| 1027 | } |
| 1028 | |
| 1029 | /** |
| 1030 | * smack_inode_unlink - Smack check on inode deletion |
| 1031 | * @dir: containing directory object |
| 1032 | * @dentry: file to unlink |
| 1033 | * |
| 1034 | * Returns 0 if current can write the containing directory |
| 1035 | * and the object, error code otherwise |
| 1036 | */ |
| 1037 | static int smack_inode_unlink(struct inode *dir, struct dentry *dentry) |
| 1038 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1039 | struct inode *ip = d_backing_inode(dentry); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1040 | struct smk_audit_info ad; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1041 | int rc; |
| 1042 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1043 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1044 | smk_ad_setfield_u_fs_path_dentry(&ad, dentry); |
| 1045 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1046 | /* |
| 1047 | * You need write access to the thing you're unlinking |
| 1048 | */ |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1049 | rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1050 | rc = smk_bu_inode(ip, MAY_WRITE, rc); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1051 | if (rc == 0) { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1052 | /* |
| 1053 | * You also need write access to the containing directory |
| 1054 | */ |
Igor Zhbanov | cdb56b6 | 2013-03-19 13:49:47 +0400 | [diff] [blame] | 1055 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1056 | smk_ad_setfield_u_fs_inode(&ad, dir); |
| 1057 | rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1058 | rc = smk_bu_inode(dir, MAY_WRITE, rc); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1059 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1060 | return rc; |
| 1061 | } |
| 1062 | |
| 1063 | /** |
| 1064 | * smack_inode_rmdir - Smack check on directory deletion |
| 1065 | * @dir: containing directory object |
| 1066 | * @dentry: directory to unlink |
| 1067 | * |
| 1068 | * Returns 0 if current can write the containing directory |
| 1069 | * and the directory, error code otherwise |
| 1070 | */ |
| 1071 | static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry) |
| 1072 | { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1073 | struct smk_audit_info ad; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1074 | int rc; |
| 1075 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1076 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1077 | smk_ad_setfield_u_fs_path_dentry(&ad, dentry); |
| 1078 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1079 | /* |
| 1080 | * You need write access to the thing you're removing |
| 1081 | */ |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1082 | rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad); |
| 1083 | rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1084 | if (rc == 0) { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1085 | /* |
| 1086 | * You also need write access to the containing directory |
| 1087 | */ |
Igor Zhbanov | cdb56b6 | 2013-03-19 13:49:47 +0400 | [diff] [blame] | 1088 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1089 | smk_ad_setfield_u_fs_inode(&ad, dir); |
| 1090 | rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1091 | rc = smk_bu_inode(dir, MAY_WRITE, rc); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1092 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1093 | |
| 1094 | return rc; |
| 1095 | } |
| 1096 | |
| 1097 | /** |
| 1098 | * smack_inode_rename - Smack check on rename |
Lukasz Pawelczyk | e95ef49 | 2014-08-29 17:02:53 +0200 | [diff] [blame] | 1099 | * @old_inode: unused |
| 1100 | * @old_dentry: the old object |
| 1101 | * @new_inode: unused |
| 1102 | * @new_dentry: the new object |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1103 | * |
| 1104 | * Read and write access is required on both the old and |
| 1105 | * new directories. |
| 1106 | * |
| 1107 | * Returns 0 if access is permitted, an error code otherwise |
| 1108 | */ |
| 1109 | static int smack_inode_rename(struct inode *old_inode, |
| 1110 | struct dentry *old_dentry, |
| 1111 | struct inode *new_inode, |
| 1112 | struct dentry *new_dentry) |
| 1113 | { |
| 1114 | int rc; |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1115 | struct smack_known *isp; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1116 | struct smk_audit_info ad; |
| 1117 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1118 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1119 | smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1120 | |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1121 | isp = smk_of_inode(d_backing_inode(old_dentry)); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1122 | rc = smk_curacc(isp, MAY_READWRITE, &ad); |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1123 | rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1124 | |
David Howells | 8802565 | 2015-01-29 12:02:32 +0000 | [diff] [blame] | 1125 | if (rc == 0 && d_is_positive(new_dentry)) { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1126 | isp = smk_of_inode(d_backing_inode(new_dentry)); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1127 | smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry); |
| 1128 | rc = smk_curacc(isp, MAY_READWRITE, &ad); |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1129 | rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1130 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1131 | return rc; |
| 1132 | } |
| 1133 | |
| 1134 | /** |
| 1135 | * smack_inode_permission - Smack version of permission() |
| 1136 | * @inode: the inode in question |
| 1137 | * @mask: the access requested |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1138 | * |
| 1139 | * This is the important Smack hook. |
| 1140 | * |
luanshi | a1a07f2 | 2019-07-05 10:35:20 +0800 | [diff] [blame] | 1141 | * Returns 0 if access is permitted, an error code otherwise |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1142 | */ |
Al Viro | e74f71e | 2011-06-20 19:38:15 -0400 | [diff] [blame] | 1143 | static int smack_inode_permission(struct inode *inode, int mask) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1144 | { |
Casey Schaufler | 1aea780 | 2021-04-22 17:41:15 +0200 | [diff] [blame] | 1145 | struct superblock_smack *sbsp = smack_superblock(inode->i_sb); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1146 | struct smk_audit_info ad; |
Al Viro | e74f71e | 2011-06-20 19:38:15 -0400 | [diff] [blame] | 1147 | int no_block = mask & MAY_NOT_BLOCK; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1148 | int rc; |
Eric Paris | d09ca73 | 2010-07-23 11:43:57 -0400 | [diff] [blame] | 1149 | |
| 1150 | mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1151 | /* |
| 1152 | * No permission to check. Existence test. Yup, it's there. |
| 1153 | */ |
| 1154 | if (mask == 0) |
| 1155 | return 0; |
Andi Kleen | 8c9e80e | 2011-04-21 17:23:19 -0700 | [diff] [blame] | 1156 | |
Seth Forshee | 9f50eda | 2015-09-23 15:16:06 -0500 | [diff] [blame] | 1157 | if (sbsp->smk_flags & SMK_SB_UNTRUSTED) { |
| 1158 | if (smk_of_inode(inode) != sbsp->smk_root) |
| 1159 | return -EACCES; |
| 1160 | } |
| 1161 | |
Andi Kleen | 8c9e80e | 2011-04-21 17:23:19 -0700 | [diff] [blame] | 1162 | /* May be droppable after audit */ |
Al Viro | e74f71e | 2011-06-20 19:38:15 -0400 | [diff] [blame] | 1163 | if (no_block) |
Andi Kleen | 8c9e80e | 2011-04-21 17:23:19 -0700 | [diff] [blame] | 1164 | return -ECHILD; |
Eric Paris | f48b739 | 2011-04-25 12:54:27 -0400 | [diff] [blame] | 1165 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1166 | smk_ad_setfield_u_fs_inode(&ad, inode); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1167 | rc = smk_curacc(smk_of_inode(inode), mask, &ad); |
| 1168 | rc = smk_bu_inode(inode, mask, rc); |
| 1169 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | /** |
| 1173 | * smack_inode_setattr - Smack check for setting attributes |
| 1174 | * @dentry: the object |
| 1175 | * @iattr: for the force flag |
| 1176 | * |
| 1177 | * Returns 0 if access is permitted, an error code otherwise |
| 1178 | */ |
| 1179 | static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr) |
| 1180 | { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1181 | struct smk_audit_info ad; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1182 | int rc; |
| 1183 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1184 | /* |
| 1185 | * Need to allow for clearing the setuid bit. |
| 1186 | */ |
| 1187 | if (iattr->ia_valid & ATTR_FORCE) |
| 1188 | return 0; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1189 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1190 | smk_ad_setfield_u_fs_path_dentry(&ad, dentry); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1191 | |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1192 | rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad); |
| 1193 | rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1194 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | /** |
| 1198 | * smack_inode_getattr - Smack check for getting attributes |
luanshi | a1a07f2 | 2019-07-05 10:35:20 +0800 | [diff] [blame] | 1199 | * @path: path to extract the info from |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1200 | * |
| 1201 | * Returns 0 if access is permitted, an error code otherwise |
| 1202 | */ |
Al Viro | 3f7036a | 2015-03-08 19:28:30 -0400 | [diff] [blame] | 1203 | static int smack_inode_getattr(const struct path *path) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1204 | { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1205 | struct smk_audit_info ad; |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1206 | struct inode *inode = d_backing_inode(path->dentry); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1207 | int rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1208 | |
Eric Paris | f48b739 | 2011-04-25 12:54:27 -0400 | [diff] [blame] | 1209 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); |
Al Viro | 3f7036a | 2015-03-08 19:28:30 -0400 | [diff] [blame] | 1210 | smk_ad_setfield_u_fs_path(&ad, *path); |
| 1211 | rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad); |
| 1212 | rc = smk_bu_inode(inode, MAY_READ, rc); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1213 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | /** |
| 1217 | * smack_inode_setxattr - Smack check for setting xattrs |
Casey Schaufler | b57d020 | 2021-10-13 14:56:43 -0700 | [diff] [blame] | 1218 | * @mnt_userns: active user namespace |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1219 | * @dentry: the object |
| 1220 | * @name: name of the attribute |
Lukasz Pawelczyk | e95ef49 | 2014-08-29 17:02:53 +0200 | [diff] [blame] | 1221 | * @value: value of the attribute |
| 1222 | * @size: size of the value |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1223 | * @flags: unused |
| 1224 | * |
| 1225 | * This protects the Smack attribute explicitly. |
| 1226 | * |
| 1227 | * Returns 0 if access is permitted, an error code otherwise |
| 1228 | */ |
Christian Brauner | 71bc356 | 2021-01-21 14:19:29 +0100 | [diff] [blame] | 1229 | static int smack_inode_setxattr(struct user_namespace *mnt_userns, |
| 1230 | struct dentry *dentry, const char *name, |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 1231 | const void *value, size_t size, int flags) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1232 | { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1233 | struct smk_audit_info ad; |
Casey Schaufler | 19760ad | 2013-12-16 16:27:26 -0800 | [diff] [blame] | 1234 | struct smack_known *skp; |
| 1235 | int check_priv = 0; |
| 1236 | int check_import = 0; |
| 1237 | int check_star = 0; |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 1238 | int rc = 0; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1239 | |
Casey Schaufler | 19760ad | 2013-12-16 16:27:26 -0800 | [diff] [blame] | 1240 | /* |
| 1241 | * Check label validity here so import won't fail in post_setxattr |
| 1242 | */ |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 1243 | if (strcmp(name, XATTR_NAME_SMACK) == 0 || |
| 1244 | strcmp(name, XATTR_NAME_SMACKIPIN) == 0 || |
Casey Schaufler | 19760ad | 2013-12-16 16:27:26 -0800 | [diff] [blame] | 1245 | strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) { |
| 1246 | check_priv = 1; |
| 1247 | check_import = 1; |
| 1248 | } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 || |
| 1249 | strcmp(name, XATTR_NAME_SMACKMMAP) == 0) { |
| 1250 | check_priv = 1; |
| 1251 | check_import = 1; |
| 1252 | check_star = 1; |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 1253 | } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) { |
Casey Schaufler | 19760ad | 2013-12-16 16:27:26 -0800 | [diff] [blame] | 1254 | check_priv = 1; |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 1255 | if (size != TRANS_TRUE_SIZE || |
| 1256 | strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0) |
| 1257 | rc = -EINVAL; |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 1258 | } else |
| 1259 | rc = cap_inode_setxattr(dentry, name, value, size, flags); |
| 1260 | |
Casey Schaufler | 19760ad | 2013-12-16 16:27:26 -0800 | [diff] [blame] | 1261 | if (check_priv && !smack_privileged(CAP_MAC_ADMIN)) |
| 1262 | rc = -EPERM; |
| 1263 | |
| 1264 | if (rc == 0 && check_import) { |
Konstantin Khlebnikov | b862e56 | 2014-08-07 20:52:43 +0400 | [diff] [blame] | 1265 | skp = size ? smk_import_entry(value, size) : NULL; |
Lukasz Pawelczyk | e774ad6 | 2015-04-20 17:12:54 +0200 | [diff] [blame] | 1266 | if (IS_ERR(skp)) |
| 1267 | rc = PTR_ERR(skp); |
| 1268 | else if (skp == NULL || (check_star && |
Casey Schaufler | 19760ad | 2013-12-16 16:27:26 -0800 | [diff] [blame] | 1269 | (skp == &smack_known_star || skp == &smack_known_web))) |
| 1270 | rc = -EINVAL; |
| 1271 | } |
| 1272 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1273 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1274 | smk_ad_setfield_u_fs_path_dentry(&ad, dentry); |
| 1275 | |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1276 | if (rc == 0) { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1277 | rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad); |
| 1278 | rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1279 | } |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 1280 | |
| 1281 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | /** |
| 1285 | * smack_inode_post_setxattr - Apply the Smack update approved above |
| 1286 | * @dentry: object |
| 1287 | * @name: attribute name |
| 1288 | * @value: attribute value |
| 1289 | * @size: attribute size |
| 1290 | * @flags: unused |
| 1291 | * |
| 1292 | * Set the pointer in the inode blob to the entry found |
| 1293 | * in the master label list. |
| 1294 | */ |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 1295 | static void smack_inode_post_setxattr(struct dentry *dentry, const char *name, |
| 1296 | const void *value, size_t size, int flags) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1297 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1298 | struct smack_known *skp; |
Casey Schaufler | fb4021b | 2018-11-12 12:43:01 -0800 | [diff] [blame] | 1299 | struct inode_smack *isp = smack_inode(d_backing_inode(dentry)); |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1300 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1301 | if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) { |
| 1302 | isp->smk_flags |= SMK_INODE_TRANSMUTE; |
| 1303 | return; |
| 1304 | } |
| 1305 | |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1306 | if (strcmp(name, XATTR_NAME_SMACK) == 0) { |
José Bollo | 9598f4c | 2014-04-03 13:48:41 +0200 | [diff] [blame] | 1307 | skp = smk_import_entry(value, size); |
Lukasz Pawelczyk | e774ad6 | 2015-04-20 17:12:54 +0200 | [diff] [blame] | 1308 | if (!IS_ERR(skp)) |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1309 | isp->smk_inode = skp; |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 1310 | } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) { |
José Bollo | 9598f4c | 2014-04-03 13:48:41 +0200 | [diff] [blame] | 1311 | skp = smk_import_entry(value, size); |
Lukasz Pawelczyk | e774ad6 | 2015-04-20 17:12:54 +0200 | [diff] [blame] | 1312 | if (!IS_ERR(skp)) |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1313 | isp->smk_task = skp; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1314 | } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) { |
José Bollo | 9598f4c | 2014-04-03 13:48:41 +0200 | [diff] [blame] | 1315 | skp = smk_import_entry(value, size); |
Lukasz Pawelczyk | e774ad6 | 2015-04-20 17:12:54 +0200 | [diff] [blame] | 1316 | if (!IS_ERR(skp)) |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1317 | isp->smk_mmap = skp; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1318 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1319 | |
| 1320 | return; |
| 1321 | } |
| 1322 | |
Casey Schaufler | ce8a432 | 2011-09-29 18:21:01 -0700 | [diff] [blame] | 1323 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1324 | * smack_inode_getxattr - Smack check on getxattr |
| 1325 | * @dentry: the object |
| 1326 | * @name: unused |
| 1327 | * |
| 1328 | * Returns 0 if access is permitted, an error code otherwise |
| 1329 | */ |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 1330 | static int smack_inode_getxattr(struct dentry *dentry, const char *name) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1331 | { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1332 | struct smk_audit_info ad; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1333 | int rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1334 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1335 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1336 | smk_ad_setfield_u_fs_path_dentry(&ad, dentry); |
| 1337 | |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1338 | rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad); |
| 1339 | rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1340 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1341 | } |
| 1342 | |
Casey Schaufler | ce8a432 | 2011-09-29 18:21:01 -0700 | [diff] [blame] | 1343 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1344 | * smack_inode_removexattr - Smack check on removexattr |
Casey Schaufler | b57d020 | 2021-10-13 14:56:43 -0700 | [diff] [blame] | 1345 | * @mnt_userns: active user namespace |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1346 | * @dentry: the object |
| 1347 | * @name: name of the attribute |
| 1348 | * |
| 1349 | * Removing the Smack attribute requires CAP_MAC_ADMIN |
| 1350 | * |
| 1351 | * Returns 0 if access is permitted, an error code otherwise |
| 1352 | */ |
Christian Brauner | 71bc356 | 2021-01-21 14:19:29 +0100 | [diff] [blame] | 1353 | static int smack_inode_removexattr(struct user_namespace *mnt_userns, |
| 1354 | struct dentry *dentry, const char *name) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1355 | { |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1356 | struct inode_smack *isp; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1357 | struct smk_audit_info ad; |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 1358 | int rc = 0; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1359 | |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 1360 | if (strcmp(name, XATTR_NAME_SMACK) == 0 || |
| 1361 | strcmp(name, XATTR_NAME_SMACKIPIN) == 0 || |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1362 | strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 || |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 1363 | strcmp(name, XATTR_NAME_SMACKEXEC) == 0 || |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1364 | strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 || |
Pankaj Kumar | 5e9ab59 | 2013-12-13 15:12:22 +0530 | [diff] [blame] | 1365 | strcmp(name, XATTR_NAME_SMACKMMAP) == 0) { |
Casey Schaufler | 1880eff | 2012-06-05 15:28:30 -0700 | [diff] [blame] | 1366 | if (!smack_privileged(CAP_MAC_ADMIN)) |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 1367 | rc = -EPERM; |
| 1368 | } else |
Christian Brauner | 71bc356 | 2021-01-21 14:19:29 +0100 | [diff] [blame] | 1369 | rc = cap_inode_removexattr(mnt_userns, dentry, name); |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 1370 | |
Casey Schaufler | f59bdfb | 2014-04-10 16:35:36 -0700 | [diff] [blame] | 1371 | if (rc != 0) |
| 1372 | return rc; |
| 1373 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1374 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1375 | smk_ad_setfield_u_fs_path_dentry(&ad, dentry); |
Casey Schaufler | bcdca22 | 2008-02-23 15:24:04 -0800 | [diff] [blame] | 1376 | |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1377 | rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad); |
| 1378 | rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc); |
Casey Schaufler | f59bdfb | 2014-04-10 16:35:36 -0700 | [diff] [blame] | 1379 | if (rc != 0) |
| 1380 | return rc; |
| 1381 | |
Casey Schaufler | fb4021b | 2018-11-12 12:43:01 -0800 | [diff] [blame] | 1382 | isp = smack_inode(d_backing_inode(dentry)); |
Casey Schaufler | f59bdfb | 2014-04-10 16:35:36 -0700 | [diff] [blame] | 1383 | /* |
| 1384 | * Don't do anything special for these. |
| 1385 | * XATTR_NAME_SMACKIPIN |
| 1386 | * XATTR_NAME_SMACKIPOUT |
Casey Schaufler | f59bdfb | 2014-04-10 16:35:36 -0700 | [diff] [blame] | 1387 | */ |
José Bollo | 8012495 | 2016-01-12 21:23:40 +0100 | [diff] [blame] | 1388 | if (strcmp(name, XATTR_NAME_SMACK) == 0) { |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 1389 | struct super_block *sbp = dentry->d_sb; |
Casey Schaufler | 1aea780 | 2021-04-22 17:41:15 +0200 | [diff] [blame] | 1390 | struct superblock_smack *sbsp = smack_superblock(sbp); |
José Bollo | 8012495 | 2016-01-12 21:23:40 +0100 | [diff] [blame] | 1391 | |
| 1392 | isp->smk_inode = sbsp->smk_default; |
| 1393 | } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1394 | isp->smk_task = NULL; |
Casey Schaufler | f59bdfb | 2014-04-10 16:35:36 -0700 | [diff] [blame] | 1395 | else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1396 | isp->smk_mmap = NULL; |
Casey Schaufler | f59bdfb | 2014-04-10 16:35:36 -0700 | [diff] [blame] | 1397 | else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) |
| 1398 | isp->smk_flags &= ~SMK_INODE_TRANSMUTE; |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1399 | |
Casey Schaufler | f59bdfb | 2014-04-10 16:35:36 -0700 | [diff] [blame] | 1400 | return 0; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | /** |
| 1404 | * smack_inode_getsecurity - get smack xattrs |
Casey Schaufler | b57d020 | 2021-10-13 14:56:43 -0700 | [diff] [blame] | 1405 | * @mnt_userns: active user namespace |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1406 | * @inode: the object |
| 1407 | * @name: attribute name |
| 1408 | * @buffer: where to put the result |
Casey Schaufler | 57e7ba0 | 2017-09-19 09:39:08 -0700 | [diff] [blame] | 1409 | * @alloc: duplicate memory |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1410 | * |
| 1411 | * Returns the size of the attribute or an error code |
| 1412 | */ |
Christian Brauner | 71bc356 | 2021-01-21 14:19:29 +0100 | [diff] [blame] | 1413 | static int smack_inode_getsecurity(struct user_namespace *mnt_userns, |
| 1414 | struct inode *inode, const char *name, |
| 1415 | void **buffer, bool alloc) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1416 | { |
| 1417 | struct socket_smack *ssp; |
| 1418 | struct socket *sock; |
| 1419 | struct super_block *sbp; |
| 1420 | struct inode *ip = (struct inode *)inode; |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1421 | struct smack_known *isp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1422 | |
Casey Schaufler | 57e7ba0 | 2017-09-19 09:39:08 -0700 | [diff] [blame] | 1423 | if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1424 | isp = smk_of_inode(inode); |
Casey Schaufler | 57e7ba0 | 2017-09-19 09:39:08 -0700 | [diff] [blame] | 1425 | else { |
| 1426 | /* |
| 1427 | * The rest of the Smack xattrs are only on sockets. |
| 1428 | */ |
| 1429 | sbp = ip->i_sb; |
| 1430 | if (sbp->s_magic != SOCKFS_MAGIC) |
| 1431 | return -EOPNOTSUPP; |
| 1432 | |
| 1433 | sock = SOCKET_I(ip); |
| 1434 | if (sock == NULL || sock->sk == NULL) |
| 1435 | return -EOPNOTSUPP; |
| 1436 | |
| 1437 | ssp = sock->sk->sk_security; |
| 1438 | |
| 1439 | if (strcmp(name, XATTR_SMACK_IPIN) == 0) |
| 1440 | isp = ssp->smk_in; |
| 1441 | else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) |
| 1442 | isp = ssp->smk_out; |
| 1443 | else |
| 1444 | return -EOPNOTSUPP; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1445 | } |
| 1446 | |
Casey Schaufler | 57e7ba0 | 2017-09-19 09:39:08 -0700 | [diff] [blame] | 1447 | if (alloc) { |
| 1448 | *buffer = kstrdup(isp->smk_known, GFP_KERNEL); |
| 1449 | if (*buffer == NULL) |
| 1450 | return -ENOMEM; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1451 | } |
| 1452 | |
Casey Schaufler | 57e7ba0 | 2017-09-19 09:39:08 -0700 | [diff] [blame] | 1453 | return strlen(isp->smk_known); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | |
| 1457 | /** |
| 1458 | * smack_inode_listsecurity - list the Smack attributes |
| 1459 | * @inode: the object |
| 1460 | * @buffer: where they go |
| 1461 | * @buffer_size: size of buffer |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1462 | */ |
| 1463 | static int smack_inode_listsecurity(struct inode *inode, char *buffer, |
| 1464 | size_t buffer_size) |
| 1465 | { |
Konstantin Khlebnikov | fd5c9d2 | 2014-08-07 20:52:33 +0400 | [diff] [blame] | 1466 | int len = sizeof(XATTR_NAME_SMACK); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1467 | |
Konstantin Khlebnikov | fd5c9d2 | 2014-08-07 20:52:33 +0400 | [diff] [blame] | 1468 | if (buffer != NULL && len <= buffer_size) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1469 | memcpy(buffer, XATTR_NAME_SMACK, len); |
Konstantin Khlebnikov | fd5c9d2 | 2014-08-07 20:52:33 +0400 | [diff] [blame] | 1470 | |
| 1471 | return len; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1472 | } |
| 1473 | |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 1474 | /** |
| 1475 | * smack_inode_getsecid - Extract inode's security id |
| 1476 | * @inode: inode to extract the info from |
| 1477 | * @secid: where result will be saved |
| 1478 | */ |
Andreas Gruenbacher | d6335d7 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1479 | static void smack_inode_getsecid(struct inode *inode, u32 *secid) |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 1480 | { |
Casey Schaufler | 0f8983c | 2018-06-01 10:45:12 -0700 | [diff] [blame] | 1481 | struct smack_known *skp = smk_of_inode(inode); |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 1482 | |
Casey Schaufler | 0f8983c | 2018-06-01 10:45:12 -0700 | [diff] [blame] | 1483 | *secid = skp->smk_secid; |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 1484 | } |
| 1485 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1486 | /* |
| 1487 | * File Hooks |
| 1488 | */ |
| 1489 | |
Casey Schaufler | 491a0b0 | 2016-01-26 15:08:35 -0800 | [diff] [blame] | 1490 | /* |
| 1491 | * There is no smack_file_permission hook |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1492 | * |
| 1493 | * Should access checks be done on each read or write? |
| 1494 | * UNICOS and SELinux say yes. |
| 1495 | * Trusted Solaris, Trusted Irix, and just about everyone else says no. |
| 1496 | * |
| 1497 | * I'll say no for now. Smack does not do the frequent |
| 1498 | * label changing that SELinux does. |
| 1499 | */ |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1500 | |
| 1501 | /** |
| 1502 | * smack_file_alloc_security - assign a file security blob |
| 1503 | * @file: the object |
| 1504 | * |
| 1505 | * The security blob for a file is a pointer to the master |
| 1506 | * label list, so no allocation is done. |
| 1507 | * |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1508 | * f_security is the owner security information. It |
| 1509 | * isn't used on file access checks, it's for send_sigio. |
| 1510 | * |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1511 | * Returns 0 |
| 1512 | */ |
| 1513 | static int smack_file_alloc_security(struct file *file) |
| 1514 | { |
Casey Schaufler | f28952a | 2018-11-12 09:38:53 -0800 | [diff] [blame] | 1515 | struct smack_known **blob = smack_file(file); |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1516 | |
Casey Schaufler | f28952a | 2018-11-12 09:38:53 -0800 | [diff] [blame] | 1517 | *blob = smk_of_current(); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1518 | return 0; |
| 1519 | } |
| 1520 | |
| 1521 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1522 | * smack_file_ioctl - Smack check on ioctls |
| 1523 | * @file: the object |
| 1524 | * @cmd: what to do |
| 1525 | * @arg: unused |
| 1526 | * |
| 1527 | * Relies heavily on the correct use of the ioctl command conventions. |
| 1528 | * |
| 1529 | * Returns 0 if allowed, error code otherwise |
| 1530 | */ |
| 1531 | static int smack_file_ioctl(struct file *file, unsigned int cmd, |
| 1532 | unsigned long arg) |
| 1533 | { |
| 1534 | int rc = 0; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1535 | struct smk_audit_info ad; |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1536 | struct inode *inode = file_inode(file); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1537 | |
Seung-Woo Kim | 83a1e53 | 2016-12-12 17:35:26 +0900 | [diff] [blame] | 1538 | if (unlikely(IS_PRIVATE(inode))) |
| 1539 | return 0; |
| 1540 | |
Eric Paris | f48b739 | 2011-04-25 12:54:27 -0400 | [diff] [blame] | 1541 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1542 | smk_ad_setfield_u_fs_path(&ad, file->f_path); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1543 | |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1544 | if (_IOC_DIR(cmd) & _IOC_WRITE) { |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1545 | rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1546 | rc = smk_bu_file(file, MAY_WRITE, rc); |
| 1547 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1548 | |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1549 | if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) { |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1550 | rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1551 | rc = smk_bu_file(file, MAY_READ, rc); |
| 1552 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1553 | |
| 1554 | return rc; |
| 1555 | } |
| 1556 | |
| 1557 | /** |
| 1558 | * smack_file_lock - Smack check on file locking |
| 1559 | * @file: the object |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 1560 | * @cmd: unused |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1561 | * |
Casey Schaufler | c0ab6e5 | 2013-10-11 18:06:39 -0700 | [diff] [blame] | 1562 | * Returns 0 if current has lock access, error code otherwise |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1563 | */ |
| 1564 | static int smack_file_lock(struct file *file, unsigned int cmd) |
| 1565 | { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1566 | struct smk_audit_info ad; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1567 | int rc; |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1568 | struct inode *inode = file_inode(file); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1569 | |
Seung-Woo Kim | 83a1e53 | 2016-12-12 17:35:26 +0900 | [diff] [blame] | 1570 | if (unlikely(IS_PRIVATE(inode))) |
| 1571 | return 0; |
| 1572 | |
Eric Paris | 92f4250 | 2011-04-25 13:15:55 -0400 | [diff] [blame] | 1573 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); |
| 1574 | smk_ad_setfield_u_fs_path(&ad, file->f_path); |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1575 | rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1576 | rc = smk_bu_file(file, MAY_LOCK, rc); |
| 1577 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | /** |
| 1581 | * smack_file_fcntl - Smack check on fcntl |
| 1582 | * @file: the object |
| 1583 | * @cmd: what action to check |
| 1584 | * @arg: unused |
| 1585 | * |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1586 | * Generally these operations are harmless. |
| 1587 | * File locking operations present an obvious mechanism |
| 1588 | * for passing information, so they require write access. |
| 1589 | * |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1590 | * Returns 0 if current has access, error code otherwise |
| 1591 | */ |
| 1592 | static int smack_file_fcntl(struct file *file, unsigned int cmd, |
| 1593 | unsigned long arg) |
| 1594 | { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1595 | struct smk_audit_info ad; |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1596 | int rc = 0; |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1597 | struct inode *inode = file_inode(file); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1598 | |
Seung-Woo Kim | 83a1e53 | 2016-12-12 17:35:26 +0900 | [diff] [blame] | 1599 | if (unlikely(IS_PRIVATE(inode))) |
| 1600 | return 0; |
| 1601 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1602 | switch (cmd) { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1603 | case F_GETLK: |
Casey Schaufler | c0ab6e5 | 2013-10-11 18:06:39 -0700 | [diff] [blame] | 1604 | break; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1605 | case F_SETLK: |
| 1606 | case F_SETLKW: |
Casey Schaufler | c0ab6e5 | 2013-10-11 18:06:39 -0700 | [diff] [blame] | 1607 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); |
| 1608 | smk_ad_setfield_u_fs_path(&ad, file->f_path); |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1609 | rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1610 | rc = smk_bu_file(file, MAY_LOCK, rc); |
Casey Schaufler | c0ab6e5 | 2013-10-11 18:06:39 -0700 | [diff] [blame] | 1611 | break; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1612 | case F_SETOWN: |
| 1613 | case F_SETSIG: |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1614 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); |
| 1615 | smk_ad_setfield_u_fs_path(&ad, file->f_path); |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1616 | rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1617 | rc = smk_bu_file(file, MAY_WRITE, rc); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1618 | break; |
| 1619 | default: |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1620 | break; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | return rc; |
| 1624 | } |
| 1625 | |
| 1626 | /** |
Casey Schaufler | b57d020 | 2021-10-13 14:56:43 -0700 | [diff] [blame] | 1627 | * smack_mmap_file - Check permissions for a mmap operation. |
| 1628 | * @file: contains the file structure for file to map (may be NULL). |
| 1629 | * @reqprot: contains the protection requested by the application. |
| 1630 | * @prot: contains the protection that will be applied by the kernel. |
| 1631 | * @flags: contains the operational flags. |
| 1632 | * |
| 1633 | * The @file may be NULL, e.g. if mapping anonymous memory. |
| 1634 | * |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1635 | * Return 0 if permission is granted. |
| 1636 | */ |
Al Viro | e546785 | 2012-05-30 13:30:51 -0400 | [diff] [blame] | 1637 | static int smack_mmap_file(struct file *file, |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1638 | unsigned long reqprot, unsigned long prot, |
Al Viro | e546785 | 2012-05-30 13:30:51 -0400 | [diff] [blame] | 1639 | unsigned long flags) |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1640 | { |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 1641 | struct smack_known *skp; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1642 | struct smack_known *mkp; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1643 | struct smack_rule *srp; |
| 1644 | struct task_smack *tsp; |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1645 | struct smack_known *okp; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1646 | struct inode_smack *isp; |
Seth Forshee | 809c02e | 2016-04-26 14:36:22 -0500 | [diff] [blame] | 1647 | struct superblock_smack *sbsp; |
Casey Schaufler | 0e0a070 | 2011-02-08 16:36:24 -0800 | [diff] [blame] | 1648 | int may; |
| 1649 | int mmay; |
| 1650 | int tmay; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1651 | int rc; |
| 1652 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1653 | if (file == NULL) |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1654 | return 0; |
| 1655 | |
Seung-Woo Kim | 83a1e53 | 2016-12-12 17:35:26 +0900 | [diff] [blame] | 1656 | if (unlikely(IS_PRIVATE(file_inode(file)))) |
| 1657 | return 0; |
| 1658 | |
Casey Schaufler | fb4021b | 2018-11-12 12:43:01 -0800 | [diff] [blame] | 1659 | isp = smack_inode(file_inode(file)); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1660 | if (isp->smk_mmap == NULL) |
| 1661 | return 0; |
Casey Schaufler | 1aea780 | 2021-04-22 17:41:15 +0200 | [diff] [blame] | 1662 | sbsp = smack_superblock(file_inode(file)->i_sb); |
Seth Forshee | 809c02e | 2016-04-26 14:36:22 -0500 | [diff] [blame] | 1663 | if (sbsp->smk_flags & SMK_SB_UNTRUSTED && |
| 1664 | isp->smk_mmap != sbsp->smk_root) |
| 1665 | return -EACCES; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1666 | mkp = isp->smk_mmap; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1667 | |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 1668 | tsp = smack_cred(current_cred()); |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1669 | skp = smk_of_current(); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1670 | rc = 0; |
| 1671 | |
| 1672 | rcu_read_lock(); |
| 1673 | /* |
| 1674 | * For each Smack rule associated with the subject |
| 1675 | * label verify that the SMACK64MMAP also has access |
| 1676 | * to that rule's object label. |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1677 | */ |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 1678 | list_for_each_entry_rcu(srp, &skp->smk_rules, list) { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1679 | okp = srp->smk_object; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1680 | /* |
| 1681 | * Matching labels always allows access. |
| 1682 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1683 | if (mkp->smk_known == okp->smk_known) |
Casey Schaufler | 0e0a070 | 2011-02-08 16:36:24 -0800 | [diff] [blame] | 1684 | continue; |
| 1685 | /* |
| 1686 | * If there is a matching local rule take |
| 1687 | * that into account as well. |
| 1688 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1689 | may = smk_access_entry(srp->smk_subject->smk_known, |
| 1690 | okp->smk_known, |
| 1691 | &tsp->smk_rules); |
Casey Schaufler | 0e0a070 | 2011-02-08 16:36:24 -0800 | [diff] [blame] | 1692 | if (may == -ENOENT) |
| 1693 | may = srp->smk_access; |
| 1694 | else |
| 1695 | may &= srp->smk_access; |
| 1696 | /* |
| 1697 | * If may is zero the SMACK64MMAP subject can't |
| 1698 | * possibly have less access. |
| 1699 | */ |
| 1700 | if (may == 0) |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1701 | continue; |
| 1702 | |
Casey Schaufler | 0e0a070 | 2011-02-08 16:36:24 -0800 | [diff] [blame] | 1703 | /* |
| 1704 | * Fetch the global list entry. |
| 1705 | * If there isn't one a SMACK64MMAP subject |
| 1706 | * can't have as much access as current. |
| 1707 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1708 | mmay = smk_access_entry(mkp->smk_known, okp->smk_known, |
| 1709 | &mkp->smk_rules); |
Casey Schaufler | 0e0a070 | 2011-02-08 16:36:24 -0800 | [diff] [blame] | 1710 | if (mmay == -ENOENT) { |
| 1711 | rc = -EACCES; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1712 | break; |
Casey Schaufler | 0e0a070 | 2011-02-08 16:36:24 -0800 | [diff] [blame] | 1713 | } |
| 1714 | /* |
| 1715 | * If there is a local entry it modifies the |
| 1716 | * potential access, too. |
| 1717 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 1718 | tmay = smk_access_entry(mkp->smk_known, okp->smk_known, |
| 1719 | &tsp->smk_rules); |
Casey Schaufler | 0e0a070 | 2011-02-08 16:36:24 -0800 | [diff] [blame] | 1720 | if (tmay != -ENOENT) |
| 1721 | mmay &= tmay; |
| 1722 | |
| 1723 | /* |
| 1724 | * If there is any access available to current that is |
| 1725 | * not available to a SMACK64MMAP subject |
| 1726 | * deny access. |
| 1727 | */ |
Casey Schaufler | 75a2563 | 2011-02-09 19:58:42 -0800 | [diff] [blame] | 1728 | if ((may | mmay) != mmay) { |
Casey Schaufler | 0e0a070 | 2011-02-08 16:36:24 -0800 | [diff] [blame] | 1729 | rc = -EACCES; |
| 1730 | break; |
| 1731 | } |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | rcu_read_unlock(); |
| 1735 | |
| 1736 | return rc; |
| 1737 | } |
| 1738 | |
| 1739 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1740 | * smack_file_set_fowner - set the file security blob value |
| 1741 | * @file: object in question |
| 1742 | * |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1743 | */ |
Jeff Layton | e0b93ed | 2014-08-22 11:27:32 -0400 | [diff] [blame] | 1744 | static void smack_file_set_fowner(struct file *file) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1745 | { |
Casey Schaufler | f28952a | 2018-11-12 09:38:53 -0800 | [diff] [blame] | 1746 | struct smack_known **blob = smack_file(file); |
| 1747 | |
| 1748 | *blob = smk_of_current(); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | /** |
| 1752 | * smack_file_send_sigiotask - Smack on sigio |
| 1753 | * @tsk: The target task |
| 1754 | * @fown: the object the signal come from |
| 1755 | * @signum: unused |
| 1756 | * |
| 1757 | * Allow a privileged task to get signals even if it shouldn't |
| 1758 | * |
| 1759 | * Returns 0 if a subject with the object's smack could |
| 1760 | * write to the task, an error code otherwise. |
| 1761 | */ |
| 1762 | static int smack_file_send_sigiotask(struct task_struct *tsk, |
| 1763 | struct fown_struct *fown, int signum) |
| 1764 | { |
Casey Schaufler | f28952a | 2018-11-12 09:38:53 -0800 | [diff] [blame] | 1765 | struct smack_known **blob; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1766 | struct smack_known *skp; |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 1767 | struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred)); |
Casey Schaufler | dcb569c | 2018-09-18 16:09:16 -0700 | [diff] [blame] | 1768 | const struct cred *tcred; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1769 | struct file *file; |
| 1770 | int rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1771 | struct smk_audit_info ad; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1772 | |
| 1773 | /* |
| 1774 | * struct fown_struct is never outside the context of a struct file |
| 1775 | */ |
| 1776 | file = container_of(fown, struct file, f_owner); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1777 | |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1778 | /* we don't log here as rc can be overriden */ |
Casey Schaufler | f28952a | 2018-11-12 09:38:53 -0800 | [diff] [blame] | 1779 | blob = smack_file(file); |
| 1780 | skp = *blob; |
Casey Schaufler | c60b906 | 2016-08-30 10:31:39 -0700 | [diff] [blame] | 1781 | rc = smk_access(skp, tkp, MAY_DELIVER, NULL); |
| 1782 | rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc); |
Casey Schaufler | dcb569c | 2018-09-18 16:09:16 -0700 | [diff] [blame] | 1783 | |
| 1784 | rcu_read_lock(); |
| 1785 | tcred = __task_cred(tsk); |
| 1786 | if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred)) |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1787 | rc = 0; |
Casey Schaufler | dcb569c | 2018-09-18 16:09:16 -0700 | [diff] [blame] | 1788 | rcu_read_unlock(); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1789 | |
| 1790 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK); |
| 1791 | smk_ad_setfield_u_tsk(&ad, tsk); |
Casey Schaufler | c60b906 | 2016-08-30 10:31:39 -0700 | [diff] [blame] | 1792 | smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1793 | return rc; |
| 1794 | } |
| 1795 | |
| 1796 | /** |
| 1797 | * smack_file_receive - Smack file receive check |
| 1798 | * @file: the object |
| 1799 | * |
| 1800 | * Returns 0 if current has access, error code otherwise |
| 1801 | */ |
| 1802 | static int smack_file_receive(struct file *file) |
| 1803 | { |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1804 | int rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1805 | int may = 0; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1806 | struct smk_audit_info ad; |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1807 | struct inode *inode = file_inode(file); |
Casey Schaufler | 79be093 | 2015-12-07 14:34:32 -0800 | [diff] [blame] | 1808 | struct socket *sock; |
| 1809 | struct task_smack *tsp; |
| 1810 | struct socket_smack *ssp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1811 | |
Seung-Woo Kim | 9777582 | 2015-04-17 15:25:04 +0900 | [diff] [blame] | 1812 | if (unlikely(IS_PRIVATE(inode))) |
| 1813 | return 0; |
| 1814 | |
Casey Schaufler | 4482a44 | 2013-12-30 17:37:45 -0800 | [diff] [blame] | 1815 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1816 | smk_ad_setfield_u_fs_path(&ad, file->f_path); |
Casey Schaufler | 79be093 | 2015-12-07 14:34:32 -0800 | [diff] [blame] | 1817 | |
Casey Schaufler | 51d59af | 2017-05-31 08:53:42 -0700 | [diff] [blame] | 1818 | if (inode->i_sb->s_magic == SOCKFS_MAGIC) { |
Casey Schaufler | 79be093 | 2015-12-07 14:34:32 -0800 | [diff] [blame] | 1819 | sock = SOCKET_I(inode); |
| 1820 | ssp = sock->sk->sk_security; |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 1821 | tsp = smack_cred(current_cred()); |
Casey Schaufler | 79be093 | 2015-12-07 14:34:32 -0800 | [diff] [blame] | 1822 | /* |
| 1823 | * If the receiving process can't write to the |
| 1824 | * passed socket or if the passed socket can't |
| 1825 | * write to the receiving process don't accept |
| 1826 | * the passed socket. |
| 1827 | */ |
| 1828 | rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad); |
| 1829 | rc = smk_bu_file(file, may, rc); |
| 1830 | if (rc < 0) |
| 1831 | return rc; |
| 1832 | rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad); |
| 1833 | rc = smk_bu_file(file, may, rc); |
| 1834 | return rc; |
| 1835 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1836 | /* |
| 1837 | * This code relies on bitmasks. |
| 1838 | */ |
| 1839 | if (file->f_mode & FMODE_READ) |
| 1840 | may = MAY_READ; |
| 1841 | if (file->f_mode & FMODE_WRITE) |
| 1842 | may |= MAY_WRITE; |
| 1843 | |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1844 | rc = smk_curacc(smk_of_inode(inode), may, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 1845 | rc = smk_bu_file(file, may, rc); |
| 1846 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1847 | } |
| 1848 | |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1849 | /** |
Eric Paris | 83d4985 | 2012-04-04 13:45:40 -0400 | [diff] [blame] | 1850 | * smack_file_open - Smack dentry open processing |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1851 | * @file: the object |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1852 | * |
| 1853 | * Set the security blob in the file structure. |
Casey Schaufler | a6834c0 | 2014-04-21 11:10:26 -0700 | [diff] [blame] | 1854 | * Allow the open only if the task has read access. There are |
| 1855 | * many read operations (e.g. fstat) that you can do with an |
| 1856 | * fd even if you have the file open write-only. |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1857 | * |
luanshi | a1a07f2 | 2019-07-05 10:35:20 +0800 | [diff] [blame] | 1858 | * Returns 0 if current has access, error code otherwise |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1859 | */ |
Al Viro | 9481769 | 2018-07-10 14:13:18 -0400 | [diff] [blame] | 1860 | static int smack_file_open(struct file *file) |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1861 | { |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 1862 | struct task_smack *tsp = smack_cred(file->f_cred); |
Casey Schaufler | 5e7270a | 2014-12-12 17:19:19 -0800 | [diff] [blame] | 1863 | struct inode *inode = file_inode(file); |
Casey Schaufler | a6834c0 | 2014-04-21 11:10:26 -0700 | [diff] [blame] | 1864 | struct smk_audit_info ad; |
| 1865 | int rc; |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1866 | |
Casey Schaufler | a6834c0 | 2014-04-21 11:10:26 -0700 | [diff] [blame] | 1867 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); |
| 1868 | smk_ad_setfield_u_fs_path(&ad, file->f_path); |
Himanshu Shukla | c9d238a | 2016-11-23 11:59:45 +0530 | [diff] [blame] | 1869 | rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad); |
Al Viro | 9481769 | 2018-07-10 14:13:18 -0400 | [diff] [blame] | 1870 | rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc); |
Casey Schaufler | a6834c0 | 2014-04-21 11:10:26 -0700 | [diff] [blame] | 1871 | |
| 1872 | return rc; |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 1873 | } |
| 1874 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1875 | /* |
| 1876 | * Task hooks |
| 1877 | */ |
| 1878 | |
| 1879 | /** |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1880 | * smack_cred_alloc_blank - "allocate" blank task-level security credentials |
luanshi | a1a07f2 | 2019-07-05 10:35:20 +0800 | [diff] [blame] | 1881 | * @cred: the new credentials |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1882 | * @gfp: the atomicity of any memory allocations |
| 1883 | * |
| 1884 | * Prepare a blank set of credentials for modification. This must allocate all |
| 1885 | * the memory the LSM module might require such that cred_transfer() can |
| 1886 | * complete without error. |
| 1887 | */ |
| 1888 | static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp) |
| 1889 | { |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 1890 | init_task_smack(smack_cred(cred), NULL, NULL); |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1891 | return 0; |
| 1892 | } |
| 1893 | |
| 1894 | |
| 1895 | /** |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 1896 | * smack_cred_free - "free" task-level security credentials |
| 1897 | * @cred: the credentials in question |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1898 | * |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1899 | */ |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 1900 | static void smack_cred_free(struct cred *cred) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1901 | { |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 1902 | struct task_smack *tsp = smack_cred(cred); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1903 | struct smack_rule *rp; |
| 1904 | struct list_head *l; |
| 1905 | struct list_head *n; |
| 1906 | |
Zbigniew Jasinski | 38416e5 | 2015-10-19 18:23:53 +0200 | [diff] [blame] | 1907 | smk_destroy_label_list(&tsp->smk_relabel); |
| 1908 | |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1909 | list_for_each_safe(l, n, &tsp->smk_rules) { |
| 1910 | rp = list_entry(l, struct smack_rule, list); |
| 1911 | list_del(&rp->list); |
Casey Schaufler | 4e328b0 | 2019-04-02 11:37:12 -0700 | [diff] [blame] | 1912 | kmem_cache_free(smack_rule_cache, rp); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1913 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | /** |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1917 | * smack_cred_prepare - prepare new set of credentials for modification |
| 1918 | * @new: the new credentials |
| 1919 | * @old: the original credentials |
| 1920 | * @gfp: the atomicity of any memory allocations |
| 1921 | * |
| 1922 | * Prepare a new set of credentials for modification. |
| 1923 | */ |
| 1924 | static int smack_cred_prepare(struct cred *new, const struct cred *old, |
| 1925 | gfp_t gfp) |
| 1926 | { |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 1927 | struct task_smack *old_tsp = smack_cred(old); |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 1928 | struct task_smack *new_tsp = smack_cred(new); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1929 | int rc; |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1930 | |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 1931 | init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task); |
Himanshu Shukla | b437aba | 2016-11-10 16:17:02 +0530 | [diff] [blame] | 1932 | |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1933 | rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp); |
| 1934 | if (rc != 0) |
| 1935 | return rc; |
| 1936 | |
Zbigniew Jasinski | 38416e5 | 2015-10-19 18:23:53 +0200 | [diff] [blame] | 1937 | rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel, |
| 1938 | gfp); |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 1939 | return rc; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1940 | } |
| 1941 | |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 1942 | /** |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1943 | * smack_cred_transfer - Transfer the old credentials to the new credentials |
| 1944 | * @new: the new credentials |
| 1945 | * @old: the original credentials |
| 1946 | * |
| 1947 | * Fill in a set of blank credentials from another set of credentials. |
| 1948 | */ |
| 1949 | static void smack_cred_transfer(struct cred *new, const struct cred *old) |
| 1950 | { |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 1951 | struct task_smack *old_tsp = smack_cred(old); |
| 1952 | struct task_smack *new_tsp = smack_cred(new); |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1953 | |
| 1954 | new_tsp->smk_task = old_tsp->smk_task; |
| 1955 | new_tsp->smk_forked = old_tsp->smk_task; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1956 | mutex_init(&new_tsp->smk_rules_lock); |
| 1957 | INIT_LIST_HEAD(&new_tsp->smk_rules); |
| 1958 | |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1959 | /* cbs copy rule list */ |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | /** |
Matthew Garrett | 3ec3011 | 2018-01-08 13:36:19 -0800 | [diff] [blame] | 1963 | * smack_cred_getsecid - get the secid corresponding to a creds structure |
luanshi | a1a07f2 | 2019-07-05 10:35:20 +0800 | [diff] [blame] | 1964 | * @cred: the object creds |
Matthew Garrett | 3ec3011 | 2018-01-08 13:36:19 -0800 | [diff] [blame] | 1965 | * @secid: where to put the result |
| 1966 | * |
| 1967 | * Sets the secid to contain a u32 version of the smack label. |
| 1968 | */ |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 1969 | static void smack_cred_getsecid(const struct cred *cred, u32 *secid) |
Matthew Garrett | 3ec3011 | 2018-01-08 13:36:19 -0800 | [diff] [blame] | 1970 | { |
| 1971 | struct smack_known *skp; |
| 1972 | |
| 1973 | rcu_read_lock(); |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 1974 | skp = smk_of_task(smack_cred(cred)); |
Matthew Garrett | 3ec3011 | 2018-01-08 13:36:19 -0800 | [diff] [blame] | 1975 | *secid = skp->smk_secid; |
| 1976 | rcu_read_unlock(); |
| 1977 | } |
| 1978 | |
| 1979 | /** |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 1980 | * smack_kernel_act_as - Set the subjective context in a set of credentials |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 1981 | * @new: points to the set of credentials to be modified. |
| 1982 | * @secid: specifies the security ID to be set |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 1983 | * |
| 1984 | * Set the security data for a kernel service. |
| 1985 | */ |
| 1986 | static int smack_kernel_act_as(struct cred *new, u32 secid) |
| 1987 | { |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 1988 | struct task_smack *new_tsp = smack_cred(new); |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 1989 | |
Casey Schaufler | 152f91d | 2016-11-14 09:38:15 -0800 | [diff] [blame] | 1990 | new_tsp->smk_task = smack_from_secid(secid); |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 1991 | return 0; |
| 1992 | } |
| 1993 | |
| 1994 | /** |
| 1995 | * smack_kernel_create_files_as - Set the file creation label in a set of creds |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 1996 | * @new: points to the set of credentials to be modified |
| 1997 | * @inode: points to the inode to use as a reference |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 1998 | * |
| 1999 | * Set the file creation context in a set of credentials to the same |
| 2000 | * as the objective context of the specified inode |
| 2001 | */ |
| 2002 | static int smack_kernel_create_files_as(struct cred *new, |
| 2003 | struct inode *inode) |
| 2004 | { |
Casey Schaufler | fb4021b | 2018-11-12 12:43:01 -0800 | [diff] [blame] | 2005 | struct inode_smack *isp = smack_inode(inode); |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 2006 | struct task_smack *tsp = smack_cred(new); |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 2007 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2008 | tsp->smk_forked = isp->smk_inode; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2009 | tsp->smk_task = tsp->smk_forked; |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 2010 | return 0; |
| 2011 | } |
| 2012 | |
| 2013 | /** |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2014 | * smk_curacc_on_task - helper to log task related access |
| 2015 | * @p: the task object |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 2016 | * @access: the access requested |
| 2017 | * @caller: name of the calling function for audit |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2018 | * |
| 2019 | * Return 0 if access is permitted |
| 2020 | */ |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 2021 | static int smk_curacc_on_task(struct task_struct *p, int access, |
| 2022 | const char *caller) |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2023 | { |
| 2024 | struct smk_audit_info ad; |
Paul Moore | a3727a8 | 2021-09-23 09:50:11 -0400 | [diff] [blame] | 2025 | struct smack_known *skp = smk_of_task_struct_obj(p); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 2026 | int rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2027 | |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 2028 | smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2029 | smk_ad_setfield_u_tsk(&ad, p); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2030 | rc = smk_curacc(skp, access, &ad); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 2031 | rc = smk_bu_task(p, access, rc); |
| 2032 | return rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2036 | * smack_task_setpgid - Smack check on setting pgid |
| 2037 | * @p: the task object |
| 2038 | * @pgid: unused |
| 2039 | * |
| 2040 | * Return 0 if write access is permitted |
| 2041 | */ |
| 2042 | static int smack_task_setpgid(struct task_struct *p, pid_t pgid) |
| 2043 | { |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 2044 | return smk_curacc_on_task(p, MAY_WRITE, __func__); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2045 | } |
| 2046 | |
| 2047 | /** |
| 2048 | * smack_task_getpgid - Smack access check for getpgid |
| 2049 | * @p: the object task |
| 2050 | * |
| 2051 | * Returns 0 if current can read the object task, error code otherwise |
| 2052 | */ |
| 2053 | static int smack_task_getpgid(struct task_struct *p) |
| 2054 | { |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 2055 | return smk_curacc_on_task(p, MAY_READ, __func__); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | /** |
| 2059 | * smack_task_getsid - Smack access check for getsid |
| 2060 | * @p: the object task |
| 2061 | * |
| 2062 | * Returns 0 if current can read the object task, error code otherwise |
| 2063 | */ |
| 2064 | static int smack_task_getsid(struct task_struct *p) |
| 2065 | { |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 2066 | return smk_curacc_on_task(p, MAY_READ, __func__); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | /** |
Paul Moore | 6326948 | 2021-09-29 11:01:21 -0400 | [diff] [blame] | 2070 | * smack_current_getsecid_subj - get the subjective secid of the current task |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2071 | * @secid: where to put the result |
| 2072 | * |
Paul Moore | 1fb057d | 2021-02-19 15:04:58 -0500 | [diff] [blame] | 2073 | * Sets the secid to contain a u32 version of the task's subjective smack label. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2074 | */ |
Paul Moore | 6326948 | 2021-09-29 11:01:21 -0400 | [diff] [blame] | 2075 | static void smack_current_getsecid_subj(u32 *secid) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2076 | { |
Paul Moore | 6326948 | 2021-09-29 11:01:21 -0400 | [diff] [blame] | 2077 | struct smack_known *skp = smk_of_current(); |
Paul Moore | 1fb057d | 2021-02-19 15:04:58 -0500 | [diff] [blame] | 2078 | |
| 2079 | *secid = skp->smk_secid; |
| 2080 | } |
| 2081 | |
| 2082 | /** |
| 2083 | * smack_task_getsecid_obj - get the objective secid of the task |
| 2084 | * @p: the task |
| 2085 | * @secid: where to put the result |
| 2086 | * |
| 2087 | * Sets the secid to contain a u32 version of the task's objective smack label. |
| 2088 | */ |
| 2089 | static void smack_task_getsecid_obj(struct task_struct *p, u32 *secid) |
| 2090 | { |
| 2091 | struct smack_known *skp = smk_of_task_struct_obj(p); |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2092 | |
| 2093 | *secid = skp->smk_secid; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | /** |
| 2097 | * smack_task_setnice - Smack check on setting nice |
| 2098 | * @p: the task object |
| 2099 | * @nice: unused |
| 2100 | * |
| 2101 | * Return 0 if write access is permitted |
| 2102 | */ |
| 2103 | static int smack_task_setnice(struct task_struct *p, int nice) |
| 2104 | { |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 2105 | return smk_curacc_on_task(p, MAY_WRITE, __func__); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | /** |
| 2109 | * smack_task_setioprio - Smack check on setting ioprio |
| 2110 | * @p: the task object |
| 2111 | * @ioprio: unused |
| 2112 | * |
| 2113 | * Return 0 if write access is permitted |
| 2114 | */ |
| 2115 | static int smack_task_setioprio(struct task_struct *p, int ioprio) |
| 2116 | { |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 2117 | return smk_curacc_on_task(p, MAY_WRITE, __func__); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2118 | } |
| 2119 | |
| 2120 | /** |
| 2121 | * smack_task_getioprio - Smack check on reading ioprio |
| 2122 | * @p: the task object |
| 2123 | * |
| 2124 | * Return 0 if read access is permitted |
| 2125 | */ |
| 2126 | static int smack_task_getioprio(struct task_struct *p) |
| 2127 | { |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 2128 | return smk_curacc_on_task(p, MAY_READ, __func__); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | /** |
| 2132 | * smack_task_setscheduler - Smack check on setting scheduler |
| 2133 | * @p: the task object |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2134 | * |
| 2135 | * Return 0 if read access is permitted |
| 2136 | */ |
KOSAKI Motohiro | b0ae198 | 2010-10-15 04:21:18 +0900 | [diff] [blame] | 2137 | static int smack_task_setscheduler(struct task_struct *p) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2138 | { |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 2139 | return smk_curacc_on_task(p, MAY_WRITE, __func__); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | /** |
| 2143 | * smack_task_getscheduler - Smack check on reading scheduler |
| 2144 | * @p: the task object |
| 2145 | * |
| 2146 | * Return 0 if read access is permitted |
| 2147 | */ |
| 2148 | static int smack_task_getscheduler(struct task_struct *p) |
| 2149 | { |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 2150 | return smk_curacc_on_task(p, MAY_READ, __func__); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2151 | } |
| 2152 | |
| 2153 | /** |
| 2154 | * smack_task_movememory - Smack check on moving memory |
| 2155 | * @p: the task object |
| 2156 | * |
| 2157 | * Return 0 if write access is permitted |
| 2158 | */ |
| 2159 | static int smack_task_movememory(struct task_struct *p) |
| 2160 | { |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 2161 | return smk_curacc_on_task(p, MAY_WRITE, __func__); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2162 | } |
| 2163 | |
| 2164 | /** |
| 2165 | * smack_task_kill - Smack check on signal delivery |
| 2166 | * @p: the task object |
| 2167 | * @info: unused |
| 2168 | * @sig: unused |
Stephen Smalley | 6b4f3d0 | 2017-09-08 12:40:01 -0400 | [diff] [blame] | 2169 | * @cred: identifies the cred to use in lieu of current's |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2170 | * |
| 2171 | * Return 0 if write access is permitted |
| 2172 | * |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2173 | */ |
Eric W. Biederman | ae7795b | 2018-09-25 11:27:20 +0200 | [diff] [blame] | 2174 | static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info, |
Stephen Smalley | 6b4f3d0 | 2017-09-08 12:40:01 -0400 | [diff] [blame] | 2175 | int sig, const struct cred *cred) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2176 | { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2177 | struct smk_audit_info ad; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2178 | struct smack_known *skp; |
Paul Moore | 1fb057d | 2021-02-19 15:04:58 -0500 | [diff] [blame] | 2179 | struct smack_known *tkp = smk_of_task_struct_obj(p); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 2180 | int rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2181 | |
Rafal Krypa | 18d872f | 2016-04-04 11:14:53 +0200 | [diff] [blame] | 2182 | if (!sig) |
| 2183 | return 0; /* null signal; existence test */ |
| 2184 | |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2185 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK); |
| 2186 | smk_ad_setfield_u_tsk(&ad, p); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2187 | /* |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2188 | * Sending a signal requires that the sender |
| 2189 | * can write the receiver. |
| 2190 | */ |
Stephen Smalley | 6b4f3d0 | 2017-09-08 12:40:01 -0400 | [diff] [blame] | 2191 | if (cred == NULL) { |
Casey Schaufler | c60b906 | 2016-08-30 10:31:39 -0700 | [diff] [blame] | 2192 | rc = smk_curacc(tkp, MAY_DELIVER, &ad); |
| 2193 | rc = smk_bu_task(p, MAY_DELIVER, rc); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 2194 | return rc; |
| 2195 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2196 | /* |
Stephen Smalley | 6b4f3d0 | 2017-09-08 12:40:01 -0400 | [diff] [blame] | 2197 | * If the cred isn't NULL we're dealing with some USB IO |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2198 | * specific behavior. This is not clean. For one thing |
| 2199 | * we can't take privilege into account. |
| 2200 | */ |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 2201 | skp = smk_of_task(smack_cred(cred)); |
Casey Schaufler | c60b906 | 2016-08-30 10:31:39 -0700 | [diff] [blame] | 2202 | rc = smk_access(skp, tkp, MAY_DELIVER, &ad); |
| 2203 | rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 2204 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2205 | } |
| 2206 | |
| 2207 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2208 | * smack_task_to_inode - copy task smack into the inode blob |
| 2209 | * @p: task to copy from |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 2210 | * @inode: inode to copy to |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2211 | * |
| 2212 | * Sets the smack pointer in the inode security blob |
| 2213 | */ |
| 2214 | static void smack_task_to_inode(struct task_struct *p, struct inode *inode) |
| 2215 | { |
Casey Schaufler | fb4021b | 2018-11-12 12:43:01 -0800 | [diff] [blame] | 2216 | struct inode_smack *isp = smack_inode(inode); |
Paul Moore | 1fb057d | 2021-02-19 15:04:58 -0500 | [diff] [blame] | 2217 | struct smack_known *skp = smk_of_task_struct_obj(p); |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2218 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2219 | isp->smk_inode = skp; |
Casey Schaufler | 7b4e884 | 2018-06-22 10:54:45 -0700 | [diff] [blame] | 2220 | isp->smk_flags |= SMK_INODE_INSTANT; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | /* |
| 2224 | * Socket hooks. |
| 2225 | */ |
| 2226 | |
| 2227 | /** |
| 2228 | * smack_sk_alloc_security - Allocate a socket blob |
| 2229 | * @sk: the socket |
| 2230 | * @family: unused |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 2231 | * @gfp_flags: memory allocation flags |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2232 | * |
| 2233 | * Assign Smack pointers to current |
| 2234 | * |
| 2235 | * Returns 0 on success, -ENOMEM is there's no memory |
| 2236 | */ |
| 2237 | static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags) |
| 2238 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2239 | struct smack_known *skp = smk_of_current(); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2240 | struct socket_smack *ssp; |
| 2241 | |
| 2242 | ssp = kzalloc(sizeof(struct socket_smack), gfp_flags); |
| 2243 | if (ssp == NULL) |
| 2244 | return -ENOMEM; |
| 2245 | |
jooseong lee | 08382c9 | 2016-11-03 11:54:39 +0100 | [diff] [blame] | 2246 | /* |
| 2247 | * Sockets created by kernel threads receive web label. |
| 2248 | */ |
| 2249 | if (unlikely(current->flags & PF_KTHREAD)) { |
| 2250 | ssp->smk_in = &smack_known_web; |
| 2251 | ssp->smk_out = &smack_known_web; |
| 2252 | } else { |
| 2253 | ssp->smk_in = skp; |
| 2254 | ssp->smk_out = skp; |
| 2255 | } |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 2256 | ssp->smk_packet = NULL; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2257 | |
| 2258 | sk->sk_security = ssp; |
| 2259 | |
| 2260 | return 0; |
| 2261 | } |
| 2262 | |
| 2263 | /** |
| 2264 | * smack_sk_free_security - Free a socket blob |
| 2265 | * @sk: the socket |
| 2266 | * |
| 2267 | * Clears the blob pointer |
| 2268 | */ |
| 2269 | static void smack_sk_free_security(struct sock *sk) |
| 2270 | { |
Vishal Goel | 0c96d1f | 2016-11-23 10:32:54 +0530 | [diff] [blame] | 2271 | #ifdef SMACK_IPV6_PORT_LABELING |
| 2272 | struct smk_port_label *spp; |
| 2273 | |
| 2274 | if (sk->sk_family == PF_INET6) { |
| 2275 | rcu_read_lock(); |
| 2276 | list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) { |
| 2277 | if (spp->smk_sock != sk) |
| 2278 | continue; |
| 2279 | spp->smk_can_reuse = 1; |
| 2280 | break; |
| 2281 | } |
| 2282 | rcu_read_unlock(); |
| 2283 | } |
| 2284 | #endif |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2285 | kfree(sk->sk_security); |
| 2286 | } |
| 2287 | |
| 2288 | /** |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2289 | * smack_ipv4host_label - check host based restrictions |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2290 | * @sip: the object end |
| 2291 | * |
| 2292 | * looks for host based access restrictions |
| 2293 | * |
| 2294 | * This version will only be appropriate for really small sets of single label |
| 2295 | * hosts. The caller is responsible for ensuring that the RCU read lock is |
| 2296 | * taken before calling this function. |
| 2297 | * |
| 2298 | * Returns the label of the far end or NULL if it's not special. |
| 2299 | */ |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2300 | static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip) |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2301 | { |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2302 | struct smk_net4addr *snp; |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2303 | struct in_addr *siap = &sip->sin_addr; |
| 2304 | |
| 2305 | if (siap->s_addr == 0) |
| 2306 | return NULL; |
| 2307 | |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2308 | list_for_each_entry_rcu(snp, &smk_net4addr_list, list) |
| 2309 | /* |
| 2310 | * we break after finding the first match because |
| 2311 | * the list is sorted from longest to shortest mask |
| 2312 | * so we have found the most specific match |
| 2313 | */ |
| 2314 | if (snp->smk_host.s_addr == |
| 2315 | (siap->s_addr & snp->smk_mask.s_addr)) |
| 2316 | return snp->smk_label; |
| 2317 | |
| 2318 | return NULL; |
| 2319 | } |
| 2320 | |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2321 | /* |
| 2322 | * smk_ipv6_localhost - Check for local ipv6 host address |
| 2323 | * @sip: the address |
| 2324 | * |
| 2325 | * Returns boolean true if this is the localhost address |
| 2326 | */ |
| 2327 | static bool smk_ipv6_localhost(struct sockaddr_in6 *sip) |
| 2328 | { |
| 2329 | __be16 *be16p = (__be16 *)&sip->sin6_addr; |
| 2330 | __be32 *be32p = (__be32 *)&sip->sin6_addr; |
| 2331 | |
| 2332 | if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 && |
| 2333 | ntohs(be16p[7]) == 1) |
| 2334 | return true; |
| 2335 | return false; |
| 2336 | } |
| 2337 | |
| 2338 | /** |
| 2339 | * smack_ipv6host_label - check host based restrictions |
| 2340 | * @sip: the object end |
| 2341 | * |
| 2342 | * looks for host based access restrictions |
| 2343 | * |
| 2344 | * This version will only be appropriate for really small sets of single label |
| 2345 | * hosts. The caller is responsible for ensuring that the RCU read lock is |
| 2346 | * taken before calling this function. |
| 2347 | * |
| 2348 | * Returns the label of the far end or NULL if it's not special. |
| 2349 | */ |
| 2350 | static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip) |
| 2351 | { |
| 2352 | struct smk_net6addr *snp; |
| 2353 | struct in6_addr *sap = &sip->sin6_addr; |
| 2354 | int i; |
| 2355 | int found = 0; |
| 2356 | |
| 2357 | /* |
| 2358 | * It's local. Don't look for a host label. |
| 2359 | */ |
| 2360 | if (smk_ipv6_localhost(sip)) |
| 2361 | return NULL; |
| 2362 | |
| 2363 | list_for_each_entry_rcu(snp, &smk_net6addr_list, list) { |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2364 | /* |
Casey Schaufler | 2e4939f | 2016-11-07 19:01:09 -0800 | [diff] [blame] | 2365 | * If the label is NULL the entry has |
| 2366 | * been renounced. Ignore it. |
| 2367 | */ |
| 2368 | if (snp->smk_label == NULL) |
| 2369 | continue; |
| 2370 | /* |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2371 | * we break after finding the first match because |
| 2372 | * the list is sorted from longest to shortest mask |
| 2373 | * so we have found the most specific match |
| 2374 | */ |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2375 | for (found = 1, i = 0; i < 8; i++) { |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2376 | if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) != |
| 2377 | snp->smk_host.s6_addr16[i]) { |
| 2378 | found = 0; |
| 2379 | break; |
| 2380 | } |
Etienne Basset | 4303154 | 2009-03-27 17:11:01 -0400 | [diff] [blame] | 2381 | } |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2382 | if (found) |
| 2383 | return snp->smk_label; |
| 2384 | } |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2385 | |
| 2386 | return NULL; |
| 2387 | } |
| 2388 | |
| 2389 | /** |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 2390 | * smack_netlbl_add - Set the secattr on a socket |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2391 | * @sk: the socket |
| 2392 | * |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 2393 | * Attach the outbound smack value (smk_out) to the socket. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2394 | * |
| 2395 | * Returns 0 on success or an error code |
| 2396 | */ |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 2397 | static int smack_netlbl_add(struct sock *sk) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2398 | { |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2399 | struct socket_smack *ssp = sk->sk_security; |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 2400 | struct smack_known *skp = ssp->smk_out; |
| 2401 | int rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2402 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 2403 | local_bh_disable(); |
| 2404 | bh_lock_sock_nested(sk); |
| 2405 | |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 2406 | rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel); |
| 2407 | switch (rc) { |
| 2408 | case 0: |
| 2409 | ssp->smk_state = SMK_NETLBL_LABELED; |
| 2410 | break; |
| 2411 | case -EDESTADDRREQ: |
| 2412 | ssp->smk_state = SMK_NETLBL_REQSKB; |
| 2413 | rc = 0; |
| 2414 | break; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 2415 | } |
| 2416 | |
| 2417 | bh_unlock_sock(sk); |
| 2418 | local_bh_enable(); |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 2419 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2420 | return rc; |
| 2421 | } |
| 2422 | |
| 2423 | /** |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 2424 | * smack_netlbl_delete - Remove the secattr from a socket |
| 2425 | * @sk: the socket |
| 2426 | * |
| 2427 | * Remove the outbound smack value from a socket |
| 2428 | */ |
| 2429 | static void smack_netlbl_delete(struct sock *sk) |
| 2430 | { |
| 2431 | struct socket_smack *ssp = sk->sk_security; |
| 2432 | |
| 2433 | /* |
| 2434 | * Take the label off the socket if one is set. |
| 2435 | */ |
| 2436 | if (ssp->smk_state != SMK_NETLBL_LABELED) |
| 2437 | return; |
| 2438 | |
| 2439 | local_bh_disable(); |
| 2440 | bh_lock_sock_nested(sk); |
| 2441 | netlbl_sock_delattr(sk); |
| 2442 | bh_unlock_sock(sk); |
| 2443 | local_bh_enable(); |
| 2444 | ssp->smk_state = SMK_NETLBL_UNLABELED; |
| 2445 | } |
| 2446 | |
| 2447 | /** |
| 2448 | * smk_ipv4_check - Perform IPv4 host access checks |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2449 | * @sk: the socket |
| 2450 | * @sap: the destination address |
| 2451 | * |
| 2452 | * Set the correct secattr for the given socket based on the destination |
| 2453 | * address and perform any outbound access checks needed. |
| 2454 | * |
| 2455 | * Returns 0 on success or an error code. |
| 2456 | * |
| 2457 | */ |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 2458 | static int smk_ipv4_check(struct sock *sk, struct sockaddr_in *sap) |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2459 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2460 | struct smack_known *skp; |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 2461 | int rc = 0; |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2462 | struct smack_known *hkp; |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2463 | struct socket_smack *ssp = sk->sk_security; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2464 | struct smk_audit_info ad; |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2465 | |
| 2466 | rcu_read_lock(); |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2467 | hkp = smack_ipv4host_label(sap); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2468 | if (hkp != NULL) { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2469 | #ifdef CONFIG_AUDIT |
Kees Cook | 923e9a1 | 2012-04-10 13:26:44 -0700 | [diff] [blame] | 2470 | struct lsm_network_audit net; |
| 2471 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 2472 | smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); |
| 2473 | ad.a.u.net->family = sap->sin_family; |
| 2474 | ad.a.u.net->dport = sap->sin_port; |
| 2475 | ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2476 | #endif |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2477 | skp = ssp->smk_out; |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2478 | rc = smk_access(skp, hkp, MAY_WRITE, &ad); |
| 2479 | rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc); |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 2480 | /* |
| 2481 | * Clear the socket netlabel if it's set. |
| 2482 | */ |
| 2483 | if (!rc) |
| 2484 | smack_netlbl_delete(sk); |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2485 | } |
| 2486 | rcu_read_unlock(); |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2487 | |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 2488 | return rc; |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2489 | } |
| 2490 | |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2491 | /** |
| 2492 | * smk_ipv6_check - check Smack access |
| 2493 | * @subject: subject Smack label |
| 2494 | * @object: object Smack label |
| 2495 | * @address: address |
| 2496 | * @act: the action being taken |
| 2497 | * |
| 2498 | * Check an IPv6 access |
| 2499 | */ |
| 2500 | static int smk_ipv6_check(struct smack_known *subject, |
| 2501 | struct smack_known *object, |
| 2502 | struct sockaddr_in6 *address, int act) |
| 2503 | { |
| 2504 | #ifdef CONFIG_AUDIT |
| 2505 | struct lsm_network_audit net; |
| 2506 | #endif |
| 2507 | struct smk_audit_info ad; |
| 2508 | int rc; |
| 2509 | |
| 2510 | #ifdef CONFIG_AUDIT |
| 2511 | smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); |
| 2512 | ad.a.u.net->family = PF_INET6; |
| 2513 | ad.a.u.net->dport = ntohs(address->sin6_port); |
| 2514 | if (act == SMK_RECEIVING) |
| 2515 | ad.a.u.net->v6info.saddr = address->sin6_addr; |
| 2516 | else |
| 2517 | ad.a.u.net->v6info.daddr = address->sin6_addr; |
| 2518 | #endif |
| 2519 | rc = smk_access(subject, object, MAY_WRITE, &ad); |
| 2520 | rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc); |
| 2521 | return rc; |
| 2522 | } |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2523 | |
| 2524 | #ifdef SMACK_IPV6_PORT_LABELING |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 2525 | /** |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2526 | * smk_ipv6_port_label - Smack port access table management |
| 2527 | * @sock: socket |
| 2528 | * @address: address |
| 2529 | * |
| 2530 | * Create or update the port list entry |
| 2531 | */ |
| 2532 | static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address) |
| 2533 | { |
| 2534 | struct sock *sk = sock->sk; |
| 2535 | struct sockaddr_in6 *addr6; |
| 2536 | struct socket_smack *ssp = sock->sk->sk_security; |
| 2537 | struct smk_port_label *spp; |
| 2538 | unsigned short port = 0; |
| 2539 | |
| 2540 | if (address == NULL) { |
| 2541 | /* |
| 2542 | * This operation is changing the Smack information |
| 2543 | * on the bound socket. Take the changes to the port |
| 2544 | * as well. |
| 2545 | */ |
Vishal Goel | 3c7ce34 | 2016-11-23 10:31:08 +0530 | [diff] [blame] | 2546 | rcu_read_lock(); |
| 2547 | list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) { |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2548 | if (sk != spp->smk_sock) |
| 2549 | continue; |
| 2550 | spp->smk_in = ssp->smk_in; |
| 2551 | spp->smk_out = ssp->smk_out; |
Vishal Goel | 3c7ce34 | 2016-11-23 10:31:08 +0530 | [diff] [blame] | 2552 | rcu_read_unlock(); |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2553 | return; |
| 2554 | } |
| 2555 | /* |
| 2556 | * A NULL address is only used for updating existing |
| 2557 | * bound entries. If there isn't one, it's OK. |
| 2558 | */ |
Vishal Goel | 3c7ce34 | 2016-11-23 10:31:08 +0530 | [diff] [blame] | 2559 | rcu_read_unlock(); |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2560 | return; |
| 2561 | } |
| 2562 | |
| 2563 | addr6 = (struct sockaddr_in6 *)address; |
| 2564 | port = ntohs(addr6->sin6_port); |
| 2565 | /* |
| 2566 | * This is a special case that is safely ignored. |
| 2567 | */ |
| 2568 | if (port == 0) |
| 2569 | return; |
| 2570 | |
| 2571 | /* |
| 2572 | * Look for an existing port list entry. |
| 2573 | * This is an indication that a port is getting reused. |
| 2574 | */ |
Vishal Goel | 3c7ce34 | 2016-11-23 10:31:08 +0530 | [diff] [blame] | 2575 | rcu_read_lock(); |
| 2576 | list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) { |
Vishal Goel | 9d44c97 | 2016-11-23 10:31:59 +0530 | [diff] [blame] | 2577 | if (spp->smk_port != port || spp->smk_sock_type != sock->type) |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2578 | continue; |
Vishal Goel | 0c96d1f | 2016-11-23 10:32:54 +0530 | [diff] [blame] | 2579 | if (spp->smk_can_reuse != 1) { |
| 2580 | rcu_read_unlock(); |
| 2581 | return; |
| 2582 | } |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2583 | spp->smk_port = port; |
| 2584 | spp->smk_sock = sk; |
| 2585 | spp->smk_in = ssp->smk_in; |
| 2586 | spp->smk_out = ssp->smk_out; |
Vishal Goel | 0c96d1f | 2016-11-23 10:32:54 +0530 | [diff] [blame] | 2587 | spp->smk_can_reuse = 0; |
Vishal Goel | 3c7ce34 | 2016-11-23 10:31:08 +0530 | [diff] [blame] | 2588 | rcu_read_unlock(); |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2589 | return; |
| 2590 | } |
Vishal Goel | 3c7ce34 | 2016-11-23 10:31:08 +0530 | [diff] [blame] | 2591 | rcu_read_unlock(); |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2592 | /* |
| 2593 | * A new port entry is required. |
| 2594 | */ |
| 2595 | spp = kzalloc(sizeof(*spp), GFP_KERNEL); |
| 2596 | if (spp == NULL) |
| 2597 | return; |
| 2598 | |
| 2599 | spp->smk_port = port; |
| 2600 | spp->smk_sock = sk; |
| 2601 | spp->smk_in = ssp->smk_in; |
| 2602 | spp->smk_out = ssp->smk_out; |
Vishal Goel | 9d44c97 | 2016-11-23 10:31:59 +0530 | [diff] [blame] | 2603 | spp->smk_sock_type = sock->type; |
Vishal Goel | 0c96d1f | 2016-11-23 10:32:54 +0530 | [diff] [blame] | 2604 | spp->smk_can_reuse = 0; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2605 | |
Vishal Goel | 3c7ce34 | 2016-11-23 10:31:08 +0530 | [diff] [blame] | 2606 | mutex_lock(&smack_ipv6_lock); |
| 2607 | list_add_rcu(&spp->list, &smk_ipv6_port_list); |
| 2608 | mutex_unlock(&smack_ipv6_lock); |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2609 | return; |
| 2610 | } |
| 2611 | |
| 2612 | /** |
| 2613 | * smk_ipv6_port_check - check Smack port access |
luanshi | a1a07f2 | 2019-07-05 10:35:20 +0800 | [diff] [blame] | 2614 | * @sk: socket |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2615 | * @address: address |
luanshi | a1a07f2 | 2019-07-05 10:35:20 +0800 | [diff] [blame] | 2616 | * @act: the action being taken |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2617 | * |
| 2618 | * Create or update the port list entry |
| 2619 | */ |
Casey Schaufler | 6ea0624 | 2013-08-05 13:21:22 -0700 | [diff] [blame] | 2620 | static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address, |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2621 | int act) |
| 2622 | { |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2623 | struct smk_port_label *spp; |
| 2624 | struct socket_smack *ssp = sk->sk_security; |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2625 | struct smack_known *skp = NULL; |
| 2626 | unsigned short port; |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2627 | struct smack_known *object; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2628 | |
| 2629 | if (act == SMK_RECEIVING) { |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2630 | skp = smack_ipv6host_label(address); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2631 | object = ssp->smk_in; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2632 | } else { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2633 | skp = ssp->smk_out; |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2634 | object = smack_ipv6host_label(address); |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2635 | } |
| 2636 | |
| 2637 | /* |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2638 | * The other end is a single label host. |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2639 | */ |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2640 | if (skp != NULL && object != NULL) |
| 2641 | return smk_ipv6_check(skp, object, address, act); |
| 2642 | if (skp == NULL) |
| 2643 | skp = smack_net_ambient; |
| 2644 | if (object == NULL) |
| 2645 | object = smack_net_ambient; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2646 | |
| 2647 | /* |
| 2648 | * It's remote, so port lookup does no good. |
| 2649 | */ |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2650 | if (!smk_ipv6_localhost(address)) |
| 2651 | return smk_ipv6_check(skp, object, address, act); |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2652 | |
| 2653 | /* |
| 2654 | * It's local so the send check has to have passed. |
| 2655 | */ |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2656 | if (act == SMK_RECEIVING) |
| 2657 | return 0; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2658 | |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2659 | port = ntohs(address->sin6_port); |
Vishal Goel | 3c7ce34 | 2016-11-23 10:31:08 +0530 | [diff] [blame] | 2660 | rcu_read_lock(); |
| 2661 | list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) { |
Vishal Goel | 9d44c97 | 2016-11-23 10:31:59 +0530 | [diff] [blame] | 2662 | if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type) |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2663 | continue; |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2664 | object = spp->smk_in; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2665 | if (act == SMK_CONNECTING) |
Casey Schaufler | 54e70ec | 2014-04-10 16:37:08 -0700 | [diff] [blame] | 2666 | ssp->smk_packet = spp->smk_out; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2667 | break; |
| 2668 | } |
Vishal Goel | 3c7ce34 | 2016-11-23 10:31:08 +0530 | [diff] [blame] | 2669 | rcu_read_unlock(); |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2670 | |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2671 | return smk_ipv6_check(skp, object, address, act); |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2672 | } |
Sebastian Andrzej Siewior | 222a96b | 2021-09-23 19:05:03 +0200 | [diff] [blame] | 2673 | #endif |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2674 | |
| 2675 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2676 | * smack_inode_setsecurity - set smack xattrs |
| 2677 | * @inode: the object |
| 2678 | * @name: attribute name |
| 2679 | * @value: attribute value |
| 2680 | * @size: size of the attribute |
| 2681 | * @flags: unused |
| 2682 | * |
| 2683 | * Sets the named attribute in the appropriate blob |
| 2684 | * |
| 2685 | * Returns 0 on success, or an error code |
| 2686 | */ |
| 2687 | static int smack_inode_setsecurity(struct inode *inode, const char *name, |
| 2688 | const void *value, size_t size, int flags) |
| 2689 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2690 | struct smack_known *skp; |
Casey Schaufler | fb4021b | 2018-11-12 12:43:01 -0800 | [diff] [blame] | 2691 | struct inode_smack *nsp = smack_inode(inode); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2692 | struct socket_smack *ssp; |
| 2693 | struct socket *sock; |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 2694 | int rc = 0; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2695 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 2696 | if (value == NULL || size > SMK_LONGLABEL || size == 0) |
Pankaj Kumar | 5e9ab59 | 2013-12-13 15:12:22 +0530 | [diff] [blame] | 2697 | return -EINVAL; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2698 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2699 | skp = smk_import_entry(value, size); |
Lukasz Pawelczyk | e774ad6 | 2015-04-20 17:12:54 +0200 | [diff] [blame] | 2700 | if (IS_ERR(skp)) |
| 2701 | return PTR_ERR(skp); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2702 | |
| 2703 | if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 2704 | nsp->smk_inode = skp; |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 2705 | nsp->smk_flags |= SMK_INODE_INSTANT; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2706 | return 0; |
| 2707 | } |
| 2708 | /* |
| 2709 | * The rest of the Smack xattrs are only on sockets. |
| 2710 | */ |
| 2711 | if (inode->i_sb->s_magic != SOCKFS_MAGIC) |
| 2712 | return -EOPNOTSUPP; |
| 2713 | |
| 2714 | sock = SOCKET_I(inode); |
Ahmed S. Darwish | 2e1d146 | 2008-02-13 15:03:34 -0800 | [diff] [blame] | 2715 | if (sock == NULL || sock->sk == NULL) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2716 | return -EOPNOTSUPP; |
| 2717 | |
| 2718 | ssp = sock->sk->sk_security; |
| 2719 | |
| 2720 | if (strcmp(name, XATTR_SMACK_IPIN) == 0) |
Casey Schaufler | 54e70ec | 2014-04-10 16:37:08 -0700 | [diff] [blame] | 2721 | ssp->smk_in = skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2722 | else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2723 | ssp->smk_out = skp; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2724 | if (sock->sk->sk_family == PF_INET) { |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 2725 | rc = smack_netlbl_add(sock->sk); |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 2726 | if (rc != 0) |
| 2727 | printk(KERN_WARNING |
| 2728 | "Smack: \"%s\" netlbl error %d.\n", |
| 2729 | __func__, -rc); |
| 2730 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2731 | } else |
| 2732 | return -EOPNOTSUPP; |
| 2733 | |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2734 | #ifdef SMACK_IPV6_PORT_LABELING |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2735 | if (sock->sk->sk_family == PF_INET6) |
| 2736 | smk_ipv6_port_label(sock, NULL); |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2737 | #endif |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2738 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2739 | return 0; |
| 2740 | } |
| 2741 | |
| 2742 | /** |
| 2743 | * smack_socket_post_create - finish socket setup |
| 2744 | * @sock: the socket |
| 2745 | * @family: protocol family |
| 2746 | * @type: unused |
| 2747 | * @protocol: unused |
| 2748 | * @kern: unused |
| 2749 | * |
| 2750 | * Sets the netlabel information on the socket |
| 2751 | * |
| 2752 | * Returns 0 on success, and error code otherwise |
| 2753 | */ |
| 2754 | static int smack_socket_post_create(struct socket *sock, int family, |
| 2755 | int type, int protocol, int kern) |
| 2756 | { |
Marcin Lis | 7412301 | 2015-01-22 15:40:33 +0100 | [diff] [blame] | 2757 | struct socket_smack *ssp; |
| 2758 | |
| 2759 | if (sock->sk == NULL) |
| 2760 | return 0; |
| 2761 | |
| 2762 | /* |
| 2763 | * Sockets created by kernel threads receive web label. |
| 2764 | */ |
| 2765 | if (unlikely(current->flags & PF_KTHREAD)) { |
| 2766 | ssp = sock->sk->sk_security; |
| 2767 | ssp->smk_in = &smack_known_web; |
| 2768 | ssp->smk_out = &smack_known_web; |
| 2769 | } |
| 2770 | |
| 2771 | if (family != PF_INET) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2772 | return 0; |
| 2773 | /* |
| 2774 | * Set the outbound netlbl. |
| 2775 | */ |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 2776 | return smack_netlbl_add(sock->sk); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 2777 | } |
| 2778 | |
Tom Gundersen | 5859cdf | 2018-05-04 16:28:22 +0200 | [diff] [blame] | 2779 | /** |
| 2780 | * smack_socket_socketpair - create socket pair |
| 2781 | * @socka: one socket |
| 2782 | * @sockb: another socket |
| 2783 | * |
| 2784 | * Cross reference the peer labels for SO_PEERSEC |
| 2785 | * |
luanshi | a1a07f2 | 2019-07-05 10:35:20 +0800 | [diff] [blame] | 2786 | * Returns 0 |
Tom Gundersen | 5859cdf | 2018-05-04 16:28:22 +0200 | [diff] [blame] | 2787 | */ |
| 2788 | static int smack_socket_socketpair(struct socket *socka, |
| 2789 | struct socket *sockb) |
| 2790 | { |
| 2791 | struct socket_smack *asp = socka->sk->sk_security; |
| 2792 | struct socket_smack *bsp = sockb->sk->sk_security; |
| 2793 | |
| 2794 | asp->smk_packet = bsp->smk_out; |
| 2795 | bsp->smk_packet = asp->smk_out; |
| 2796 | |
| 2797 | return 0; |
| 2798 | } |
| 2799 | |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2800 | #ifdef SMACK_IPV6_PORT_LABELING |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 2801 | /** |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2802 | * smack_socket_bind - record port binding information. |
| 2803 | * @sock: the socket |
| 2804 | * @address: the port address |
| 2805 | * @addrlen: size of the address |
| 2806 | * |
| 2807 | * Records the label bound to a port. |
| 2808 | * |
Tetsuo Handa | b9ef551 | 2019-04-12 19:59:35 +0900 | [diff] [blame] | 2809 | * Returns 0 on success, and error code otherwise |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2810 | */ |
| 2811 | static int smack_socket_bind(struct socket *sock, struct sockaddr *address, |
| 2812 | int addrlen) |
| 2813 | { |
Tetsuo Handa | b9ef551 | 2019-04-12 19:59:35 +0900 | [diff] [blame] | 2814 | if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) { |
| 2815 | if (addrlen < SIN6_LEN_RFC2133 || |
| 2816 | address->sa_family != AF_INET6) |
| 2817 | return -EINVAL; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2818 | smk_ipv6_port_label(sock, address); |
Tetsuo Handa | b9ef551 | 2019-04-12 19:59:35 +0900 | [diff] [blame] | 2819 | } |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2820 | return 0; |
| 2821 | } |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2822 | #endif /* SMACK_IPV6_PORT_LABELING */ |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2823 | |
| 2824 | /** |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 2825 | * smack_socket_connect - connect access check |
| 2826 | * @sock: the socket |
| 2827 | * @sap: the other end |
| 2828 | * @addrlen: size of sap |
| 2829 | * |
| 2830 | * Verifies that a connection may be possible |
| 2831 | * |
| 2832 | * Returns 0 on success, and error code otherwise |
| 2833 | */ |
| 2834 | static int smack_socket_connect(struct socket *sock, struct sockaddr *sap, |
| 2835 | int addrlen) |
| 2836 | { |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2837 | int rc = 0; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 2838 | |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2839 | if (sock->sk == NULL) |
| 2840 | return 0; |
Casey Schaufler | 87fbfff | 2020-02-03 09:15:00 -0800 | [diff] [blame] | 2841 | if (sock->sk->sk_family != PF_INET && |
| 2842 | (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6)) |
| 2843 | return 0; |
| 2844 | if (addrlen < offsetofend(struct sockaddr, sa_family)) |
| 2845 | return 0; |
| 2846 | if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) { |
| 2847 | struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap; |
Arnd Bergmann | 00720f0 | 2020-04-08 21:04:31 +0200 | [diff] [blame] | 2848 | struct smack_known *rsp = NULL; |
Vasyl Gomonovych | da49b5d | 2017-12-21 16:57:52 +0100 | [diff] [blame] | 2849 | |
Casey Schaufler | 87fbfff | 2020-02-03 09:15:00 -0800 | [diff] [blame] | 2850 | if (addrlen < SIN6_LEN_RFC2133) |
| 2851 | return 0; |
Arnd Bergmann | 00720f0 | 2020-04-08 21:04:31 +0200 | [diff] [blame] | 2852 | if (__is_defined(SMACK_IPV6_SECMARK_LABELING)) |
| 2853 | rsp = smack_ipv6host_label(sip); |
Casey Schaufler | 87fbfff | 2020-02-03 09:15:00 -0800 | [diff] [blame] | 2854 | if (rsp != NULL) { |
| 2855 | struct socket_smack *ssp = sock->sk->sk_security; |
| 2856 | |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 2857 | rc = smk_ipv6_check(ssp->smk_out, rsp, sip, |
Casey Schaufler | 87fbfff | 2020-02-03 09:15:00 -0800 | [diff] [blame] | 2858 | SMK_CONNECTING); |
| 2859 | } |
Sebastian Andrzej Siewior | 222a96b | 2021-09-23 19:05:03 +0200 | [diff] [blame] | 2860 | #ifdef SMACK_IPV6_PORT_LABELING |
| 2861 | rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING); |
| 2862 | #endif |
Arnd Bergmann | 00720f0 | 2020-04-08 21:04:31 +0200 | [diff] [blame] | 2863 | |
Casey Schaufler | 87fbfff | 2020-02-03 09:15:00 -0800 | [diff] [blame] | 2864 | return rc; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2865 | } |
Casey Schaufler | 87fbfff | 2020-02-03 09:15:00 -0800 | [diff] [blame] | 2866 | if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in)) |
| 2867 | return 0; |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 2868 | rc = smk_ipv4_check(sock->sk, (struct sockaddr_in *)sap); |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 2869 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2870 | } |
| 2871 | |
| 2872 | /** |
| 2873 | * smack_flags_to_may - convert S_ to MAY_ values |
| 2874 | * @flags: the S_ value |
| 2875 | * |
| 2876 | * Returns the equivalent MAY_ value |
| 2877 | */ |
| 2878 | static int smack_flags_to_may(int flags) |
| 2879 | { |
| 2880 | int may = 0; |
| 2881 | |
| 2882 | if (flags & S_IRUGO) |
| 2883 | may |= MAY_READ; |
| 2884 | if (flags & S_IWUGO) |
| 2885 | may |= MAY_WRITE; |
| 2886 | if (flags & S_IXUGO) |
| 2887 | may |= MAY_EXEC; |
| 2888 | |
| 2889 | return may; |
| 2890 | } |
| 2891 | |
| 2892 | /** |
| 2893 | * smack_msg_msg_alloc_security - Set the security blob for msg_msg |
| 2894 | * @msg: the object |
| 2895 | * |
| 2896 | * Returns 0 |
| 2897 | */ |
| 2898 | static int smack_msg_msg_alloc_security(struct msg_msg *msg) |
| 2899 | { |
Casey Schaufler | ecd5f82 | 2018-11-20 11:55:02 -0800 | [diff] [blame] | 2900 | struct smack_known **blob = smack_msg_msg(msg); |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 2901 | |
Casey Schaufler | ecd5f82 | 2018-11-20 11:55:02 -0800 | [diff] [blame] | 2902 | *blob = smk_of_current(); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2903 | return 0; |
| 2904 | } |
| 2905 | |
| 2906 | /** |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 2907 | * smack_of_ipc - the smack pointer for the ipc |
| 2908 | * @isp: the object |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2909 | * |
| 2910 | * Returns a pointer to the smack value |
| 2911 | */ |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 2912 | static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2913 | { |
Casey Schaufler | 019bcca | 2018-09-21 17:19:54 -0700 | [diff] [blame] | 2914 | struct smack_known **blob = smack_ipc(isp); |
| 2915 | |
| 2916 | return *blob; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | /** |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 2920 | * smack_ipc_alloc_security - Set the security blob for ipc |
| 2921 | * @isp: the object |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2922 | * |
| 2923 | * Returns 0 |
| 2924 | */ |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 2925 | static int smack_ipc_alloc_security(struct kern_ipc_perm *isp) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2926 | { |
Casey Schaufler | 019bcca | 2018-09-21 17:19:54 -0700 | [diff] [blame] | 2927 | struct smack_known **blob = smack_ipc(isp); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2928 | |
Casey Schaufler | 019bcca | 2018-09-21 17:19:54 -0700 | [diff] [blame] | 2929 | *blob = smk_of_current(); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2930 | return 0; |
| 2931 | } |
| 2932 | |
| 2933 | /** |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2934 | * smk_curacc_shm : check if current has access on shm |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 2935 | * @isp : the object |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2936 | * @access : access requested |
| 2937 | * |
| 2938 | * Returns 0 if current has the requested access, error code otherwise |
| 2939 | */ |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 2940 | static int smk_curacc_shm(struct kern_ipc_perm *isp, int access) |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2941 | { |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 2942 | struct smack_known *ssp = smack_of_ipc(isp); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2943 | struct smk_audit_info ad; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 2944 | int rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2945 | |
| 2946 | #ifdef CONFIG_AUDIT |
| 2947 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC); |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 2948 | ad.a.u.ipc_id = isp->id; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2949 | #endif |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 2950 | rc = smk_curacc(ssp, access, &ad); |
| 2951 | rc = smk_bu_current("shm", ssp, access, rc); |
| 2952 | return rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2953 | } |
| 2954 | |
| 2955 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2956 | * smack_shm_associate - Smack access check for shm |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 2957 | * @isp: the object |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2958 | * @shmflg: access requested |
| 2959 | * |
| 2960 | * Returns 0 if current has the requested access, error code otherwise |
| 2961 | */ |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 2962 | static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2963 | { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2964 | int may; |
| 2965 | |
| 2966 | may = smack_flags_to_may(shmflg); |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 2967 | return smk_curacc_shm(isp, may); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2968 | } |
| 2969 | |
| 2970 | /** |
| 2971 | * smack_shm_shmctl - Smack access check for shm |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 2972 | * @isp: the object |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2973 | * @cmd: what it wants to do |
| 2974 | * |
| 2975 | * Returns 0 if current has the requested access, error code otherwise |
| 2976 | */ |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 2977 | static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2978 | { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2979 | int may; |
| 2980 | |
| 2981 | switch (cmd) { |
| 2982 | case IPC_STAT: |
| 2983 | case SHM_STAT: |
Davidlohr Bueso | c21a697 | 2018-04-10 16:35:23 -0700 | [diff] [blame] | 2984 | case SHM_STAT_ANY: |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2985 | may = MAY_READ; |
| 2986 | break; |
| 2987 | case IPC_SET: |
| 2988 | case SHM_LOCK: |
| 2989 | case SHM_UNLOCK: |
| 2990 | case IPC_RMID: |
| 2991 | may = MAY_READWRITE; |
| 2992 | break; |
| 2993 | case IPC_INFO: |
| 2994 | case SHM_INFO: |
| 2995 | /* |
| 2996 | * System level information. |
| 2997 | */ |
| 2998 | return 0; |
| 2999 | default: |
| 3000 | return -EINVAL; |
| 3001 | } |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3002 | return smk_curacc_shm(isp, may); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3003 | } |
| 3004 | |
| 3005 | /** |
| 3006 | * smack_shm_shmat - Smack access for shmat |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3007 | * @isp: the object |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3008 | * @shmaddr: unused |
| 3009 | * @shmflg: access requested |
| 3010 | * |
| 3011 | * Returns 0 if current has the requested access, error code otherwise |
| 3012 | */ |
luanshi | a1a07f2 | 2019-07-05 10:35:20 +0800 | [diff] [blame] | 3013 | static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3014 | int shmflg) |
| 3015 | { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3016 | int may; |
| 3017 | |
| 3018 | may = smack_flags_to_may(shmflg); |
luanshi | a1a07f2 | 2019-07-05 10:35:20 +0800 | [diff] [blame] | 3019 | return smk_curacc_shm(isp, may); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | /** |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3023 | * smk_curacc_sem : check if current has access on sem |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3024 | * @isp : the object |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3025 | * @access : access requested |
| 3026 | * |
| 3027 | * Returns 0 if current has the requested access, error code otherwise |
| 3028 | */ |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3029 | static int smk_curacc_sem(struct kern_ipc_perm *isp, int access) |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3030 | { |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3031 | struct smack_known *ssp = smack_of_ipc(isp); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3032 | struct smk_audit_info ad; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 3033 | int rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3034 | |
| 3035 | #ifdef CONFIG_AUDIT |
| 3036 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC); |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3037 | ad.a.u.ipc_id = isp->id; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3038 | #endif |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 3039 | rc = smk_curacc(ssp, access, &ad); |
| 3040 | rc = smk_bu_current("sem", ssp, access, rc); |
| 3041 | return rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3045 | * smack_sem_associate - Smack access check for sem |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3046 | * @isp: the object |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3047 | * @semflg: access requested |
| 3048 | * |
| 3049 | * Returns 0 if current has the requested access, error code otherwise |
| 3050 | */ |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3051 | static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3052 | { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3053 | int may; |
| 3054 | |
| 3055 | may = smack_flags_to_may(semflg); |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3056 | return smk_curacc_sem(isp, may); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3057 | } |
| 3058 | |
| 3059 | /** |
Casey Schaufler | b57d020 | 2021-10-13 14:56:43 -0700 | [diff] [blame] | 3060 | * smack_sem_semctl - Smack access check for sem |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3061 | * @isp: the object |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3062 | * @cmd: what it wants to do |
| 3063 | * |
| 3064 | * Returns 0 if current has the requested access, error code otherwise |
| 3065 | */ |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3066 | static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3067 | { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3068 | int may; |
| 3069 | |
| 3070 | switch (cmd) { |
| 3071 | case GETPID: |
| 3072 | case GETNCNT: |
| 3073 | case GETZCNT: |
| 3074 | case GETVAL: |
| 3075 | case GETALL: |
| 3076 | case IPC_STAT: |
| 3077 | case SEM_STAT: |
Davidlohr Bueso | a280d6d | 2018-04-10 16:35:26 -0700 | [diff] [blame] | 3078 | case SEM_STAT_ANY: |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3079 | may = MAY_READ; |
| 3080 | break; |
| 3081 | case SETVAL: |
| 3082 | case SETALL: |
| 3083 | case IPC_RMID: |
| 3084 | case IPC_SET: |
| 3085 | may = MAY_READWRITE; |
| 3086 | break; |
| 3087 | case IPC_INFO: |
| 3088 | case SEM_INFO: |
| 3089 | /* |
| 3090 | * System level information |
| 3091 | */ |
| 3092 | return 0; |
| 3093 | default: |
| 3094 | return -EINVAL; |
| 3095 | } |
| 3096 | |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3097 | return smk_curacc_sem(isp, may); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3098 | } |
| 3099 | |
| 3100 | /** |
| 3101 | * smack_sem_semop - Smack checks of semaphore operations |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3102 | * @isp: the object |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3103 | * @sops: unused |
| 3104 | * @nsops: unused |
| 3105 | * @alter: unused |
| 3106 | * |
| 3107 | * Treated as read and write in all cases. |
| 3108 | * |
| 3109 | * Returns 0 if access is allowed, error code otherwise |
| 3110 | */ |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3111 | static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3112 | unsigned nsops, int alter) |
| 3113 | { |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3114 | return smk_curacc_sem(isp, MAY_READWRITE); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3115 | } |
| 3116 | |
| 3117 | /** |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3118 | * smk_curacc_msq : helper to check if current has access on msq |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3119 | * @isp : the msq |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3120 | * @access : access requested |
| 3121 | * |
| 3122 | * return 0 if current has access, error otherwise |
| 3123 | */ |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3124 | static int smk_curacc_msq(struct kern_ipc_perm *isp, int access) |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3125 | { |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3126 | struct smack_known *msp = smack_of_ipc(isp); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3127 | struct smk_audit_info ad; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 3128 | int rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3129 | |
| 3130 | #ifdef CONFIG_AUDIT |
| 3131 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC); |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3132 | ad.a.u.ipc_id = isp->id; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3133 | #endif |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 3134 | rc = smk_curacc(msp, access, &ad); |
| 3135 | rc = smk_bu_current("msq", msp, access, rc); |
| 3136 | return rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3137 | } |
| 3138 | |
| 3139 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3140 | * smack_msg_queue_associate - Smack access check for msg_queue |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3141 | * @isp: the object |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3142 | * @msqflg: access requested |
| 3143 | * |
| 3144 | * Returns 0 if current has the requested access, error code otherwise |
| 3145 | */ |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3146 | static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3147 | { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3148 | int may; |
| 3149 | |
| 3150 | may = smack_flags_to_may(msqflg); |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3151 | return smk_curacc_msq(isp, may); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | /** |
| 3155 | * smack_msg_queue_msgctl - Smack access check for msg_queue |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3156 | * @isp: the object |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3157 | * @cmd: what it wants to do |
| 3158 | * |
| 3159 | * Returns 0 if current has the requested access, error code otherwise |
| 3160 | */ |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3161 | static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3162 | { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3163 | int may; |
| 3164 | |
| 3165 | switch (cmd) { |
| 3166 | case IPC_STAT: |
| 3167 | case MSG_STAT: |
Davidlohr Bueso | 23c8cec | 2018-04-10 16:35:30 -0700 | [diff] [blame] | 3168 | case MSG_STAT_ANY: |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3169 | may = MAY_READ; |
| 3170 | break; |
| 3171 | case IPC_SET: |
| 3172 | case IPC_RMID: |
| 3173 | may = MAY_READWRITE; |
| 3174 | break; |
| 3175 | case IPC_INFO: |
| 3176 | case MSG_INFO: |
| 3177 | /* |
| 3178 | * System level information |
| 3179 | */ |
| 3180 | return 0; |
| 3181 | default: |
| 3182 | return -EINVAL; |
| 3183 | } |
| 3184 | |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3185 | return smk_curacc_msq(isp, may); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3186 | } |
| 3187 | |
| 3188 | /** |
| 3189 | * smack_msg_queue_msgsnd - Smack access check for msg_queue |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3190 | * @isp: the object |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3191 | * @msg: unused |
| 3192 | * @msqflg: access requested |
| 3193 | * |
| 3194 | * Returns 0 if current has the requested access, error code otherwise |
| 3195 | */ |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3196 | static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3197 | int msqflg) |
| 3198 | { |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3199 | int may; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3200 | |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3201 | may = smack_flags_to_may(msqflg); |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3202 | return smk_curacc_msq(isp, may); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | /** |
Casey Schaufler | b57d020 | 2021-10-13 14:56:43 -0700 | [diff] [blame] | 3206 | * smack_msg_queue_msgrcv - Smack access check for msg_queue |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3207 | * @isp: the object |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3208 | * @msg: unused |
| 3209 | * @target: unused |
| 3210 | * @type: unused |
| 3211 | * @mode: unused |
| 3212 | * |
| 3213 | * Returns 0 if current has read and write access, error code otherwise |
| 3214 | */ |
Casey Schaufler | b57d020 | 2021-10-13 14:56:43 -0700 | [diff] [blame] | 3215 | static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, |
| 3216 | struct msg_msg *msg, |
| 3217 | struct task_struct *target, long type, |
| 3218 | int mode) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3219 | { |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 3220 | return smk_curacc_msq(isp, MAY_READWRITE); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3221 | } |
| 3222 | |
| 3223 | /** |
| 3224 | * smack_ipc_permission - Smack access for ipc_permission() |
| 3225 | * @ipp: the object permissions |
| 3226 | * @flag: access requested |
| 3227 | * |
| 3228 | * Returns 0 if current has read and write access, error code otherwise |
| 3229 | */ |
| 3230 | static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag) |
| 3231 | { |
Casey Schaufler | 019bcca | 2018-09-21 17:19:54 -0700 | [diff] [blame] | 3232 | struct smack_known **blob = smack_ipc(ipp); |
| 3233 | struct smack_known *iskp = *blob; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3234 | int may = smack_flags_to_may(flag); |
| 3235 | struct smk_audit_info ad; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 3236 | int rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3237 | |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3238 | #ifdef CONFIG_AUDIT |
| 3239 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC); |
| 3240 | ad.a.u.ipc_id = ipp->id; |
| 3241 | #endif |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3242 | rc = smk_curacc(iskp, may, &ad); |
| 3243 | rc = smk_bu_current("svipc", iskp, may, rc); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 3244 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3245 | } |
| 3246 | |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 3247 | /** |
| 3248 | * smack_ipc_getsecid - Extract smack security id |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 3249 | * @ipp: the object permissions |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 3250 | * @secid: where result will be saved |
| 3251 | */ |
| 3252 | static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid) |
| 3253 | { |
Casey Schaufler | 019bcca | 2018-09-21 17:19:54 -0700 | [diff] [blame] | 3254 | struct smack_known **blob = smack_ipc(ipp); |
| 3255 | struct smack_known *iskp = *blob; |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 3256 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3257 | *secid = iskp->smk_secid; |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 3258 | } |
| 3259 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3260 | /** |
| 3261 | * smack_d_instantiate - Make sure the blob is correct on an inode |
Dan Carpenter | 3e62cbb | 2010-06-01 09:14:04 +0200 | [diff] [blame] | 3262 | * @opt_dentry: dentry where inode will be attached |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3263 | * @inode: the object |
| 3264 | * |
| 3265 | * Set the inode's security blob if it hasn't been done already. |
| 3266 | */ |
| 3267 | static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode) |
| 3268 | { |
| 3269 | struct super_block *sbp; |
| 3270 | struct superblock_smack *sbsp; |
| 3271 | struct inode_smack *isp; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3272 | struct smack_known *skp; |
| 3273 | struct smack_known *ckp = smk_of_current(); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3274 | struct smack_known *final; |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 3275 | char trattr[TRANS_TRUE_SIZE]; |
| 3276 | int transflag = 0; |
Casey Schaufler | 2267b13 | 2012-03-13 19:14:19 -0700 | [diff] [blame] | 3277 | int rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3278 | struct dentry *dp; |
| 3279 | |
| 3280 | if (inode == NULL) |
| 3281 | return; |
| 3282 | |
Casey Schaufler | fb4021b | 2018-11-12 12:43:01 -0800 | [diff] [blame] | 3283 | isp = smack_inode(inode); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3284 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3285 | /* |
| 3286 | * If the inode is already instantiated |
| 3287 | * take the quick way out |
| 3288 | */ |
| 3289 | if (isp->smk_flags & SMK_INODE_INSTANT) |
Casey Schaufler | 921bb1c | 2020-04-24 15:23:04 -0700 | [diff] [blame] | 3290 | return; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3291 | |
| 3292 | sbp = inode->i_sb; |
Casey Schaufler | 1aea780 | 2021-04-22 17:41:15 +0200 | [diff] [blame] | 3293 | sbsp = smack_superblock(sbp); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3294 | /* |
| 3295 | * We're going to use the superblock default label |
| 3296 | * if there's no label on the file. |
| 3297 | */ |
| 3298 | final = sbsp->smk_default; |
| 3299 | |
| 3300 | /* |
Casey Schaufler | e97dcb0 | 2008-06-02 10:04:32 -0700 | [diff] [blame] | 3301 | * If this is the root inode the superblock |
| 3302 | * may be in the process of initialization. |
| 3303 | * If that is the case use the root value out |
| 3304 | * of the superblock. |
| 3305 | */ |
| 3306 | if (opt_dentry->d_parent == opt_dentry) { |
Łukasz Stelmach | 1d8c232 | 2014-12-16 16:53:08 +0100 | [diff] [blame] | 3307 | switch (sbp->s_magic) { |
| 3308 | case CGROUP_SUPER_MAGIC: |
José Bollo | 58c442f | 2018-02-27 17:06:21 +0100 | [diff] [blame] | 3309 | case CGROUP2_SUPER_MAGIC: |
Casey Schaufler | 36ea735 | 2014-04-28 15:23:01 -0700 | [diff] [blame] | 3310 | /* |
| 3311 | * The cgroup filesystem is never mounted, |
| 3312 | * so there's no opportunity to set the mount |
| 3313 | * options. |
| 3314 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3315 | sbsp->smk_root = &smack_known_star; |
| 3316 | sbsp->smk_default = &smack_known_star; |
Łukasz Stelmach | 1d8c232 | 2014-12-16 16:53:08 +0100 | [diff] [blame] | 3317 | isp->smk_inode = sbsp->smk_root; |
| 3318 | break; |
| 3319 | case TMPFS_MAGIC: |
| 3320 | /* |
| 3321 | * What about shmem/tmpfs anonymous files with dentry |
| 3322 | * obtained from d_alloc_pseudo()? |
| 3323 | */ |
| 3324 | isp->smk_inode = smk_of_current(); |
| 3325 | break; |
Roman Kubiak | 8da4aba | 2015-10-05 12:27:16 +0200 | [diff] [blame] | 3326 | case PIPEFS_MAGIC: |
| 3327 | isp->smk_inode = smk_of_current(); |
| 3328 | break; |
Rafal Krypa | 805b65a | 2016-12-09 14:03:04 +0100 | [diff] [blame] | 3329 | case SOCKFS_MAGIC: |
| 3330 | /* |
| 3331 | * Socket access is controlled by the socket |
| 3332 | * structures associated with the task involved. |
| 3333 | */ |
| 3334 | isp->smk_inode = &smack_known_star; |
| 3335 | break; |
Łukasz Stelmach | 1d8c232 | 2014-12-16 16:53:08 +0100 | [diff] [blame] | 3336 | default: |
| 3337 | isp->smk_inode = sbsp->smk_root; |
| 3338 | break; |
Casey Schaufler | 36ea735 | 2014-04-28 15:23:01 -0700 | [diff] [blame] | 3339 | } |
Casey Schaufler | e97dcb0 | 2008-06-02 10:04:32 -0700 | [diff] [blame] | 3340 | isp->smk_flags |= SMK_INODE_INSTANT; |
Casey Schaufler | 921bb1c | 2020-04-24 15:23:04 -0700 | [diff] [blame] | 3341 | return; |
Casey Schaufler | e97dcb0 | 2008-06-02 10:04:32 -0700 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | /* |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3345 | * This is pretty hackish. |
| 3346 | * Casey says that we shouldn't have to do |
| 3347 | * file system specific code, but it does help |
| 3348 | * with keeping it simple. |
| 3349 | */ |
| 3350 | switch (sbp->s_magic) { |
| 3351 | case SMACK_MAGIC: |
Casey Schaufler | 36ea735 | 2014-04-28 15:23:01 -0700 | [diff] [blame] | 3352 | case CGROUP_SUPER_MAGIC: |
José Bollo | 58c442f | 2018-02-27 17:06:21 +0100 | [diff] [blame] | 3353 | case CGROUP2_SUPER_MAGIC: |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3354 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3355 | * Casey says that it's a little embarrassing |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3356 | * that the smack file system doesn't do |
| 3357 | * extended attributes. |
Casey Schaufler | 36ea735 | 2014-04-28 15:23:01 -0700 | [diff] [blame] | 3358 | * |
Casey Schaufler | 36ea735 | 2014-04-28 15:23:01 -0700 | [diff] [blame] | 3359 | * Cgroupfs is special |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3360 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3361 | final = &smack_known_star; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3362 | break; |
| 3363 | case DEVPTS_SUPER_MAGIC: |
| 3364 | /* |
| 3365 | * devpts seems content with the label of the task. |
| 3366 | * Programs that change smack have to treat the |
| 3367 | * pty with respect. |
| 3368 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3369 | final = ckp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3370 | break; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3371 | case PROC_SUPER_MAGIC: |
| 3372 | /* |
| 3373 | * Casey says procfs appears not to care. |
| 3374 | * The superblock default suffices. |
| 3375 | */ |
| 3376 | break; |
| 3377 | case TMPFS_MAGIC: |
| 3378 | /* |
| 3379 | * Device labels should come from the filesystem, |
| 3380 | * but watch out, because they're volitile, |
| 3381 | * getting recreated on every reboot. |
| 3382 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3383 | final = &smack_known_star; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3384 | /* |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3385 | * If a smack value has been set we want to use it, |
| 3386 | * but since tmpfs isn't giving us the opportunity |
| 3387 | * to set mount options simulate setting the |
| 3388 | * superblock default. |
| 3389 | */ |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 3390 | fallthrough; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3391 | default: |
| 3392 | /* |
| 3393 | * This isn't an understood special case. |
| 3394 | * Get the value from the xattr. |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3395 | */ |
| 3396 | |
| 3397 | /* |
| 3398 | * UNIX domain sockets use lower level socket data. |
| 3399 | */ |
| 3400 | if (S_ISSOCK(inode->i_mode)) { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3401 | final = &smack_known_star; |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3402 | break; |
| 3403 | } |
| 3404 | /* |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3405 | * No xattr support means, alas, no SMACK label. |
| 3406 | * Use the aforeapplied default. |
| 3407 | * It would be curious if the label of the task |
| 3408 | * does not match that assigned. |
| 3409 | */ |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 3410 | if (!(inode->i_opflags & IOP_XATTR)) |
| 3411 | break; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3412 | /* |
| 3413 | * Get the dentry for xattr. |
| 3414 | */ |
Dan Carpenter | 3e62cbb | 2010-06-01 09:14:04 +0200 | [diff] [blame] | 3415 | dp = dget(opt_dentry); |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3416 | skp = smk_fetch(XATTR_NAME_SMACK, inode, dp); |
Lukasz Pawelczyk | e774ad6 | 2015-04-20 17:12:54 +0200 | [diff] [blame] | 3417 | if (!IS_ERR_OR_NULL(skp)) |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3418 | final = skp; |
Casey Schaufler | 2267b13 | 2012-03-13 19:14:19 -0700 | [diff] [blame] | 3419 | |
| 3420 | /* |
| 3421 | * Transmuting directory |
| 3422 | */ |
| 3423 | if (S_ISDIR(inode->i_mode)) { |
| 3424 | /* |
| 3425 | * If this is a new directory and the label was |
| 3426 | * transmuted when the inode was initialized |
| 3427 | * set the transmute attribute on the directory |
| 3428 | * and mark the inode. |
| 3429 | * |
| 3430 | * If there is a transmute attribute on the |
| 3431 | * directory mark the inode. |
| 3432 | */ |
| 3433 | if (isp->smk_flags & SMK_INODE_CHANGED) { |
| 3434 | isp->smk_flags &= ~SMK_INODE_CHANGED; |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 3435 | rc = __vfs_setxattr(&init_user_ns, dp, inode, |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 3436 | XATTR_NAME_SMACKTRANSMUTE, |
Casey Schaufler | 2267b13 | 2012-03-13 19:14:19 -0700 | [diff] [blame] | 3437 | TRANS_TRUE, TRANS_TRUE_SIZE, |
| 3438 | 0); |
| 3439 | } else { |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 3440 | rc = __vfs_getxattr(dp, inode, |
Casey Schaufler | 2267b13 | 2012-03-13 19:14:19 -0700 | [diff] [blame] | 3441 | XATTR_NAME_SMACKTRANSMUTE, trattr, |
| 3442 | TRANS_TRUE_SIZE); |
| 3443 | if (rc >= 0 && strncmp(trattr, TRANS_TRUE, |
| 3444 | TRANS_TRUE_SIZE) != 0) |
| 3445 | rc = -EINVAL; |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 3446 | } |
Casey Schaufler | 2267b13 | 2012-03-13 19:14:19 -0700 | [diff] [blame] | 3447 | if (rc >= 0) |
| 3448 | transflag = SMK_INODE_TRANSMUTE; |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 3449 | } |
Seth Forshee | 809c02e | 2016-04-26 14:36:22 -0500 | [diff] [blame] | 3450 | /* |
| 3451 | * Don't let the exec or mmap label be "*" or "@". |
| 3452 | */ |
| 3453 | skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp); |
| 3454 | if (IS_ERR(skp) || skp == &smack_known_star || |
| 3455 | skp == &smack_known_web) |
| 3456 | skp = NULL; |
| 3457 | isp->smk_task = skp; |
Lukasz Pawelczyk | e774ad6 | 2015-04-20 17:12:54 +0200 | [diff] [blame] | 3458 | |
Casey Schaufler | 19760ad | 2013-12-16 16:27:26 -0800 | [diff] [blame] | 3459 | skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp); |
Lukasz Pawelczyk | e774ad6 | 2015-04-20 17:12:54 +0200 | [diff] [blame] | 3460 | if (IS_ERR(skp) || skp == &smack_known_star || |
| 3461 | skp == &smack_known_web) |
Casey Schaufler | 19760ad | 2013-12-16 16:27:26 -0800 | [diff] [blame] | 3462 | skp = NULL; |
| 3463 | isp->smk_mmap = skp; |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 3464 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3465 | dput(dp); |
| 3466 | break; |
| 3467 | } |
| 3468 | |
| 3469 | if (final == NULL) |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3470 | isp->smk_inode = ckp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3471 | else |
| 3472 | isp->smk_inode = final; |
| 3473 | |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 3474 | isp->smk_flags |= (SMK_INODE_INSTANT | transflag); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3475 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3476 | return; |
| 3477 | } |
| 3478 | |
| 3479 | /** |
| 3480 | * smack_getprocattr - Smack process attribute access |
| 3481 | * @p: the object task |
| 3482 | * @name: the name of the attribute in /proc/.../attr |
| 3483 | * @value: where to put the result |
| 3484 | * |
| 3485 | * Places a copy of the task Smack into value |
| 3486 | * |
| 3487 | * Returns the length of the smack label or an error code |
| 3488 | */ |
| 3489 | static int smack_getprocattr(struct task_struct *p, char *name, char **value) |
| 3490 | { |
Paul Moore | a3727a8 | 2021-09-23 09:50:11 -0400 | [diff] [blame] | 3491 | struct smack_known *skp = smk_of_task_struct_obj(p); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3492 | char *cp; |
| 3493 | int slen; |
| 3494 | |
| 3495 | if (strcmp(name, "current") != 0) |
| 3496 | return -EINVAL; |
| 3497 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3498 | cp = kstrdup(skp->smk_known, GFP_KERNEL); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3499 | if (cp == NULL) |
| 3500 | return -ENOMEM; |
| 3501 | |
| 3502 | slen = strlen(cp); |
| 3503 | *value = cp; |
| 3504 | return slen; |
| 3505 | } |
| 3506 | |
| 3507 | /** |
| 3508 | * smack_setprocattr - Smack process attribute setting |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3509 | * @name: the name of the attribute in /proc/.../attr |
| 3510 | * @value: the value to set |
| 3511 | * @size: the size of the value |
| 3512 | * |
| 3513 | * Sets the Smack value of the task. Only setting self |
| 3514 | * is permitted and only with privilege |
| 3515 | * |
| 3516 | * Returns the length of the smack label or an error code |
| 3517 | */ |
Stephen Smalley | b21507e | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3518 | static int smack_setprocattr(const char *name, void *value, size_t size) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3519 | { |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 3520 | struct task_smack *tsp = smack_cred(current_cred()); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3521 | struct cred *new; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3522 | struct smack_known *skp; |
Zbigniew Jasinski | 38416e5 | 2015-10-19 18:23:53 +0200 | [diff] [blame] | 3523 | struct smack_known_list_elem *sklep; |
| 3524 | int rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3525 | |
Zbigniew Jasinski | 38416e5 | 2015-10-19 18:23:53 +0200 | [diff] [blame] | 3526 | if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel)) |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 3527 | return -EPERM; |
| 3528 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 3529 | if (value == NULL || size == 0 || size >= SMK_LONGLABEL) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3530 | return -EINVAL; |
| 3531 | |
| 3532 | if (strcmp(name, "current") != 0) |
| 3533 | return -EINVAL; |
| 3534 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3535 | skp = smk_import_entry(value, size); |
Lukasz Pawelczyk | e774ad6 | 2015-04-20 17:12:54 +0200 | [diff] [blame] | 3536 | if (IS_ERR(skp)) |
| 3537 | return PTR_ERR(skp); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3538 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3539 | /* |
Himanshu Shukla | 7128ea1 | 2016-11-10 16:17:49 +0530 | [diff] [blame] | 3540 | * No process is ever allowed the web ("@") label |
| 3541 | * and the star ("*") label. |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3542 | */ |
Himanshu Shukla | 7128ea1 | 2016-11-10 16:17:49 +0530 | [diff] [blame] | 3543 | if (skp == &smack_known_web || skp == &smack_known_star) |
| 3544 | return -EINVAL; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3545 | |
Zbigniew Jasinski | 38416e5 | 2015-10-19 18:23:53 +0200 | [diff] [blame] | 3546 | if (!smack_privileged(CAP_MAC_ADMIN)) { |
| 3547 | rc = -EPERM; |
| 3548 | list_for_each_entry(sklep, &tsp->smk_relabel, list) |
| 3549 | if (sklep->smk_label == skp) { |
| 3550 | rc = 0; |
| 3551 | break; |
| 3552 | } |
| 3553 | if (rc) |
| 3554 | return rc; |
| 3555 | } |
| 3556 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3557 | new = prepare_creds(); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3558 | if (new == NULL) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3559 | return -ENOMEM; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 3560 | |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 3561 | tsp = smack_cred(new); |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3562 | tsp->smk_task = skp; |
Zbigniew Jasinski | 38416e5 | 2015-10-19 18:23:53 +0200 | [diff] [blame] | 3563 | /* |
| 3564 | * process can change its label only once |
| 3565 | */ |
| 3566 | smk_destroy_label_list(&tsp->smk_relabel); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 3567 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3568 | commit_creds(new); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3569 | return size; |
| 3570 | } |
| 3571 | |
| 3572 | /** |
| 3573 | * smack_unix_stream_connect - Smack access on UDS |
David S. Miller | 3610cda | 2011-01-05 15:38:53 -0800 | [diff] [blame] | 3574 | * @sock: one sock |
| 3575 | * @other: the other sock |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3576 | * @newsk: unused |
| 3577 | * |
| 3578 | * Return 0 if a subject with the smack of sock could access |
| 3579 | * an object with the smack of other, otherwise an error code |
| 3580 | */ |
David S. Miller | 3610cda | 2011-01-05 15:38:53 -0800 | [diff] [blame] | 3581 | static int smack_unix_stream_connect(struct sock *sock, |
| 3582 | struct sock *other, struct sock *newsk) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3583 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3584 | struct smack_known *skp; |
Casey Schaufler | 54e70ec | 2014-04-10 16:37:08 -0700 | [diff] [blame] | 3585 | struct smack_known *okp; |
James Morris | d2e7ad1 | 2011-01-10 09:46:24 +1100 | [diff] [blame] | 3586 | struct socket_smack *ssp = sock->sk_security; |
| 3587 | struct socket_smack *osp = other->sk_security; |
Casey Schaufler | 975d5e5 | 2011-09-26 14:43:39 -0700 | [diff] [blame] | 3588 | struct socket_smack *nsp = newsk->sk_security; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3589 | struct smk_audit_info ad; |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3590 | int rc = 0; |
Kees Cook | 923e9a1 | 2012-04-10 13:26:44 -0700 | [diff] [blame] | 3591 | #ifdef CONFIG_AUDIT |
| 3592 | struct lsm_network_audit net; |
Kees Cook | 923e9a1 | 2012-04-10 13:26:44 -0700 | [diff] [blame] | 3593 | #endif |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3594 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3595 | if (!smack_privileged(CAP_MAC_OVERRIDE)) { |
| 3596 | skp = ssp->smk_out; |
Zbigniew Jasinski | 96be7b5 | 2014-12-29 15:34:58 +0100 | [diff] [blame] | 3597 | okp = osp->smk_in; |
Casey Schaufler | 54e70ec | 2014-04-10 16:37:08 -0700 | [diff] [blame] | 3598 | #ifdef CONFIG_AUDIT |
| 3599 | smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); |
| 3600 | smk_ad_setfield_u_net_sk(&ad, other); |
| 3601 | #endif |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3602 | rc = smk_access(skp, okp, MAY_WRITE, &ad); |
| 3603 | rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 3604 | if (rc == 0) { |
Zbigniew Jasinski | 96be7b5 | 2014-12-29 15:34:58 +0100 | [diff] [blame] | 3605 | okp = osp->smk_out; |
| 3606 | skp = ssp->smk_in; |
Rafal Krypa | 138a868 | 2015-01-08 18:52:45 +0100 | [diff] [blame] | 3607 | rc = smk_access(okp, skp, MAY_WRITE, &ad); |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3608 | rc = smk_bu_note("UDS connect", okp, skp, |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 3609 | MAY_WRITE, rc); |
| 3610 | } |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3611 | } |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3612 | |
Casey Schaufler | 975d5e5 | 2011-09-26 14:43:39 -0700 | [diff] [blame] | 3613 | /* |
| 3614 | * Cross reference the peer labels for SO_PEERSEC. |
| 3615 | */ |
| 3616 | if (rc == 0) { |
Casey Schaufler | 54e70ec | 2014-04-10 16:37:08 -0700 | [diff] [blame] | 3617 | nsp->smk_packet = ssp->smk_out; |
| 3618 | ssp->smk_packet = osp->smk_out; |
Casey Schaufler | 975d5e5 | 2011-09-26 14:43:39 -0700 | [diff] [blame] | 3619 | } |
| 3620 | |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3621 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3622 | } |
| 3623 | |
| 3624 | /** |
| 3625 | * smack_unix_may_send - Smack access on UDS |
| 3626 | * @sock: one socket |
| 3627 | * @other: the other socket |
| 3628 | * |
| 3629 | * Return 0 if a subject with the smack of sock could access |
| 3630 | * an object with the smack of other, otherwise an error code |
| 3631 | */ |
| 3632 | static int smack_unix_may_send(struct socket *sock, struct socket *other) |
| 3633 | { |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3634 | struct socket_smack *ssp = sock->sk->sk_security; |
| 3635 | struct socket_smack *osp = other->sk->sk_security; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3636 | struct smk_audit_info ad; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 3637 | int rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3638 | |
Kees Cook | 923e9a1 | 2012-04-10 13:26:44 -0700 | [diff] [blame] | 3639 | #ifdef CONFIG_AUDIT |
| 3640 | struct lsm_network_audit net; |
| 3641 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3642 | smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3643 | smk_ad_setfield_u_net_sk(&ad, other->sk); |
Kees Cook | 923e9a1 | 2012-04-10 13:26:44 -0700 | [diff] [blame] | 3644 | #endif |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3645 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3646 | if (smack_privileged(CAP_MAC_OVERRIDE)) |
| 3647 | return 0; |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 3648 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3649 | rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad); |
| 3650 | rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc); |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 3651 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | /** |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3655 | * smack_socket_sendmsg - Smack check based on destination host |
| 3656 | * @sock: the socket |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 3657 | * @msg: the message |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3658 | * @size: the size of the message |
| 3659 | * |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3660 | * Return 0 if the current subject can write to the destination host. |
| 3661 | * For IPv4 this is only a question if the destination is a single label host. |
| 3662 | * For IPv6 this is a check against the label of the port. |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3663 | */ |
| 3664 | static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg, |
| 3665 | int size) |
| 3666 | { |
| 3667 | struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name; |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 3668 | #if IS_ENABLED(CONFIG_IPV6) |
Casey Schaufler | 6ea0624 | 2013-08-05 13:21:22 -0700 | [diff] [blame] | 3669 | struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name; |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 3670 | #endif |
| 3671 | #ifdef SMACK_IPV6_SECMARK_LABELING |
| 3672 | struct socket_smack *ssp = sock->sk->sk_security; |
| 3673 | struct smack_known *rsp; |
| 3674 | #endif |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3675 | int rc = 0; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3676 | |
| 3677 | /* |
| 3678 | * Perfectly reasonable for this to be NULL |
| 3679 | */ |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3680 | if (sip == NULL) |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3681 | return 0; |
| 3682 | |
Roman Kubiak | 81bd0d5 | 2015-12-17 13:24:35 +0100 | [diff] [blame] | 3683 | switch (sock->sk->sk_family) { |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3684 | case AF_INET: |
Tetsuo Handa | b9ef551 | 2019-04-12 19:59:35 +0900 | [diff] [blame] | 3685 | if (msg->msg_namelen < sizeof(struct sockaddr_in) || |
| 3686 | sip->sin_family != AF_INET) |
| 3687 | return -EINVAL; |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 3688 | rc = smk_ipv4_check(sock->sk, sip); |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3689 | break; |
Casey Schaufler | 619ae03 | 2019-04-30 14:13:32 -0700 | [diff] [blame] | 3690 | #if IS_ENABLED(CONFIG_IPV6) |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3691 | case AF_INET6: |
Tetsuo Handa | b9ef551 | 2019-04-12 19:59:35 +0900 | [diff] [blame] | 3692 | if (msg->msg_namelen < SIN6_LEN_RFC2133 || |
| 3693 | sap->sin6_family != AF_INET6) |
| 3694 | return -EINVAL; |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 3695 | #ifdef SMACK_IPV6_SECMARK_LABELING |
| 3696 | rsp = smack_ipv6host_label(sap); |
| 3697 | if (rsp != NULL) |
| 3698 | rc = smk_ipv6_check(ssp->smk_out, rsp, sap, |
| 3699 | SMK_CONNECTING); |
| 3700 | #endif |
| 3701 | #ifdef SMACK_IPV6_PORT_LABELING |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3702 | rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING); |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 3703 | #endif |
Casey Schaufler | 619ae03 | 2019-04-30 14:13:32 -0700 | [diff] [blame] | 3704 | #endif /* IS_ENABLED(CONFIG_IPV6) */ |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3705 | break; |
| 3706 | } |
| 3707 | return rc; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3708 | } |
| 3709 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3710 | /** |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 3711 | * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3712 | * @sap: netlabel secattr |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 3713 | * @ssp: socket security information |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3714 | * |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3715 | * Returns a pointer to a Smack label entry found on the label list. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3716 | */ |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3717 | static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap, |
| 3718 | struct socket_smack *ssp) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3719 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3720 | struct smack_known *skp; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 3721 | int found = 0; |
Casey Schaufler | 677264e | 2013-06-28 13:47:07 -0700 | [diff] [blame] | 3722 | int acat; |
| 3723 | int kcat; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3724 | |
Casey Schaufler | 322dd63 | 2020-08-11 17:39:43 -0700 | [diff] [blame] | 3725 | /* |
| 3726 | * Netlabel found it in the cache. |
| 3727 | */ |
| 3728 | if ((sap->flags & NETLBL_SECATTR_CACHE) != 0) |
| 3729 | return (struct smack_known *)sap->cache->data; |
| 3730 | |
| 3731 | if ((sap->flags & NETLBL_SECATTR_SECID) != 0) |
| 3732 | /* |
| 3733 | * Looks like a fallback, which gives us a secid. |
| 3734 | */ |
| 3735 | return smack_from_secid(sap->attr.secid); |
| 3736 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3737 | if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3738 | /* |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3739 | * Looks like a CIPSO packet. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3740 | * If there are flags but no level netlabel isn't |
| 3741 | * behaving the way we expect it to. |
| 3742 | * |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 3743 | * Look it up in the label table |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3744 | * Without guidance regarding the smack value |
| 3745 | * for the packet fall back on the network |
| 3746 | * ambient value. |
| 3747 | */ |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 3748 | rcu_read_lock(); |
Vishal Goel | 348dc28 | 2016-11-23 10:45:31 +0530 | [diff] [blame] | 3749 | list_for_each_entry_rcu(skp, &smack_known_list, list) { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3750 | if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl) |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 3751 | continue; |
Casey Schaufler | 677264e | 2013-06-28 13:47:07 -0700 | [diff] [blame] | 3752 | /* |
| 3753 | * Compare the catsets. Use the netlbl APIs. |
| 3754 | */ |
| 3755 | if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) { |
| 3756 | if ((skp->smk_netlabel.flags & |
| 3757 | NETLBL_SECATTR_MLS_CAT) == 0) |
| 3758 | found = 1; |
| 3759 | break; |
| 3760 | } |
| 3761 | for (acat = -1, kcat = -1; acat == kcat; ) { |
Paul Moore | 4fbe63d | 2014-08-01 11:17:37 -0400 | [diff] [blame] | 3762 | acat = netlbl_catmap_walk(sap->attr.mls.cat, |
| 3763 | acat + 1); |
| 3764 | kcat = netlbl_catmap_walk( |
Casey Schaufler | 677264e | 2013-06-28 13:47:07 -0700 | [diff] [blame] | 3765 | skp->smk_netlabel.attr.mls.cat, |
| 3766 | kcat + 1); |
| 3767 | if (acat < 0 || kcat < 0) |
| 3768 | break; |
| 3769 | } |
| 3770 | if (acat == kcat) { |
| 3771 | found = 1; |
| 3772 | break; |
| 3773 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3774 | } |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 3775 | rcu_read_unlock(); |
| 3776 | |
| 3777 | if (found) |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3778 | return skp; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 3779 | |
Casey Schaufler | 54e70ec | 2014-04-10 16:37:08 -0700 | [diff] [blame] | 3780 | if (ssp != NULL && ssp->smk_in == &smack_known_star) |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 3781 | return &smack_known_web; |
| 3782 | return &smack_known_star; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3783 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3784 | /* |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 3785 | * Without guidance regarding the smack value |
| 3786 | * for the packet fall back on the network |
| 3787 | * ambient value. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3788 | */ |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 3789 | return smack_net_ambient; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3790 | } |
| 3791 | |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3792 | #if IS_ENABLED(CONFIG_IPV6) |
Casey Schaufler | 6ea0624 | 2013-08-05 13:21:22 -0700 | [diff] [blame] | 3793 | static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip) |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3794 | { |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3795 | u8 nexthdr; |
| 3796 | int offset; |
| 3797 | int proto = -EINVAL; |
| 3798 | struct ipv6hdr _ipv6h; |
| 3799 | struct ipv6hdr *ip6; |
| 3800 | __be16 frag_off; |
| 3801 | struct tcphdr _tcph, *th; |
| 3802 | struct udphdr _udph, *uh; |
| 3803 | struct dccp_hdr _dccph, *dh; |
| 3804 | |
| 3805 | sip->sin6_port = 0; |
| 3806 | |
| 3807 | offset = skb_network_offset(skb); |
| 3808 | ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); |
| 3809 | if (ip6 == NULL) |
| 3810 | return -EINVAL; |
| 3811 | sip->sin6_addr = ip6->saddr; |
| 3812 | |
| 3813 | nexthdr = ip6->nexthdr; |
| 3814 | offset += sizeof(_ipv6h); |
| 3815 | offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); |
| 3816 | if (offset < 0) |
| 3817 | return -EINVAL; |
| 3818 | |
| 3819 | proto = nexthdr; |
| 3820 | switch (proto) { |
| 3821 | case IPPROTO_TCP: |
| 3822 | th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); |
| 3823 | if (th != NULL) |
| 3824 | sip->sin6_port = th->source; |
| 3825 | break; |
| 3826 | case IPPROTO_UDP: |
Piotr Sawicki | a07ef95 | 2018-07-19 11:45:16 +0200 | [diff] [blame] | 3827 | case IPPROTO_UDPLITE: |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3828 | uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); |
| 3829 | if (uh != NULL) |
| 3830 | sip->sin6_port = uh->source; |
| 3831 | break; |
| 3832 | case IPPROTO_DCCP: |
| 3833 | dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); |
| 3834 | if (dh != NULL) |
| 3835 | sip->sin6_port = dh->dccph_sport; |
| 3836 | break; |
| 3837 | } |
| 3838 | return proto; |
| 3839 | } |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3840 | #endif /* CONFIG_IPV6 */ |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3841 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3842 | /** |
Casey Schaufler | 36be812 | 2020-08-11 17:39:41 -0700 | [diff] [blame] | 3843 | * smack_from_skb - Smack data from the secmark in an skb |
| 3844 | * @skb: packet |
| 3845 | * |
| 3846 | * Returns smack_known of the secmark or NULL if that won't work. |
| 3847 | */ |
Casey Schaufler | bf0afe6 | 2020-09-22 14:59:31 -0700 | [diff] [blame] | 3848 | #ifdef CONFIG_NETWORK_SECMARK |
Casey Schaufler | 36be812 | 2020-08-11 17:39:41 -0700 | [diff] [blame] | 3849 | static struct smack_known *smack_from_skb(struct sk_buff *skb) |
| 3850 | { |
| 3851 | if (skb == NULL || skb->secmark == 0) |
| 3852 | return NULL; |
| 3853 | |
| 3854 | return smack_from_secid(skb->secmark); |
| 3855 | } |
Casey Schaufler | bf0afe6 | 2020-09-22 14:59:31 -0700 | [diff] [blame] | 3856 | #else |
| 3857 | static inline struct smack_known *smack_from_skb(struct sk_buff *skb) |
| 3858 | { |
| 3859 | return NULL; |
| 3860 | } |
| 3861 | #endif |
Casey Schaufler | 36be812 | 2020-08-11 17:39:41 -0700 | [diff] [blame] | 3862 | |
| 3863 | /** |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 3864 | * smack_from_netlbl - Smack data from the IP options in an skb |
| 3865 | * @sk: socket data came in on |
| 3866 | * @family: address family |
| 3867 | * @skb: packet |
| 3868 | * |
Casey Schaufler | 322dd63 | 2020-08-11 17:39:43 -0700 | [diff] [blame] | 3869 | * Find the Smack label in the IP options. If it hasn't been |
| 3870 | * added to the netlabel cache, add it here. |
| 3871 | * |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 3872 | * Returns smack_known of the IP options or NULL if that won't work. |
| 3873 | */ |
Florian Westphal | 41dd959 | 2020-11-30 16:36:29 +0100 | [diff] [blame] | 3874 | static struct smack_known *smack_from_netlbl(const struct sock *sk, u16 family, |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 3875 | struct sk_buff *skb) |
| 3876 | { |
| 3877 | struct netlbl_lsm_secattr secattr; |
| 3878 | struct socket_smack *ssp = NULL; |
| 3879 | struct smack_known *skp = NULL; |
| 3880 | |
| 3881 | netlbl_secattr_init(&secattr); |
| 3882 | |
| 3883 | if (sk) |
| 3884 | ssp = sk->sk_security; |
Casey Schaufler | 322dd63 | 2020-08-11 17:39:43 -0700 | [diff] [blame] | 3885 | |
| 3886 | if (netlbl_skbuff_getattr(skb, family, &secattr) == 0) { |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 3887 | skp = smack_from_secattr(&secattr, ssp); |
Casey Schaufler | 322dd63 | 2020-08-11 17:39:43 -0700 | [diff] [blame] | 3888 | if (secattr.flags & NETLBL_SECATTR_CACHEABLE) |
Alex Shi | 9b0072e | 2020-11-08 14:45:42 +0800 | [diff] [blame] | 3889 | netlbl_cache_add(skb, family, &skp->smk_netlabel); |
Casey Schaufler | 322dd63 | 2020-08-11 17:39:43 -0700 | [diff] [blame] | 3890 | } |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 3891 | |
| 3892 | netlbl_secattr_destroy(&secattr); |
| 3893 | |
| 3894 | return skp; |
| 3895 | } |
| 3896 | |
| 3897 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3898 | * smack_socket_sock_rcv_skb - Smack packet delivery access check |
| 3899 | * @sk: socket |
| 3900 | * @skb: packet |
| 3901 | * |
| 3902 | * Returns 0 if the packet should be delivered, an error code otherwise |
| 3903 | */ |
| 3904 | static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) |
| 3905 | { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3906 | struct socket_smack *ssp = sk->sk_security; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3907 | struct smack_known *skp = NULL; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3908 | int rc = 0; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3909 | struct smk_audit_info ad; |
Piotr Sawicki | 129a998 | 2018-07-19 11:42:58 +0200 | [diff] [blame] | 3910 | u16 family = sk->sk_family; |
Kees Cook | 923e9a1 | 2012-04-10 13:26:44 -0700 | [diff] [blame] | 3911 | #ifdef CONFIG_AUDIT |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 3912 | struct lsm_network_audit net; |
Kees Cook | 923e9a1 | 2012-04-10 13:26:44 -0700 | [diff] [blame] | 3913 | #endif |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3914 | #if IS_ENABLED(CONFIG_IPV6) |
| 3915 | struct sockaddr_in6 sadd; |
| 3916 | int proto; |
Piotr Sawicki | 129a998 | 2018-07-19 11:42:58 +0200 | [diff] [blame] | 3917 | |
| 3918 | if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) |
| 3919 | family = PF_INET; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3920 | #endif /* CONFIG_IPV6 */ |
| 3921 | |
Piotr Sawicki | 129a998 | 2018-07-19 11:42:58 +0200 | [diff] [blame] | 3922 | switch (family) { |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3923 | case PF_INET: |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3924 | /* |
| 3925 | * If there is a secmark use it rather than the CIPSO label. |
| 3926 | * If there is no secmark fall back to CIPSO. |
| 3927 | * The secmark is assumed to reflect policy better. |
| 3928 | */ |
Casey Schaufler | 36be812 | 2020-08-11 17:39:41 -0700 | [diff] [blame] | 3929 | skp = smack_from_skb(skb); |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 3930 | if (skp == NULL) { |
| 3931 | skp = smack_from_netlbl(sk, family, skb); |
| 3932 | if (skp == NULL) |
| 3933 | skp = smack_net_ambient; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3934 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3935 | |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3936 | #ifdef CONFIG_AUDIT |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3937 | smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); |
Piotr Sawicki | 129a998 | 2018-07-19 11:42:58 +0200 | [diff] [blame] | 3938 | ad.a.u.net->family = family; |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3939 | ad.a.u.net->netif = skb->skb_iif; |
| 3940 | ipv4_skb_to_auditdata(skb, &ad.a, NULL); |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 3941 | #endif |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3942 | /* |
| 3943 | * Receiving a packet requires that the other end |
| 3944 | * be able to write here. Read access is not required. |
| 3945 | * This is the simplist possible security model |
| 3946 | * for networking. |
| 3947 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 3948 | rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad); |
| 3949 | rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in, |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 3950 | MAY_WRITE, rc); |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3951 | if (rc != 0) |
Piotr Sawicki | 129a998 | 2018-07-19 11:42:58 +0200 | [diff] [blame] | 3952 | netlbl_skbuff_err(skb, family, rc, 0); |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3953 | break; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3954 | #if IS_ENABLED(CONFIG_IPV6) |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3955 | case PF_INET6: |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3956 | proto = smk_skb_to_addr_ipv6(skb, &sadd); |
Piotr Sawicki | a07ef95 | 2018-07-19 11:45:16 +0200 | [diff] [blame] | 3957 | if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE && |
| 3958 | proto != IPPROTO_TCP && proto != IPPROTO_DCCP) |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3959 | break; |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 3960 | #ifdef SMACK_IPV6_SECMARK_LABELING |
Casey Schaufler | 36be812 | 2020-08-11 17:39:41 -0700 | [diff] [blame] | 3961 | skp = smack_from_skb(skb); |
| 3962 | if (skp == NULL) { |
| 3963 | if (smk_ipv6_localhost(&sadd)) |
| 3964 | break; |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 3965 | skp = smack_ipv6host_label(&sadd); |
Casey Schaufler | 36be812 | 2020-08-11 17:39:41 -0700 | [diff] [blame] | 3966 | if (skp == NULL) |
| 3967 | skp = smack_net_ambient; |
| 3968 | } |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3969 | #ifdef CONFIG_AUDIT |
| 3970 | smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); |
Piotr Sawicki | 129a998 | 2018-07-19 11:42:58 +0200 | [diff] [blame] | 3971 | ad.a.u.net->family = family; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3972 | ad.a.u.net->netif = skb->skb_iif; |
| 3973 | ipv6_skb_to_auditdata(skb, &ad.a, NULL); |
| 3974 | #endif /* CONFIG_AUDIT */ |
| 3975 | rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad); |
| 3976 | rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in, |
| 3977 | MAY_WRITE, rc); |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 3978 | #endif /* SMACK_IPV6_SECMARK_LABELING */ |
| 3979 | #ifdef SMACK_IPV6_PORT_LABELING |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3980 | rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING); |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 3981 | #endif /* SMACK_IPV6_PORT_LABELING */ |
Piotr Sawicki | d66a8ac | 2018-07-19 11:47:31 +0200 | [diff] [blame] | 3982 | if (rc != 0) |
| 3983 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, |
| 3984 | ICMPV6_ADM_PROHIBITED, 0); |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3985 | break; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3986 | #endif /* CONFIG_IPV6 */ |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 3987 | } |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 3988 | |
Paul Moore | a813429 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 3989 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3990 | } |
| 3991 | |
| 3992 | /** |
| 3993 | * smack_socket_getpeersec_stream - pull in packet label |
| 3994 | * @sock: the socket |
| 3995 | * @optval: user's destination |
| 3996 | * @optlen: size thereof |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 3997 | * @len: max thereof |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 3998 | * |
| 3999 | * returns zero on success, an error code otherwise |
| 4000 | */ |
| 4001 | static int smack_socket_getpeersec_stream(struct socket *sock, |
| 4002 | char __user *optval, |
| 4003 | int __user *optlen, unsigned len) |
| 4004 | { |
| 4005 | struct socket_smack *ssp; |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 4006 | char *rcp = ""; |
| 4007 | int slen = 1; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4008 | int rc = 0; |
| 4009 | |
| 4010 | ssp = sock->sk->sk_security; |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 4011 | if (ssp->smk_packet != NULL) { |
Casey Schaufler | 54e70ec | 2014-04-10 16:37:08 -0700 | [diff] [blame] | 4012 | rcp = ssp->smk_packet->smk_known; |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 4013 | slen = strlen(rcp) + 1; |
| 4014 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4015 | |
| 4016 | if (slen > len) |
| 4017 | rc = -ERANGE; |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 4018 | else if (copy_to_user(optval, rcp, slen) != 0) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4019 | rc = -EFAULT; |
| 4020 | |
| 4021 | if (put_user(slen, optlen) != 0) |
| 4022 | rc = -EFAULT; |
| 4023 | |
| 4024 | return rc; |
| 4025 | } |
| 4026 | |
| 4027 | |
| 4028 | /** |
| 4029 | * smack_socket_getpeersec_dgram - pull in packet label |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 4030 | * @sock: the peer socket |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4031 | * @skb: packet data |
| 4032 | * @secid: pointer to where to put the secid of the packet |
| 4033 | * |
| 4034 | * Sets the netlabel socket state on sk from parent |
| 4035 | */ |
| 4036 | static int smack_socket_getpeersec_dgram(struct socket *sock, |
| 4037 | struct sk_buff *skb, u32 *secid) |
| 4038 | |
| 4039 | { |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 4040 | struct socket_smack *ssp = NULL; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4041 | struct smack_known *skp; |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 4042 | struct sock *sk = NULL; |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 4043 | int family = PF_UNSPEC; |
| 4044 | u32 s = 0; /* 0 is the invalid secid */ |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4045 | |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 4046 | if (skb != NULL) { |
| 4047 | if (skb->protocol == htons(ETH_P_IP)) |
| 4048 | family = PF_INET; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 4049 | #if IS_ENABLED(CONFIG_IPV6) |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 4050 | else if (skb->protocol == htons(ETH_P_IPV6)) |
| 4051 | family = PF_INET6; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 4052 | #endif /* CONFIG_IPV6 */ |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4053 | } |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 4054 | if (family == PF_UNSPEC && sock != NULL) |
| 4055 | family = sock->sk->sk_family; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4056 | |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 4057 | switch (family) { |
| 4058 | case PF_UNIX: |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 4059 | ssp = sock->sk->sk_security; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4060 | s = ssp->smk_out->smk_secid; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 4061 | break; |
| 4062 | case PF_INET: |
Casey Schaufler | 36be812 | 2020-08-11 17:39:41 -0700 | [diff] [blame] | 4063 | skp = smack_from_skb(skb); |
| 4064 | if (skp) { |
| 4065 | s = skp->smk_secid; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 4066 | break; |
Casey Schaufler | 36be812 | 2020-08-11 17:39:41 -0700 | [diff] [blame] | 4067 | } |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 4068 | /* |
| 4069 | * Translate what netlabel gave us. |
| 4070 | */ |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 4071 | if (sock != NULL) |
| 4072 | sk = sock->sk; |
| 4073 | skp = smack_from_netlbl(sk, family, skb); |
| 4074 | if (skp != NULL) |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4075 | s = skp->smk_secid; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 4076 | break; |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 4077 | case PF_INET6: |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 4078 | #ifdef SMACK_IPV6_SECMARK_LABELING |
Casey Schaufler | 36be812 | 2020-08-11 17:39:41 -0700 | [diff] [blame] | 4079 | skp = smack_from_skb(skb); |
| 4080 | if (skp) |
| 4081 | s = skp->smk_secid; |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 4082 | #endif |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 4083 | break; |
Casey Schaufler | b4e0d5f | 2010-11-24 17:12:10 -0800 | [diff] [blame] | 4084 | } |
| 4085 | *secid = s; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4086 | if (s == 0) |
| 4087 | return -EINVAL; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4088 | return 0; |
| 4089 | } |
| 4090 | |
| 4091 | /** |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 4092 | * smack_sock_graft - Initialize a newly created socket with an existing sock |
| 4093 | * @sk: child sock |
| 4094 | * @parent: parent socket |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4095 | * |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 4096 | * Set the smk_{in,out} state of an existing sock based on the process that |
| 4097 | * is creating the new socket. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4098 | */ |
| 4099 | static void smack_sock_graft(struct sock *sk, struct socket *parent) |
| 4100 | { |
| 4101 | struct socket_smack *ssp; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4102 | struct smack_known *skp = smk_of_current(); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4103 | |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 4104 | if (sk == NULL || |
| 4105 | (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4106 | return; |
| 4107 | |
| 4108 | ssp = sk->sk_security; |
Casey Schaufler | 54e70ec | 2014-04-10 16:37:08 -0700 | [diff] [blame] | 4109 | ssp->smk_in = skp; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4110 | ssp->smk_out = skp; |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 4111 | /* cssp->smk_packet is already set in smack_inet_csk_clone() */ |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4112 | } |
| 4113 | |
| 4114 | /** |
| 4115 | * smack_inet_conn_request - Smack access check on connect |
| 4116 | * @sk: socket involved |
| 4117 | * @skb: packet |
| 4118 | * @req: unused |
| 4119 | * |
| 4120 | * Returns 0 if a task with the packet label could write to |
| 4121 | * the socket, otherwise an error code |
| 4122 | */ |
Florian Westphal | 41dd959 | 2020-11-30 16:36:29 +0100 | [diff] [blame] | 4123 | static int smack_inet_conn_request(const struct sock *sk, struct sk_buff *skb, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4124 | struct request_sock *req) |
| 4125 | { |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 4126 | u16 family = sk->sk_family; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 4127 | struct smack_known *skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4128 | struct socket_smack *ssp = sk->sk_security; |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 4129 | struct sockaddr_in addr; |
| 4130 | struct iphdr *hdr; |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 4131 | struct smack_known *hskp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4132 | int rc; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 4133 | struct smk_audit_info ad; |
Kees Cook | 923e9a1 | 2012-04-10 13:26:44 -0700 | [diff] [blame] | 4134 | #ifdef CONFIG_AUDIT |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4135 | struct lsm_network_audit net; |
Kees Cook | 923e9a1 | 2012-04-10 13:26:44 -0700 | [diff] [blame] | 4136 | #endif |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4137 | |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 4138 | #if IS_ENABLED(CONFIG_IPV6) |
Casey Schaufler | c673944 | 2013-05-22 18:42:56 -0700 | [diff] [blame] | 4139 | if (family == PF_INET6) { |
| 4140 | /* |
| 4141 | * Handle mapped IPv4 packets arriving |
| 4142 | * via IPv6 sockets. Don't set up netlabel |
| 4143 | * processing on IPv6. |
| 4144 | */ |
| 4145 | if (skb->protocol == htons(ETH_P_IP)) |
| 4146 | family = PF_INET; |
| 4147 | else |
| 4148 | return 0; |
| 4149 | } |
Casey Schaufler | 69f287a | 2014-12-12 17:08:40 -0800 | [diff] [blame] | 4150 | #endif /* CONFIG_IPV6 */ |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4151 | |
Casey Schaufler | 7f368ad | 2015-02-11 12:52:32 -0800 | [diff] [blame] | 4152 | /* |
| 4153 | * If there is a secmark use it rather than the CIPSO label. |
| 4154 | * If there is no secmark fall back to CIPSO. |
| 4155 | * The secmark is assumed to reflect policy better. |
| 4156 | */ |
Casey Schaufler | 36be812 | 2020-08-11 17:39:41 -0700 | [diff] [blame] | 4157 | skp = smack_from_skb(skb); |
Casey Schaufler | a2af031 | 2020-08-11 17:39:42 -0700 | [diff] [blame] | 4158 | if (skp == NULL) { |
| 4159 | skp = smack_from_netlbl(sk, family, skb); |
| 4160 | if (skp == NULL) |
| 4161 | skp = &smack_known_huh; |
Casey Schaufler | 7f368ad | 2015-02-11 12:52:32 -0800 | [diff] [blame] | 4162 | } |
Casey Schaufler | 7f368ad | 2015-02-11 12:52:32 -0800 | [diff] [blame] | 4163 | |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 4164 | #ifdef CONFIG_AUDIT |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4165 | smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); |
| 4166 | ad.a.u.net->family = family; |
| 4167 | ad.a.u.net->netif = skb->skb_iif; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 4168 | ipv4_skb_to_auditdata(skb, &ad.a, NULL); |
| 4169 | #endif |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4170 | /* |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 4171 | * Receiving a packet requires that the other end be able to write |
| 4172 | * here. Read access is not required. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4173 | */ |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 4174 | rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad); |
| 4175 | rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc); |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 4176 | if (rc != 0) |
| 4177 | return rc; |
| 4178 | |
| 4179 | /* |
| 4180 | * Save the peer's label in the request_sock so we can later setup |
| 4181 | * smk_packet in the child socket so that SO_PEERCRED can report it. |
| 4182 | */ |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4183 | req->peer_secid = skp->smk_secid; |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 4184 | |
| 4185 | /* |
| 4186 | * We need to decide if we want to label the incoming connection here |
| 4187 | * if we do we only need to label the request_sock and the stack will |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4188 | * propagate the wire-label to the sock when it is created. |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 4189 | */ |
| 4190 | hdr = ip_hdr(skb); |
| 4191 | addr.sin_addr.s_addr = hdr->saddr; |
| 4192 | rcu_read_lock(); |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 4193 | hskp = smack_ipv4host_label(&addr); |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 4194 | rcu_read_unlock(); |
| 4195 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 4196 | if (hskp == NULL) |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 4197 | rc = netlbl_req_setattr(req, &skp->smk_netlabel); |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4198 | else |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 4199 | netlbl_req_delattr(req); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4200 | |
| 4201 | return rc; |
| 4202 | } |
| 4203 | |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 4204 | /** |
| 4205 | * smack_inet_csk_clone - Copy the connection information to the new socket |
| 4206 | * @sk: the new socket |
| 4207 | * @req: the connection's request_sock |
| 4208 | * |
| 4209 | * Transfer the connection's peer label to the newly created socket. |
| 4210 | */ |
| 4211 | static void smack_inet_csk_clone(struct sock *sk, |
| 4212 | const struct request_sock *req) |
| 4213 | { |
| 4214 | struct socket_smack *ssp = sk->sk_security; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4215 | struct smack_known *skp; |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 4216 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4217 | if (req->peer_secid != 0) { |
| 4218 | skp = smack_from_secid(req->peer_secid); |
Casey Schaufler | 54e70ec | 2014-04-10 16:37:08 -0700 | [diff] [blame] | 4219 | ssp->smk_packet = skp; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4220 | } else |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 4221 | ssp->smk_packet = NULL; |
Paul Moore | 07feee8 | 2009-03-27 17:10:54 -0400 | [diff] [blame] | 4222 | } |
| 4223 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4224 | /* |
| 4225 | * Key management security hooks |
| 4226 | * |
| 4227 | * Casey has not tested key support very heavily. |
| 4228 | * The permission check is most likely too restrictive. |
| 4229 | * If you care about keys please have a look. |
| 4230 | */ |
| 4231 | #ifdef CONFIG_KEYS |
| 4232 | |
| 4233 | /** |
| 4234 | * smack_key_alloc - Set the key security blob |
| 4235 | * @key: object |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 4236 | * @cred: the credentials to use |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4237 | * @flags: unused |
| 4238 | * |
| 4239 | * No allocation required |
| 4240 | * |
| 4241 | * Returns 0 |
| 4242 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 4243 | static int smack_key_alloc(struct key *key, const struct cred *cred, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4244 | unsigned long flags) |
| 4245 | { |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 4246 | struct smack_known *skp = smk_of_task(smack_cred(cred)); |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4247 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 4248 | key->security = skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4249 | return 0; |
| 4250 | } |
| 4251 | |
| 4252 | /** |
| 4253 | * smack_key_free - Clear the key security blob |
| 4254 | * @key: the object |
| 4255 | * |
| 4256 | * Clear the blob pointer |
| 4257 | */ |
| 4258 | static void smack_key_free(struct key *key) |
| 4259 | { |
| 4260 | key->security = NULL; |
| 4261 | } |
| 4262 | |
Lukasz Pawelczyk | 1a28979 | 2014-11-26 15:31:06 +0100 | [diff] [blame] | 4263 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4264 | * smack_key_permission - Smack access on a key |
| 4265 | * @key_ref: gets to the object |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 4266 | * @cred: the credentials to use |
David Howells | 8c0637e | 2020-05-12 15:16:29 +0100 | [diff] [blame] | 4267 | * @need_perm: requested key permission |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4268 | * |
| 4269 | * Return 0 if the task has read and write to the object, |
| 4270 | * an error code otherwise |
| 4271 | */ |
| 4272 | static int smack_key_permission(key_ref_t key_ref, |
David Howells | 8c0637e | 2020-05-12 15:16:29 +0100 | [diff] [blame] | 4273 | const struct cred *cred, |
| 4274 | enum key_need_perm need_perm) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4275 | { |
| 4276 | struct key *keyp; |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 4277 | struct smk_audit_info ad; |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 4278 | struct smack_known *tkp = smk_of_task(smack_cred(cred)); |
Dmitry Kasatkin | fffea21 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 4279 | int request = 0; |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 4280 | int rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4281 | |
Zoran Markovic | 5b841bf | 2018-10-17 16:25:44 -0700 | [diff] [blame] | 4282 | /* |
| 4283 | * Validate requested permissions |
| 4284 | */ |
David Howells | 8c0637e | 2020-05-12 15:16:29 +0100 | [diff] [blame] | 4285 | switch (need_perm) { |
| 4286 | case KEY_NEED_READ: |
| 4287 | case KEY_NEED_SEARCH: |
| 4288 | case KEY_NEED_VIEW: |
| 4289 | request |= MAY_READ; |
| 4290 | break; |
| 4291 | case KEY_NEED_WRITE: |
| 4292 | case KEY_NEED_LINK: |
| 4293 | case KEY_NEED_SETATTR: |
| 4294 | request |= MAY_WRITE; |
| 4295 | break; |
| 4296 | case KEY_NEED_UNSPECIFIED: |
| 4297 | case KEY_NEED_UNLINK: |
| 4298 | case KEY_SYSADMIN_OVERRIDE: |
| 4299 | case KEY_AUTHTOKEN_OVERRIDE: |
| 4300 | case KEY_DEFER_PERM_CHECK: |
| 4301 | return 0; |
| 4302 | default: |
Zoran Markovic | 5b841bf | 2018-10-17 16:25:44 -0700 | [diff] [blame] | 4303 | return -EINVAL; |
David Howells | 8c0637e | 2020-05-12 15:16:29 +0100 | [diff] [blame] | 4304 | } |
Zoran Markovic | 5b841bf | 2018-10-17 16:25:44 -0700 | [diff] [blame] | 4305 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4306 | keyp = key_ref_to_ptr(key_ref); |
| 4307 | if (keyp == NULL) |
| 4308 | return -EINVAL; |
| 4309 | /* |
| 4310 | * If the key hasn't been initialized give it access so that |
| 4311 | * it may do so. |
| 4312 | */ |
| 4313 | if (keyp->security == NULL) |
| 4314 | return 0; |
| 4315 | /* |
| 4316 | * This should not occur |
| 4317 | */ |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4318 | if (tkp == NULL) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4319 | return -EACCES; |
Casey Schaufler | d19dfe5 | 2018-01-08 10:25:32 -0800 | [diff] [blame] | 4320 | |
David Howells | a8478a6 | 2020-01-14 17:07:13 +0000 | [diff] [blame] | 4321 | if (smack_privileged(CAP_MAC_OVERRIDE)) |
Casey Schaufler | d19dfe5 | 2018-01-08 10:25:32 -0800 | [diff] [blame] | 4322 | return 0; |
| 4323 | |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 4324 | #ifdef CONFIG_AUDIT |
| 4325 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY); |
| 4326 | ad.a.u.key_struct.key = keyp->serial; |
| 4327 | ad.a.u.key_struct.key_desc = keyp->description; |
| 4328 | #endif |
Casey Schaufler | d166c80 | 2014-08-27 14:51:27 -0700 | [diff] [blame] | 4329 | rc = smk_access(tkp, keyp->security, request, &ad); |
| 4330 | rc = smk_bu_note("key access", tkp, keyp->security, request, rc); |
| 4331 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4332 | } |
José Bollo | 7fc5f36 | 2015-02-17 15:41:22 +0100 | [diff] [blame] | 4333 | |
| 4334 | /* |
| 4335 | * smack_key_getsecurity - Smack label tagging the key |
| 4336 | * @key points to the key to be queried |
| 4337 | * @_buffer points to a pointer that should be set to point to the |
| 4338 | * resulting string (if no label or an error occurs). |
| 4339 | * Return the length of the string (including terminating NUL) or -ve if |
| 4340 | * an error. |
| 4341 | * May also return 0 (and a NULL buffer pointer) if there is no label. |
| 4342 | */ |
| 4343 | static int smack_key_getsecurity(struct key *key, char **_buffer) |
| 4344 | { |
| 4345 | struct smack_known *skp = key->security; |
| 4346 | size_t length; |
| 4347 | char *copy; |
| 4348 | |
| 4349 | if (key->security == NULL) { |
| 4350 | *_buffer = NULL; |
| 4351 | return 0; |
| 4352 | } |
| 4353 | |
| 4354 | copy = kstrdup(skp->smk_known, GFP_KERNEL); |
| 4355 | if (copy == NULL) |
| 4356 | return -ENOMEM; |
| 4357 | length = strlen(copy) + 1; |
| 4358 | |
| 4359 | *_buffer = copy; |
| 4360 | return length; |
| 4361 | } |
| 4362 | |
David Howells | a8478a6 | 2020-01-14 17:07:13 +0000 | [diff] [blame] | 4363 | |
| 4364 | #ifdef CONFIG_KEY_NOTIFICATIONS |
| 4365 | /** |
| 4366 | * smack_watch_key - Smack access to watch a key for notifications. |
| 4367 | * @key: The key to be watched |
| 4368 | * |
| 4369 | * Return 0 if the @watch->cred has permission to read from the key object and |
| 4370 | * an error otherwise. |
| 4371 | */ |
| 4372 | static int smack_watch_key(struct key *key) |
| 4373 | { |
| 4374 | struct smk_audit_info ad; |
| 4375 | struct smack_known *tkp = smk_of_current(); |
| 4376 | int rc; |
| 4377 | |
| 4378 | if (key == NULL) |
| 4379 | return -EINVAL; |
| 4380 | /* |
| 4381 | * If the key hasn't been initialized give it access so that |
| 4382 | * it may do so. |
| 4383 | */ |
| 4384 | if (key->security == NULL) |
| 4385 | return 0; |
| 4386 | /* |
| 4387 | * This should not occur |
| 4388 | */ |
| 4389 | if (tkp == NULL) |
| 4390 | return -EACCES; |
| 4391 | |
| 4392 | if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred())) |
| 4393 | return 0; |
| 4394 | |
| 4395 | #ifdef CONFIG_AUDIT |
| 4396 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY); |
| 4397 | ad.a.u.key_struct.key = key->serial; |
| 4398 | ad.a.u.key_struct.key_desc = key->description; |
| 4399 | #endif |
| 4400 | rc = smk_access(tkp, key->security, MAY_READ, &ad); |
| 4401 | rc = smk_bu_note("key watch", tkp, key->security, MAY_READ, rc); |
| 4402 | return rc; |
| 4403 | } |
| 4404 | #endif /* CONFIG_KEY_NOTIFICATIONS */ |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4405 | #endif /* CONFIG_KEYS */ |
| 4406 | |
David Howells | a8478a6 | 2020-01-14 17:07:13 +0000 | [diff] [blame] | 4407 | #ifdef CONFIG_WATCH_QUEUE |
| 4408 | /** |
| 4409 | * smack_post_notification - Smack access to post a notification to a queue |
| 4410 | * @w_cred: The credentials of the watcher. |
| 4411 | * @cred: The credentials of the event source (may be NULL). |
| 4412 | * @n: The notification message to be posted. |
| 4413 | */ |
| 4414 | static int smack_post_notification(const struct cred *w_cred, |
| 4415 | const struct cred *cred, |
| 4416 | struct watch_notification *n) |
| 4417 | { |
| 4418 | struct smk_audit_info ad; |
| 4419 | struct smack_known *subj, *obj; |
| 4420 | int rc; |
| 4421 | |
| 4422 | /* Always let maintenance notifications through. */ |
| 4423 | if (n->type == WATCH_TYPE_META) |
| 4424 | return 0; |
| 4425 | |
| 4426 | if (!cred) |
| 4427 | return 0; |
| 4428 | subj = smk_of_task(smack_cred(cred)); |
| 4429 | obj = smk_of_task(smack_cred(w_cred)); |
| 4430 | |
| 4431 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NOTIFICATION); |
| 4432 | rc = smk_access(subj, obj, MAY_WRITE, &ad); |
| 4433 | rc = smk_bu_note("notification", subj, obj, MAY_WRITE, rc); |
| 4434 | return rc; |
| 4435 | } |
| 4436 | #endif /* CONFIG_WATCH_QUEUE */ |
| 4437 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4438 | /* |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4439 | * Smack Audit hooks |
| 4440 | * |
| 4441 | * Audit requires a unique representation of each Smack specific |
| 4442 | * rule. This unique representation is used to distinguish the |
| 4443 | * object to be audited from remaining kernel objects and also |
| 4444 | * works as a glue between the audit hooks. |
| 4445 | * |
| 4446 | * Since repository entries are added but never deleted, we'll use |
| 4447 | * the smack_known label address related to the given audit rule as |
| 4448 | * the needed unique representation. This also better fits the smack |
| 4449 | * model where nearly everything is a label. |
| 4450 | */ |
| 4451 | #ifdef CONFIG_AUDIT |
| 4452 | |
| 4453 | /** |
| 4454 | * smack_audit_rule_init - Initialize a smack audit rule |
| 4455 | * @field: audit rule fields given from user-space (audit.h) |
| 4456 | * @op: required testing operator (=, !=, >, <, ...) |
| 4457 | * @rulestr: smack label to be audited |
| 4458 | * @vrule: pointer to save our own audit rule representation |
| 4459 | * |
| 4460 | * Prepare to audit cases where (@field @op @rulestr) is true. |
| 4461 | * The label to be audited is created if necessay. |
| 4462 | */ |
| 4463 | static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) |
| 4464 | { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 4465 | struct smack_known *skp; |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4466 | char **rule = (char **)vrule; |
| 4467 | *rule = NULL; |
| 4468 | |
| 4469 | if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER) |
| 4470 | return -EINVAL; |
| 4471 | |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 4472 | if (op != Audit_equal && op != Audit_not_equal) |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4473 | return -EINVAL; |
| 4474 | |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 4475 | skp = smk_import_entry(rulestr, 0); |
Lukasz Pawelczyk | e774ad6 | 2015-04-20 17:12:54 +0200 | [diff] [blame] | 4476 | if (IS_ERR(skp)) |
| 4477 | return PTR_ERR(skp); |
| 4478 | |
| 4479 | *rule = skp->smk_known; |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4480 | |
| 4481 | return 0; |
| 4482 | } |
| 4483 | |
| 4484 | /** |
| 4485 | * smack_audit_rule_known - Distinguish Smack audit rules |
| 4486 | * @krule: rule of interest, in Audit kernel representation format |
| 4487 | * |
| 4488 | * This is used to filter Smack rules from remaining Audit ones. |
| 4489 | * If it's proved that this rule belongs to us, the |
| 4490 | * audit_rule_match hook will be called to do the final judgement. |
| 4491 | */ |
| 4492 | static int smack_audit_rule_known(struct audit_krule *krule) |
| 4493 | { |
| 4494 | struct audit_field *f; |
| 4495 | int i; |
| 4496 | |
| 4497 | for (i = 0; i < krule->field_count; i++) { |
| 4498 | f = &krule->fields[i]; |
| 4499 | |
| 4500 | if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER) |
| 4501 | return 1; |
| 4502 | } |
| 4503 | |
| 4504 | return 0; |
| 4505 | } |
| 4506 | |
| 4507 | /** |
| 4508 | * smack_audit_rule_match - Audit given object ? |
| 4509 | * @secid: security id for identifying the object to test |
| 4510 | * @field: audit rule flags given from user-space |
| 4511 | * @op: required testing operator |
| 4512 | * @vrule: smack internal rule presentation |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4513 | * |
| 4514 | * The core Audit hook. It's used to take the decision of |
| 4515 | * whether to audit or not to audit a given object. |
| 4516 | */ |
Richard Guy Briggs | 90462a5 | 2019-01-31 11:52:11 -0500 | [diff] [blame] | 4517 | static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule) |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4518 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4519 | struct smack_known *skp; |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4520 | char *rule = vrule; |
| 4521 | |
Richard Guy Briggs | 4eb0f4a | 2013-11-21 13:57:33 -0500 | [diff] [blame] | 4522 | if (unlikely(!rule)) { |
| 4523 | WARN_ONCE(1, "Smack: missing rule\n"); |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4524 | return -ENOENT; |
| 4525 | } |
| 4526 | |
| 4527 | if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER) |
| 4528 | return 0; |
| 4529 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4530 | skp = smack_from_secid(secid); |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4531 | |
| 4532 | /* |
| 4533 | * No need to do string comparisons. If a match occurs, |
| 4534 | * both pointers will point to the same smack_known |
| 4535 | * label. |
| 4536 | */ |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 4537 | if (op == Audit_equal) |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4538 | return (rule == skp->smk_known); |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 4539 | if (op == Audit_not_equal) |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4540 | return (rule != skp->smk_known); |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4541 | |
| 4542 | return 0; |
| 4543 | } |
| 4544 | |
Casey Schaufler | 491a0b0 | 2016-01-26 15:08:35 -0800 | [diff] [blame] | 4545 | /* |
| 4546 | * There is no need for a smack_audit_rule_free hook. |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4547 | * No memory was allocated. |
| 4548 | */ |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4549 | |
| 4550 | #endif /* CONFIG_AUDIT */ |
| 4551 | |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 4552 | /** |
David Quigley | 746df9b | 2013-05-22 12:50:35 -0400 | [diff] [blame] | 4553 | * smack_ismaclabel - check if xattr @name references a smack MAC label |
| 4554 | * @name: Full xattr name to check. |
| 4555 | */ |
| 4556 | static int smack_ismaclabel(const char *name) |
| 4557 | { |
| 4558 | return (strcmp(name, XATTR_SMACK_SUFFIX) == 0); |
| 4559 | } |
| 4560 | |
| 4561 | |
| 4562 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4563 | * smack_secid_to_secctx - return the smack label for a secid |
| 4564 | * @secid: incoming integer |
| 4565 | * @secdata: destination |
| 4566 | * @seclen: how long it is |
| 4567 | * |
| 4568 | * Exists for networking code. |
| 4569 | */ |
| 4570 | static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) |
| 4571 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4572 | struct smack_known *skp = smack_from_secid(secid); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4573 | |
Eric Paris | d5630b9 | 2010-10-13 16:24:48 -0400 | [diff] [blame] | 4574 | if (secdata) |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 4575 | *secdata = skp->smk_known; |
| 4576 | *seclen = strlen(skp->smk_known); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4577 | return 0; |
| 4578 | } |
| 4579 | |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 4580 | /** |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 4581 | * smack_secctx_to_secid - return the secid for a smack label |
| 4582 | * @secdata: smack label |
| 4583 | * @seclen: how long result is |
| 4584 | * @secid: outgoing integer |
| 4585 | * |
| 4586 | * Exists for audit and networking code. |
| 4587 | */ |
David Howells | e52c1764 | 2008-04-29 20:52:51 +0100 | [diff] [blame] | 4588 | static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 4589 | { |
Lukasz Pawelczyk | 21c7eae | 2014-08-29 17:02:55 +0200 | [diff] [blame] | 4590 | struct smack_known *skp = smk_find_entry(secdata); |
| 4591 | |
| 4592 | if (skp) |
| 4593 | *secid = skp->smk_secid; |
| 4594 | else |
| 4595 | *secid = 0; |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 4596 | return 0; |
| 4597 | } |
| 4598 | |
Casey Schaufler | 491a0b0 | 2016-01-26 15:08:35 -0800 | [diff] [blame] | 4599 | /* |
| 4600 | * There used to be a smack_release_secctx hook |
| 4601 | * that did nothing back when hooks were in a vector. |
| 4602 | * Now that there's a list such a hook adds cost. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4603 | */ |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4604 | |
David P. Quigley | 1ee65e3 | 2009-09-03 14:25:57 -0400 | [diff] [blame] | 4605 | static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) |
| 4606 | { |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 4607 | return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, |
| 4608 | ctxlen, 0); |
David P. Quigley | 1ee65e3 | 2009-09-03 14:25:57 -0400 | [diff] [blame] | 4609 | } |
| 4610 | |
| 4611 | static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) |
| 4612 | { |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 4613 | return __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_SMACK, |
| 4614 | ctx, ctxlen, 0); |
David P. Quigley | 1ee65e3 | 2009-09-03 14:25:57 -0400 | [diff] [blame] | 4615 | } |
| 4616 | |
| 4617 | static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) |
| 4618 | { |
Casey Schaufler | 0f8983c | 2018-06-01 10:45:12 -0700 | [diff] [blame] | 4619 | struct smack_known *skp = smk_of_inode(inode); |
David P. Quigley | 1ee65e3 | 2009-09-03 14:25:57 -0400 | [diff] [blame] | 4620 | |
Casey Schaufler | 0f8983c | 2018-06-01 10:45:12 -0700 | [diff] [blame] | 4621 | *ctx = skp->smk_known; |
| 4622 | *ctxlen = strlen(skp->smk_known); |
David P. Quigley | 1ee65e3 | 2009-09-03 14:25:57 -0400 | [diff] [blame] | 4623 | return 0; |
| 4624 | } |
| 4625 | |
Casey Schaufler | d6d80cb | 2017-09-28 14:54:50 -0700 | [diff] [blame] | 4626 | static int smack_inode_copy_up(struct dentry *dentry, struct cred **new) |
| 4627 | { |
| 4628 | |
| 4629 | struct task_smack *tsp; |
| 4630 | struct smack_known *skp; |
| 4631 | struct inode_smack *isp; |
| 4632 | struct cred *new_creds = *new; |
| 4633 | |
| 4634 | if (new_creds == NULL) { |
| 4635 | new_creds = prepare_creds(); |
| 4636 | if (new_creds == NULL) |
| 4637 | return -ENOMEM; |
| 4638 | } |
| 4639 | |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 4640 | tsp = smack_cred(new_creds); |
Casey Schaufler | d6d80cb | 2017-09-28 14:54:50 -0700 | [diff] [blame] | 4641 | |
| 4642 | /* |
| 4643 | * Get label from overlay inode and set it in create_sid |
| 4644 | */ |
Vishal Goel | 387ef96 | 2021-09-17 13:08:14 +0530 | [diff] [blame] | 4645 | isp = smack_inode(d_inode(dentry)); |
Casey Schaufler | d6d80cb | 2017-09-28 14:54:50 -0700 | [diff] [blame] | 4646 | skp = isp->smk_inode; |
| 4647 | tsp->smk_task = skp; |
| 4648 | *new = new_creds; |
| 4649 | return 0; |
| 4650 | } |
| 4651 | |
| 4652 | static int smack_inode_copy_up_xattr(const char *name) |
| 4653 | { |
| 4654 | /* |
| 4655 | * Return 1 if this is the smack access Smack attribute. |
| 4656 | */ |
| 4657 | if (strcmp(name, XATTR_NAME_SMACK) == 0) |
| 4658 | return 1; |
| 4659 | |
| 4660 | return -EOPNOTSUPP; |
| 4661 | } |
| 4662 | |
| 4663 | static int smack_dentry_create_files_as(struct dentry *dentry, int mode, |
| 4664 | struct qstr *name, |
| 4665 | const struct cred *old, |
| 4666 | struct cred *new) |
| 4667 | { |
Casey Schaufler | b17103a | 2018-11-09 16:12:56 -0800 | [diff] [blame] | 4668 | struct task_smack *otsp = smack_cred(old); |
| 4669 | struct task_smack *ntsp = smack_cred(new); |
Casey Schaufler | d6d80cb | 2017-09-28 14:54:50 -0700 | [diff] [blame] | 4670 | struct inode_smack *isp; |
| 4671 | int may; |
| 4672 | |
| 4673 | /* |
| 4674 | * Use the process credential unless all of |
| 4675 | * the transmuting criteria are met |
| 4676 | */ |
| 4677 | ntsp->smk_task = otsp->smk_task; |
| 4678 | |
| 4679 | /* |
| 4680 | * the attribute of the containing directory |
| 4681 | */ |
Casey Schaufler | fb4021b | 2018-11-12 12:43:01 -0800 | [diff] [blame] | 4682 | isp = smack_inode(d_inode(dentry->d_parent)); |
Casey Schaufler | d6d80cb | 2017-09-28 14:54:50 -0700 | [diff] [blame] | 4683 | |
| 4684 | if (isp->smk_flags & SMK_INODE_TRANSMUTE) { |
| 4685 | rcu_read_lock(); |
| 4686 | may = smk_access_entry(otsp->smk_task->smk_known, |
| 4687 | isp->smk_inode->smk_known, |
| 4688 | &otsp->smk_task->smk_rules); |
| 4689 | rcu_read_unlock(); |
| 4690 | |
| 4691 | /* |
| 4692 | * If the directory is transmuting and the rule |
| 4693 | * providing access is transmuting use the containing |
| 4694 | * directory label instead of the process label. |
| 4695 | */ |
| 4696 | if (may > 0 && (may & MAY_TRANSMUTE)) |
| 4697 | ntsp->smk_task = isp->smk_inode; |
| 4698 | } |
| 4699 | return 0; |
| 4700 | } |
| 4701 | |
Casey Schaufler | d9d8c93 | 2021-05-21 11:21:16 -0700 | [diff] [blame] | 4702 | #ifdef CONFIG_IO_URING |
| 4703 | /** |
| 4704 | * smack_uring_override_creds - Is io_uring cred override allowed? |
| 4705 | * @new: the target creds |
| 4706 | * |
| 4707 | * Check to see if the current task is allowed to override it's credentials |
| 4708 | * to service an io_uring operation. |
| 4709 | */ |
| 4710 | static int smack_uring_override_creds(const struct cred *new) |
| 4711 | { |
| 4712 | struct task_smack *tsp = smack_cred(current_cred()); |
| 4713 | struct task_smack *nsp = smack_cred(new); |
| 4714 | |
| 4715 | /* |
| 4716 | * Allow the degenerate case where the new Smack value is |
| 4717 | * the same as the current Smack value. |
| 4718 | */ |
| 4719 | if (tsp->smk_task == nsp->smk_task) |
| 4720 | return 0; |
| 4721 | |
| 4722 | if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred())) |
| 4723 | return 0; |
| 4724 | |
| 4725 | return -EPERM; |
| 4726 | } |
| 4727 | |
| 4728 | /** |
| 4729 | * smack_uring_sqpoll - check if a io_uring polling thread can be created |
| 4730 | * |
| 4731 | * Check to see if the current task is allowed to create a new io_uring |
| 4732 | * kernel polling thread. |
| 4733 | */ |
| 4734 | static int smack_uring_sqpoll(void) |
| 4735 | { |
| 4736 | if (smack_privileged_cred(CAP_MAC_ADMIN, current_cred())) |
| 4737 | return 0; |
| 4738 | |
| 4739 | return -EPERM; |
| 4740 | } |
| 4741 | |
| 4742 | #endif /* CONFIG_IO_URING */ |
| 4743 | |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 4744 | struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = { |
| 4745 | .lbs_cred = sizeof(struct task_smack), |
Casey Schaufler | 33bf60c | 2018-11-12 12:02:49 -0800 | [diff] [blame] | 4746 | .lbs_file = sizeof(struct smack_known *), |
Casey Schaufler | afb1cbe3 | 2018-09-21 17:19:29 -0700 | [diff] [blame] | 4747 | .lbs_inode = sizeof(struct inode_smack), |
Casey Schaufler | ecd5f82 | 2018-11-20 11:55:02 -0800 | [diff] [blame] | 4748 | .lbs_ipc = sizeof(struct smack_known *), |
| 4749 | .lbs_msg_msg = sizeof(struct smack_known *), |
Casey Schaufler | 1aea780 | 2021-04-22 17:41:15 +0200 | [diff] [blame] | 4750 | .lbs_superblock = sizeof(struct superblock_smack), |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 4751 | }; |
| 4752 | |
James Morris | ca97d93 | 2017-02-15 00:18:51 +1100 | [diff] [blame] | 4753 | static struct security_hook_list smack_hooks[] __lsm_ro_after_init = { |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4754 | LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check), |
| 4755 | LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme), |
| 4756 | LSM_HOOK_INIT(syslog, smack_syslog), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4757 | |
Al Viro | 0b52075 | 2018-12-23 16:02:47 -0500 | [diff] [blame] | 4758 | LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup), |
David Howells | 2febd25 | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 4759 | LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param), |
| 4760 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4761 | LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security), |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 4762 | LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts), |
Al Viro | 5b40023 | 2018-12-12 20:13:29 -0500 | [diff] [blame] | 4763 | LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4764 | LSM_HOOK_INIT(sb_statfs, smack_sb_statfs), |
Vivek Trivedi | 3bf2789 | 2015-06-22 15:36:06 +0530 | [diff] [blame] | 4765 | LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4766 | |
Eric W. Biederman | b8bff59 | 2020-03-22 15:46:24 -0500 | [diff] [blame] | 4767 | LSM_HOOK_INIT(bprm_creds_for_exec, smack_bprm_creds_for_exec), |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 4768 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4769 | LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4770 | LSM_HOOK_INIT(inode_init_security, smack_inode_init_security), |
| 4771 | LSM_HOOK_INIT(inode_link, smack_inode_link), |
| 4772 | LSM_HOOK_INIT(inode_unlink, smack_inode_unlink), |
| 4773 | LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir), |
| 4774 | LSM_HOOK_INIT(inode_rename, smack_inode_rename), |
| 4775 | LSM_HOOK_INIT(inode_permission, smack_inode_permission), |
| 4776 | LSM_HOOK_INIT(inode_setattr, smack_inode_setattr), |
| 4777 | LSM_HOOK_INIT(inode_getattr, smack_inode_getattr), |
| 4778 | LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr), |
| 4779 | LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr), |
| 4780 | LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr), |
| 4781 | LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr), |
| 4782 | LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity), |
| 4783 | LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity), |
| 4784 | LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity), |
| 4785 | LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4786 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4787 | LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4788 | LSM_HOOK_INIT(file_ioctl, smack_file_ioctl), |
| 4789 | LSM_HOOK_INIT(file_lock, smack_file_lock), |
| 4790 | LSM_HOOK_INIT(file_fcntl, smack_file_fcntl), |
| 4791 | LSM_HOOK_INIT(mmap_file, smack_mmap_file), |
| 4792 | LSM_HOOK_INIT(mmap_addr, cap_mmap_addr), |
| 4793 | LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner), |
| 4794 | LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask), |
| 4795 | LSM_HOOK_INIT(file_receive, smack_file_receive), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4796 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4797 | LSM_HOOK_INIT(file_open, smack_file_open), |
Casey Schaufler | 531f1d4 | 2011-09-19 12:41:42 -0700 | [diff] [blame] | 4798 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4799 | LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank), |
| 4800 | LSM_HOOK_INIT(cred_free, smack_cred_free), |
| 4801 | LSM_HOOK_INIT(cred_prepare, smack_cred_prepare), |
| 4802 | LSM_HOOK_INIT(cred_transfer, smack_cred_transfer), |
Matthew Garrett | 3ec3011 | 2018-01-08 13:36:19 -0800 | [diff] [blame] | 4803 | LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4804 | LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as), |
| 4805 | LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as), |
| 4806 | LSM_HOOK_INIT(task_setpgid, smack_task_setpgid), |
| 4807 | LSM_HOOK_INIT(task_getpgid, smack_task_getpgid), |
| 4808 | LSM_HOOK_INIT(task_getsid, smack_task_getsid), |
Paul Moore | 6326948 | 2021-09-29 11:01:21 -0400 | [diff] [blame] | 4809 | LSM_HOOK_INIT(current_getsecid_subj, smack_current_getsecid_subj), |
Paul Moore | 1fb057d | 2021-02-19 15:04:58 -0500 | [diff] [blame] | 4810 | LSM_HOOK_INIT(task_getsecid_obj, smack_task_getsecid_obj), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4811 | LSM_HOOK_INIT(task_setnice, smack_task_setnice), |
| 4812 | LSM_HOOK_INIT(task_setioprio, smack_task_setioprio), |
| 4813 | LSM_HOOK_INIT(task_getioprio, smack_task_getioprio), |
| 4814 | LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler), |
| 4815 | LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler), |
| 4816 | LSM_HOOK_INIT(task_movememory, smack_task_movememory), |
| 4817 | LSM_HOOK_INIT(task_kill, smack_task_kill), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4818 | LSM_HOOK_INIT(task_to_inode, smack_task_to_inode), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4819 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4820 | LSM_HOOK_INIT(ipc_permission, smack_ipc_permission), |
| 4821 | LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4822 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4823 | LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4824 | |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 4825 | LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4826 | LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate), |
| 4827 | LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl), |
| 4828 | LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd), |
| 4829 | LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4830 | |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 4831 | LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4832 | LSM_HOOK_INIT(shm_associate, smack_shm_associate), |
| 4833 | LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl), |
| 4834 | LSM_HOOK_INIT(shm_shmat, smack_shm_shmat), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4835 | |
Eric W. Biederman | 0d79cbf | 2018-03-23 23:56:19 -0500 | [diff] [blame] | 4836 | LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4837 | LSM_HOOK_INIT(sem_associate, smack_sem_associate), |
| 4838 | LSM_HOOK_INIT(sem_semctl, smack_sem_semctl), |
| 4839 | LSM_HOOK_INIT(sem_semop, smack_sem_semop), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4840 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4841 | LSM_HOOK_INIT(d_instantiate, smack_d_instantiate), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4842 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4843 | LSM_HOOK_INIT(getprocattr, smack_getprocattr), |
| 4844 | LSM_HOOK_INIT(setprocattr, smack_setprocattr), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4845 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4846 | LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect), |
| 4847 | LSM_HOOK_INIT(unix_may_send, smack_unix_may_send), |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4848 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4849 | LSM_HOOK_INIT(socket_post_create, smack_socket_post_create), |
Tom Gundersen | 5859cdf | 2018-05-04 16:28:22 +0200 | [diff] [blame] | 4850 | LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair), |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 4851 | #ifdef SMACK_IPV6_PORT_LABELING |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4852 | LSM_HOOK_INIT(socket_bind, smack_socket_bind), |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 4853 | #endif |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4854 | LSM_HOOK_INIT(socket_connect, smack_socket_connect), |
| 4855 | LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg), |
| 4856 | LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb), |
| 4857 | LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream), |
| 4858 | LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram), |
| 4859 | LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security), |
| 4860 | LSM_HOOK_INIT(sk_free_security, smack_sk_free_security), |
| 4861 | LSM_HOOK_INIT(sock_graft, smack_sock_graft), |
| 4862 | LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request), |
| 4863 | LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone), |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4864 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4865 | /* key management security hooks */ |
| 4866 | #ifdef CONFIG_KEYS |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4867 | LSM_HOOK_INIT(key_alloc, smack_key_alloc), |
| 4868 | LSM_HOOK_INIT(key_free, smack_key_free), |
| 4869 | LSM_HOOK_INIT(key_permission, smack_key_permission), |
| 4870 | LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity), |
David Howells | a8478a6 | 2020-01-14 17:07:13 +0000 | [diff] [blame] | 4871 | #ifdef CONFIG_KEY_NOTIFICATIONS |
| 4872 | LSM_HOOK_INIT(watch_key, smack_watch_key), |
| 4873 | #endif |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4874 | #endif /* CONFIG_KEYS */ |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4875 | |
David Howells | a8478a6 | 2020-01-14 17:07:13 +0000 | [diff] [blame] | 4876 | #ifdef CONFIG_WATCH_QUEUE |
| 4877 | LSM_HOOK_INIT(post_notification, smack_post_notification), |
| 4878 | #endif |
| 4879 | |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4880 | /* Audit hooks */ |
| 4881 | #ifdef CONFIG_AUDIT |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4882 | LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init), |
| 4883 | LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known), |
| 4884 | LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match), |
Ahmed S. Darwish | d20bdda | 2008-04-30 08:34:10 +1000 | [diff] [blame] | 4885 | #endif /* CONFIG_AUDIT */ |
| 4886 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4887 | LSM_HOOK_INIT(ismaclabel, smack_ismaclabel), |
| 4888 | LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx), |
| 4889 | LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 4890 | LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx), |
| 4891 | LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx), |
| 4892 | LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx), |
Casey Schaufler | d6d80cb | 2017-09-28 14:54:50 -0700 | [diff] [blame] | 4893 | LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up), |
| 4894 | LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr), |
| 4895 | LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as), |
Casey Schaufler | d9d8c93 | 2021-05-21 11:21:16 -0700 | [diff] [blame] | 4896 | #ifdef CONFIG_IO_URING |
| 4897 | LSM_HOOK_INIT(uring_override_creds, smack_uring_override_creds), |
| 4898 | LSM_HOOK_INIT(uring_sqpoll, smack_uring_sqpoll), |
| 4899 | #endif |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4900 | }; |
| 4901 | |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 4902 | |
Casey Schaufler | 86812bb | 2012-04-17 18:55:46 -0700 | [diff] [blame] | 4903 | static __init void init_smack_known_list(void) |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 4904 | { |
Casey Schaufler | 86812bb | 2012-04-17 18:55:46 -0700 | [diff] [blame] | 4905 | /* |
Casey Schaufler | 86812bb | 2012-04-17 18:55:46 -0700 | [diff] [blame] | 4906 | * Initialize rule list locks |
| 4907 | */ |
| 4908 | mutex_init(&smack_known_huh.smk_rules_lock); |
| 4909 | mutex_init(&smack_known_hat.smk_rules_lock); |
| 4910 | mutex_init(&smack_known_floor.smk_rules_lock); |
| 4911 | mutex_init(&smack_known_star.smk_rules_lock); |
Casey Schaufler | 86812bb | 2012-04-17 18:55:46 -0700 | [diff] [blame] | 4912 | mutex_init(&smack_known_web.smk_rules_lock); |
| 4913 | /* |
| 4914 | * Initialize rule lists |
| 4915 | */ |
| 4916 | INIT_LIST_HEAD(&smack_known_huh.smk_rules); |
| 4917 | INIT_LIST_HEAD(&smack_known_hat.smk_rules); |
| 4918 | INIT_LIST_HEAD(&smack_known_star.smk_rules); |
| 4919 | INIT_LIST_HEAD(&smack_known_floor.smk_rules); |
Casey Schaufler | 86812bb | 2012-04-17 18:55:46 -0700 | [diff] [blame] | 4920 | INIT_LIST_HEAD(&smack_known_web.smk_rules); |
| 4921 | /* |
| 4922 | * Create the known labels list |
| 4923 | */ |
Tomasz Stanislawski | 4d7cf4a | 2013-06-11 14:55:13 +0200 | [diff] [blame] | 4924 | smk_insert_entry(&smack_known_huh); |
| 4925 | smk_insert_entry(&smack_known_hat); |
| 4926 | smk_insert_entry(&smack_known_star); |
| 4927 | smk_insert_entry(&smack_known_floor); |
Tomasz Stanislawski | 4d7cf4a | 2013-06-11 14:55:13 +0200 | [diff] [blame] | 4928 | smk_insert_entry(&smack_known_web); |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 4929 | } |
| 4930 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4931 | /** |
| 4932 | * smack_init - initialize the smack system |
| 4933 | * |
luanshi | a1a07f2 | 2019-07-05 10:35:20 +0800 | [diff] [blame] | 4934 | * Returns 0 on success, -ENOMEM is there's no memory |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4935 | */ |
| 4936 | static __init int smack_init(void) |
| 4937 | { |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 4938 | struct cred *cred = (struct cred *) current->cred; |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 4939 | struct task_smack *tsp; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 4940 | |
Casey Schaufler | 4e328b0 | 2019-04-02 11:37:12 -0700 | [diff] [blame] | 4941 | smack_rule_cache = KMEM_CACHE(smack_rule, 0); |
Casey Schaufler | 4ca7528 | 2020-04-28 15:00:26 -0700 | [diff] [blame] | 4942 | if (!smack_rule_cache) |
Casey Schaufler | 4e328b0 | 2019-04-02 11:37:12 -0700 | [diff] [blame] | 4943 | return -ENOMEM; |
Casey Schaufler | 4e328b0 | 2019-04-02 11:37:12 -0700 | [diff] [blame] | 4944 | |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 4945 | /* |
| 4946 | * Set the security state for the initial task. |
| 4947 | */ |
| 4948 | tsp = smack_cred(cred); |
| 4949 | init_task_smack(tsp, &smack_known_floor, &smack_known_floor); |
| 4950 | |
| 4951 | /* |
| 4952 | * Register with LSM |
| 4953 | */ |
| 4954 | security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack"); |
José Bollo | d21b7b0 | 2015-10-02 15:15:56 +0200 | [diff] [blame] | 4955 | smack_enabled = 1; |
| 4956 | |
Casey Schaufler | 21abb1e | 2015-07-22 14:25:31 -0700 | [diff] [blame] | 4957 | pr_info("Smack: Initializing.\n"); |
| 4958 | #ifdef CONFIG_SECURITY_SMACK_NETFILTER |
| 4959 | pr_info("Smack: Netfilter enabled.\n"); |
| 4960 | #endif |
| 4961 | #ifdef SMACK_IPV6_PORT_LABELING |
| 4962 | pr_info("Smack: IPv6 port labeling enabled.\n"); |
| 4963 | #endif |
| 4964 | #ifdef SMACK_IPV6_SECMARK_LABELING |
| 4965 | pr_info("Smack: IPv6 Netfilter enabled.\n"); |
| 4966 | #endif |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4967 | |
Casey Schaufler | 86812bb | 2012-04-17 18:55:46 -0700 | [diff] [blame] | 4968 | /* initialize the smack_known_list */ |
| 4969 | init_smack_known_list(); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4970 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 4971 | return 0; |
| 4972 | } |
| 4973 | |
| 4974 | /* |
| 4975 | * Smack requires early initialization in order to label |
| 4976 | * all processes and objects when they are created. |
| 4977 | */ |
Kees Cook | 3d6e5f6 | 2018-10-10 17:18:23 -0700 | [diff] [blame] | 4978 | DEFINE_LSM(smack) = { |
Kees Cook | 07aed2f | 2018-10-10 17:18:24 -0700 | [diff] [blame] | 4979 | .name = "smack", |
Kees Cook | 14bd99c | 2018-09-19 19:57:06 -0700 | [diff] [blame] | 4980 | .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 4981 | .blobs = &smack_blob_sizes, |
Kees Cook | 3d6e5f6 | 2018-10-10 17:18:23 -0700 | [diff] [blame] | 4982 | .init = smack_init, |
| 4983 | }; |