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