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