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