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