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