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