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