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