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