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