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