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