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