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