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