1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Simplified MAC Kernel (smack) security module 4 * 5 * This file contains the smack hook function implementations. 6 * 7 * Authors: 8 * Casey Schaufler <casey@schaufler-ca.com> 9 * Jarkko Sakkinen <jarkko.sakkinen@intel.com> 10 * 11 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com> 12 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P. 13 * Paul Moore <paul@paul-moore.com> 14 * Copyright (C) 2010 Nokia Corporation 15 * Copyright (C) 2011 Intel Corporation. 16 */ 17 18 #include <linux/xattr.h> 19 #include <linux/pagemap.h> 20 #include <linux/mount.h> 21 #include <linux/stat.h> 22 #include <linux/kd.h> 23 #include <asm/ioctls.h> 24 #include <linux/ip.h> 25 #include <linux/tcp.h> 26 #include <linux/udp.h> 27 #include <linux/dccp.h> 28 #include <linux/icmpv6.h> 29 #include <linux/slab.h> 30 #include <linux/mutex.h> 31 #include <net/cipso_ipv4.h> 32 #include <net/ip.h> 33 #include <net/ipv6.h> 34 #include <linux/audit.h> 35 #include <linux/magic.h> 36 #include <linux/dcache.h> 37 #include <linux/personality.h> 38 #include <linux/msg.h> 39 #include <linux/shm.h> 40 #include <uapi/linux/shm.h> 41 #include <linux/binfmts.h> 42 #include <linux/parser.h> 43 #include <linux/fs_context.h> 44 #include <linux/fs_parser.h> 45 #include <linux/watch_queue.h> 46 #include <linux/io_uring/cmd.h> 47 #include <uapi/linux/lsm.h> 48 #include "smack.h" 49 50 #define TRANS_TRUE "TRUE" 51 #define TRANS_TRUE_SIZE 4 52 53 #define SMK_CONNECTING 0 54 #define SMK_RECEIVING 1 55 #define SMK_SENDING 2 56 57 /* 58 * Smack uses multiple xattrs. 59 * SMACK64 - for access control, 60 * SMACK64TRANSMUTE - label initialization, 61 * Not saved on files - SMACK64IPIN and SMACK64IPOUT, 62 * Must be set explicitly - SMACK64EXEC and SMACK64MMAP 63 */ 64 #define SMACK_INODE_INIT_XATTRS 2 65 66 #ifdef SMACK_IPV6_PORT_LABELING 67 static DEFINE_MUTEX(smack_ipv6_lock); 68 static LIST_HEAD(smk_ipv6_port_list); 69 #endif 70 struct kmem_cache *smack_rule_cache; 71 int smack_enabled __initdata; 72 73 #define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s} 74 static struct { 75 const char *name; 76 int len; 77 int opt; 78 } smk_mount_opts[] = { 79 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault}, 80 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute) 81 }; 82 #undef A 83 84 static int match_opt_prefix(char *s, int l, char **arg) 85 { 86 int i; 87 88 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) { 89 size_t len = smk_mount_opts[i].len; 90 if (len > l || memcmp(s, smk_mount_opts[i].name, len)) 91 continue; 92 if (len == l || s[len] != '=') 93 continue; 94 *arg = s + len + 1; 95 return smk_mount_opts[i].opt; 96 } 97 return Opt_error; 98 } 99 100 #ifdef CONFIG_SECURITY_SMACK_BRINGUP 101 static char *smk_bu_mess[] = { 102 "Bringup Error", /* Unused */ 103 "Bringup", /* SMACK_BRINGUP_ALLOW */ 104 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */ 105 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */ 106 }; 107 108 static void smk_bu_mode(int mode, char *s) 109 { 110 int i = 0; 111 112 if (mode & MAY_READ) 113 s[i++] = 'r'; 114 if (mode & MAY_WRITE) 115 s[i++] = 'w'; 116 if (mode & MAY_EXEC) 117 s[i++] = 'x'; 118 if (mode & MAY_APPEND) 119 s[i++] = 'a'; 120 if (mode & MAY_TRANSMUTE) 121 s[i++] = 't'; 122 if (mode & MAY_LOCK) 123 s[i++] = 'l'; 124 if (i == 0) 125 s[i++] = '-'; 126 s[i] = '\0'; 127 } 128 #endif 129 130 #ifdef CONFIG_SECURITY_SMACK_BRINGUP 131 static int smk_bu_note(char *note, struct smack_known *sskp, 132 struct smack_known *oskp, int mode, int rc) 133 { 134 char acc[SMK_NUM_ACCESS_TYPE + 1]; 135 136 if (rc <= 0) 137 return rc; 138 if (rc > SMACK_UNCONFINED_OBJECT) 139 rc = 0; 140 141 smk_bu_mode(mode, acc); 142 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc], 143 sskp->smk_known, oskp->smk_known, acc, note); 144 return 0; 145 } 146 #else 147 #define smk_bu_note(note, sskp, oskp, mode, RC) (RC) 148 #endif 149 150 #ifdef CONFIG_SECURITY_SMACK_BRINGUP 151 static int smk_bu_current(char *note, struct smack_known *oskp, 152 int mode, int rc) 153 { 154 struct task_smack *tsp = smack_cred(current_cred()); 155 char acc[SMK_NUM_ACCESS_TYPE + 1]; 156 157 if (rc <= 0) 158 return rc; 159 if (rc > SMACK_UNCONFINED_OBJECT) 160 rc = 0; 161 162 smk_bu_mode(mode, acc); 163 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc], 164 tsp->smk_task->smk_known, oskp->smk_known, 165 acc, current->comm, note); 166 return 0; 167 } 168 #else 169 #define smk_bu_current(note, oskp, mode, RC) (RC) 170 #endif 171 172 #ifdef CONFIG_SECURITY_SMACK_BRINGUP 173 static int smk_bu_task(struct task_struct *otp, int mode, int rc) 174 { 175 struct task_smack *tsp = smack_cred(current_cred()); 176 struct smack_known *smk_task = smk_of_task_struct_obj(otp); 177 char acc[SMK_NUM_ACCESS_TYPE + 1]; 178 179 if (rc <= 0) 180 return rc; 181 if (rc > SMACK_UNCONFINED_OBJECT) 182 rc = 0; 183 184 smk_bu_mode(mode, acc); 185 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc], 186 tsp->smk_task->smk_known, smk_task->smk_known, acc, 187 current->comm, otp->comm); 188 return 0; 189 } 190 #else 191 #define smk_bu_task(otp, mode, RC) (RC) 192 #endif 193 194 #ifdef CONFIG_SECURITY_SMACK_BRINGUP 195 static int smk_bu_inode(struct inode *inode, int mode, int rc) 196 { 197 struct task_smack *tsp = smack_cred(current_cred()); 198 struct inode_smack *isp = smack_inode(inode); 199 char acc[SMK_NUM_ACCESS_TYPE + 1]; 200 201 if (isp->smk_flags & SMK_INODE_IMPURE) 202 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n", 203 inode->i_sb->s_id, inode->i_ino, current->comm); 204 205 if (rc <= 0) 206 return rc; 207 if (rc > SMACK_UNCONFINED_OBJECT) 208 rc = 0; 209 if (rc == SMACK_UNCONFINED_SUBJECT && 210 (mode & (MAY_WRITE | MAY_APPEND))) 211 isp->smk_flags |= SMK_INODE_IMPURE; 212 213 smk_bu_mode(mode, acc); 214 215 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc], 216 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc, 217 inode->i_sb->s_id, inode->i_ino, current->comm); 218 return 0; 219 } 220 #else 221 #define smk_bu_inode(inode, mode, RC) (RC) 222 #endif 223 224 #ifdef CONFIG_SECURITY_SMACK_BRINGUP 225 static int smk_bu_file(struct file *file, int mode, int rc) 226 { 227 struct task_smack *tsp = smack_cred(current_cred()); 228 struct smack_known *sskp = tsp->smk_task; 229 struct inode *inode = file_inode(file); 230 struct inode_smack *isp = smack_inode(inode); 231 char acc[SMK_NUM_ACCESS_TYPE + 1]; 232 233 if (isp->smk_flags & SMK_INODE_IMPURE) 234 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n", 235 inode->i_sb->s_id, inode->i_ino, current->comm); 236 237 if (rc <= 0) 238 return rc; 239 if (rc > SMACK_UNCONFINED_OBJECT) 240 rc = 0; 241 242 smk_bu_mode(mode, acc); 243 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc], 244 sskp->smk_known, smk_of_inode(inode)->smk_known, acc, 245 inode->i_sb->s_id, inode->i_ino, file, 246 current->comm); 247 return 0; 248 } 249 #else 250 #define smk_bu_file(file, mode, RC) (RC) 251 #endif 252 253 #ifdef CONFIG_SECURITY_SMACK_BRINGUP 254 static int smk_bu_credfile(const struct cred *cred, struct file *file, 255 int mode, int rc) 256 { 257 struct task_smack *tsp = smack_cred(cred); 258 struct smack_known *sskp = tsp->smk_task; 259 struct inode *inode = file_inode(file); 260 struct inode_smack *isp = smack_inode(inode); 261 char acc[SMK_NUM_ACCESS_TYPE + 1]; 262 263 if (isp->smk_flags & SMK_INODE_IMPURE) 264 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n", 265 inode->i_sb->s_id, inode->i_ino, current->comm); 266 267 if (rc <= 0) 268 return rc; 269 if (rc > SMACK_UNCONFINED_OBJECT) 270 rc = 0; 271 272 smk_bu_mode(mode, acc); 273 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc], 274 sskp->smk_known, smk_of_inode(inode)->smk_known, acc, 275 inode->i_sb->s_id, inode->i_ino, file, 276 current->comm); 277 return 0; 278 } 279 #else 280 #define smk_bu_credfile(cred, file, mode, RC) (RC) 281 #endif 282 283 /** 284 * smk_fetch - Fetch the smack label from a file. 285 * @name: type of the label (attribute) 286 * @ip: a pointer to the inode 287 * @dp: a pointer to the dentry 288 * 289 * Returns a pointer to the master list entry for the Smack label, 290 * NULL if there was no label to fetch, or an error code. 291 */ 292 static struct smack_known *smk_fetch(const char *name, struct inode *ip, 293 struct dentry *dp) 294 { 295 int rc; 296 char *buffer; 297 struct smack_known *skp = NULL; 298 299 if (!(ip->i_opflags & IOP_XATTR)) 300 return ERR_PTR(-EOPNOTSUPP); 301 302 buffer = kzalloc(SMK_LONGLABEL, GFP_NOFS); 303 if (buffer == NULL) 304 return ERR_PTR(-ENOMEM); 305 306 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL); 307 if (rc < 0) 308 skp = ERR_PTR(rc); 309 else if (rc == 0) 310 skp = NULL; 311 else 312 skp = smk_import_entry(buffer, rc); 313 314 kfree(buffer); 315 316 return skp; 317 } 318 319 /** 320 * init_inode_smack - initialize an inode security blob 321 * @inode: inode to extract the info from 322 * @skp: a pointer to the Smack label entry to use in the blob 323 * 324 */ 325 static void init_inode_smack(struct inode *inode, struct smack_known *skp) 326 { 327 struct inode_smack *isp = smack_inode(inode); 328 329 isp->smk_inode = skp; 330 isp->smk_flags = 0; 331 } 332 333 /** 334 * init_task_smack - initialize a task security blob 335 * @tsp: blob to initialize 336 * @task: a pointer to the Smack label for the running task 337 * @forked: a pointer to the Smack label for the forked task 338 * 339 */ 340 static void init_task_smack(struct task_smack *tsp, struct smack_known *task, 341 struct smack_known *forked) 342 { 343 tsp->smk_task = task; 344 tsp->smk_forked = forked; 345 INIT_LIST_HEAD(&tsp->smk_rules); 346 INIT_LIST_HEAD(&tsp->smk_relabel); 347 mutex_init(&tsp->smk_rules_lock); 348 } 349 350 /** 351 * smk_copy_rules - copy a rule set 352 * @nhead: new rules header pointer 353 * @ohead: old rules header pointer 354 * @gfp: type of the memory for the allocation 355 * 356 * Returns 0 on success, -ENOMEM on error 357 */ 358 static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead, 359 gfp_t gfp) 360 { 361 struct smack_rule *nrp; 362 struct smack_rule *orp; 363 int rc = 0; 364 365 list_for_each_entry_rcu(orp, ohead, list) { 366 nrp = kmem_cache_zalloc(smack_rule_cache, gfp); 367 if (nrp == NULL) { 368 rc = -ENOMEM; 369 break; 370 } 371 *nrp = *orp; 372 list_add_rcu(&nrp->list, nhead); 373 } 374 return rc; 375 } 376 377 /** 378 * smk_copy_relabel - copy smk_relabel labels list 379 * @nhead: new rules header pointer 380 * @ohead: old rules header pointer 381 * @gfp: type of the memory for the allocation 382 * 383 * Returns 0 on success, -ENOMEM on error 384 */ 385 static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead, 386 gfp_t gfp) 387 { 388 struct smack_known_list_elem *nklep; 389 struct smack_known_list_elem *oklep; 390 391 list_for_each_entry(oklep, ohead, list) { 392 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp); 393 if (nklep == NULL) { 394 smk_destroy_label_list(nhead); 395 return -ENOMEM; 396 } 397 nklep->smk_label = oklep->smk_label; 398 list_add(&nklep->list, nhead); 399 } 400 401 return 0; 402 } 403 404 /** 405 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_* 406 * @mode: input mode in form of PTRACE_MODE_* 407 * 408 * Returns a converted MAY_* mode usable by smack rules 409 */ 410 static inline unsigned int smk_ptrace_mode(unsigned int mode) 411 { 412 if (mode & PTRACE_MODE_ATTACH) 413 return MAY_READWRITE; 414 if (mode & PTRACE_MODE_READ) 415 return MAY_READ; 416 417 return 0; 418 } 419 420 /** 421 * smk_ptrace_rule_check - helper for ptrace access 422 * @tracer: tracer process 423 * @tracee_known: label entry of the process that's about to be traced 424 * @mode: ptrace attachment mode (PTRACE_MODE_*) 425 * @func: name of the function that called us, used for audit 426 * 427 * Returns 0 on access granted, -error on error 428 */ 429 static int smk_ptrace_rule_check(struct task_struct *tracer, 430 struct smack_known *tracee_known, 431 unsigned int mode, const char *func) 432 { 433 int rc; 434 struct smk_audit_info ad, *saip = NULL; 435 struct task_smack *tsp; 436 struct smack_known *tracer_known; 437 const struct cred *tracercred; 438 439 if ((mode & PTRACE_MODE_NOAUDIT) == 0) { 440 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK); 441 smk_ad_setfield_u_tsk(&ad, tracer); 442 saip = &ad; 443 } 444 445 rcu_read_lock(); 446 tracercred = __task_cred(tracer); 447 tsp = smack_cred(tracercred); 448 tracer_known = smk_of_task(tsp); 449 450 if ((mode & PTRACE_MODE_ATTACH) && 451 (smack_ptrace_rule == SMACK_PTRACE_EXACT || 452 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) { 453 if (tracer_known->smk_known == tracee_known->smk_known) 454 rc = 0; 455 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN) 456 rc = -EACCES; 457 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred)) 458 rc = 0; 459 else 460 rc = -EACCES; 461 462 if (saip) 463 smack_log(tracer_known->smk_known, 464 tracee_known->smk_known, 465 0, rc, saip); 466 467 rcu_read_unlock(); 468 return rc; 469 } 470 471 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */ 472 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip); 473 474 rcu_read_unlock(); 475 return rc; 476 } 477 478 /* 479 * LSM hooks. 480 * We he, that is fun! 481 */ 482 483 /** 484 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH 485 * @ctp: child task pointer 486 * @mode: ptrace attachment mode (PTRACE_MODE_*) 487 * 488 * Returns 0 if access is OK, an error code otherwise 489 * 490 * Do the capability checks. 491 */ 492 static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode) 493 { 494 struct smack_known *skp; 495 496 skp = smk_of_task_struct_obj(ctp); 497 498 return smk_ptrace_rule_check(current, skp, mode, __func__); 499 } 500 501 /** 502 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME 503 * @ptp: parent task pointer 504 * 505 * Returns 0 if access is OK, an error code otherwise 506 * 507 * Do the capability checks, and require PTRACE_MODE_ATTACH. 508 */ 509 static int smack_ptrace_traceme(struct task_struct *ptp) 510 { 511 struct smack_known *skp; 512 513 skp = smk_of_task(smack_cred(current_cred())); 514 515 return smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__); 516 } 517 518 /** 519 * smack_syslog - Smack approval on syslog 520 * @typefrom_file: unused 521 * 522 * Returns 0 on success, error code otherwise. 523 */ 524 static int smack_syslog(int typefrom_file) 525 { 526 int rc = 0; 527 struct smack_known *skp = smk_of_current(); 528 529 if (smack_privileged(CAP_MAC_OVERRIDE)) 530 return 0; 531 532 if (smack_syslog_label != NULL && smack_syslog_label != skp) 533 rc = -EACCES; 534 535 return rc; 536 } 537 538 /* 539 * Superblock Hooks. 540 */ 541 542 /** 543 * smack_sb_alloc_security - allocate a superblock blob 544 * @sb: the superblock getting the blob 545 * 546 * Returns 0 on success or -ENOMEM on error. 547 */ 548 static int smack_sb_alloc_security(struct super_block *sb) 549 { 550 struct superblock_smack *sbsp = smack_superblock(sb); 551 552 sbsp->smk_root = &smack_known_floor; 553 sbsp->smk_default = &smack_known_floor; 554 sbsp->smk_floor = &smack_known_floor; 555 sbsp->smk_hat = &smack_known_hat; 556 /* 557 * SMK_SB_INITIALIZED will be zero from kzalloc. 558 */ 559 560 return 0; 561 } 562 563 struct smack_mnt_opts { 564 const char *fsdefault; 565 const char *fsfloor; 566 const char *fshat; 567 const char *fsroot; 568 const char *fstransmute; 569 }; 570 571 static void smack_free_mnt_opts(void *mnt_opts) 572 { 573 kfree(mnt_opts); 574 } 575 576 static int smack_add_opt(int token, const char *s, void **mnt_opts) 577 { 578 struct smack_mnt_opts *opts = *mnt_opts; 579 struct smack_known *skp; 580 581 if (!opts) { 582 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL); 583 if (!opts) 584 return -ENOMEM; 585 *mnt_opts = opts; 586 } 587 if (!s) 588 return -ENOMEM; 589 590 skp = smk_import_entry(s, 0); 591 if (IS_ERR(skp)) 592 return PTR_ERR(skp); 593 594 switch (token) { 595 case Opt_fsdefault: 596 if (opts->fsdefault) 597 goto out_opt_err; 598 opts->fsdefault = skp->smk_known; 599 break; 600 case Opt_fsfloor: 601 if (opts->fsfloor) 602 goto out_opt_err; 603 opts->fsfloor = skp->smk_known; 604 break; 605 case Opt_fshat: 606 if (opts->fshat) 607 goto out_opt_err; 608 opts->fshat = skp->smk_known; 609 break; 610 case Opt_fsroot: 611 if (opts->fsroot) 612 goto out_opt_err; 613 opts->fsroot = skp->smk_known; 614 break; 615 case Opt_fstransmute: 616 if (opts->fstransmute) 617 goto out_opt_err; 618 opts->fstransmute = skp->smk_known; 619 break; 620 } 621 return 0; 622 623 out_opt_err: 624 pr_warn("Smack: duplicate mount options\n"); 625 return -EINVAL; 626 } 627 628 /** 629 * smack_fs_context_submount - Initialise security data for a filesystem context 630 * @fc: The filesystem context. 631 * @reference: reference superblock 632 * 633 * Returns 0 on success or -ENOMEM on error. 634 */ 635 static int smack_fs_context_submount(struct fs_context *fc, 636 struct super_block *reference) 637 { 638 struct superblock_smack *sbsp; 639 struct smack_mnt_opts *ctx; 640 struct inode_smack *isp; 641 642 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 643 if (!ctx) 644 return -ENOMEM; 645 fc->security = ctx; 646 647 sbsp = smack_superblock(reference); 648 isp = smack_inode(reference->s_root->d_inode); 649 650 if (sbsp->smk_default) { 651 ctx->fsdefault = kstrdup(sbsp->smk_default->smk_known, GFP_KERNEL); 652 if (!ctx->fsdefault) 653 return -ENOMEM; 654 } 655 656 if (sbsp->smk_floor) { 657 ctx->fsfloor = kstrdup(sbsp->smk_floor->smk_known, GFP_KERNEL); 658 if (!ctx->fsfloor) 659 return -ENOMEM; 660 } 661 662 if (sbsp->smk_hat) { 663 ctx->fshat = kstrdup(sbsp->smk_hat->smk_known, GFP_KERNEL); 664 if (!ctx->fshat) 665 return -ENOMEM; 666 } 667 668 if (isp->smk_flags & SMK_INODE_TRANSMUTE) { 669 if (sbsp->smk_root) { 670 ctx->fstransmute = kstrdup(sbsp->smk_root->smk_known, GFP_KERNEL); 671 if (!ctx->fstransmute) 672 return -ENOMEM; 673 } 674 } 675 return 0; 676 } 677 678 /** 679 * smack_fs_context_dup - Duplicate the security data on fs_context duplication 680 * @fc: The new filesystem context. 681 * @src_fc: The source filesystem context being duplicated. 682 * 683 * Returns 0 on success or -ENOMEM on error. 684 */ 685 static int smack_fs_context_dup(struct fs_context *fc, 686 struct fs_context *src_fc) 687 { 688 struct smack_mnt_opts *dst, *src = src_fc->security; 689 690 if (!src) 691 return 0; 692 693 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL); 694 if (!fc->security) 695 return -ENOMEM; 696 697 dst = fc->security; 698 dst->fsdefault = src->fsdefault; 699 dst->fsfloor = src->fsfloor; 700 dst->fshat = src->fshat; 701 dst->fsroot = src->fsroot; 702 dst->fstransmute = src->fstransmute; 703 704 return 0; 705 } 706 707 static const struct fs_parameter_spec smack_fs_parameters[] = { 708 fsparam_string("smackfsdef", Opt_fsdefault), 709 fsparam_string("smackfsdefault", Opt_fsdefault), 710 fsparam_string("smackfsfloor", Opt_fsfloor), 711 fsparam_string("smackfshat", Opt_fshat), 712 fsparam_string("smackfsroot", Opt_fsroot), 713 fsparam_string("smackfstransmute", Opt_fstransmute), 714 {} 715 }; 716 717 /** 718 * smack_fs_context_parse_param - Parse a single mount parameter 719 * @fc: The new filesystem context being constructed. 720 * @param: The parameter. 721 * 722 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on 723 * error. 724 */ 725 static int smack_fs_context_parse_param(struct fs_context *fc, 726 struct fs_parameter *param) 727 { 728 struct fs_parse_result result; 729 int opt, rc; 730 731 opt = fs_parse(fc, smack_fs_parameters, param, &result); 732 if (opt < 0) 733 return opt; 734 735 rc = smack_add_opt(opt, param->string, &fc->security); 736 if (!rc) 737 param->string = NULL; 738 return rc; 739 } 740 741 static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts) 742 { 743 char *from = options, *to = options; 744 bool first = true; 745 746 while (1) { 747 char *next = strchr(from, ','); 748 int token, len, rc; 749 char *arg = NULL; 750 751 if (next) 752 len = next - from; 753 else 754 len = strlen(from); 755 756 token = match_opt_prefix(from, len, &arg); 757 if (token != Opt_error) { 758 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL); 759 rc = smack_add_opt(token, arg, mnt_opts); 760 kfree(arg); 761 if (unlikely(rc)) { 762 if (*mnt_opts) 763 smack_free_mnt_opts(*mnt_opts); 764 *mnt_opts = NULL; 765 return rc; 766 } 767 } else { 768 if (!first) { // copy with preceding comma 769 from--; 770 len++; 771 } 772 if (to != from) 773 memmove(to, from, len); 774 to += len; 775 first = false; 776 } 777 if (!from[len]) 778 break; 779 from += len + 1; 780 } 781 *to = '\0'; 782 return 0; 783 } 784 785 /** 786 * smack_set_mnt_opts - set Smack specific mount options 787 * @sb: the file system superblock 788 * @mnt_opts: Smack mount options 789 * @kern_flags: mount option from kernel space or user space 790 * @set_kern_flags: where to store converted mount opts 791 * 792 * Returns 0 on success, an error code on failure 793 * 794 * Allow filesystems with binary mount data to explicitly set Smack mount 795 * labels. 796 */ 797 static int smack_set_mnt_opts(struct super_block *sb, 798 void *mnt_opts, 799 unsigned long kern_flags, 800 unsigned long *set_kern_flags) 801 { 802 struct dentry *root = sb->s_root; 803 struct inode *inode = d_backing_inode(root); 804 struct superblock_smack *sp = smack_superblock(sb); 805 struct inode_smack *isp; 806 struct smack_known *skp; 807 struct smack_mnt_opts *opts = mnt_opts; 808 bool transmute = false; 809 810 if (sp->smk_flags & SMK_SB_INITIALIZED) 811 return 0; 812 813 if (!smack_privileged(CAP_MAC_ADMIN)) { 814 /* 815 * Unprivileged mounts don't get to specify Smack values. 816 */ 817 if (opts) 818 return -EPERM; 819 /* 820 * Unprivileged mounts get root and default from the caller. 821 */ 822 skp = smk_of_current(); 823 sp->smk_root = skp; 824 sp->smk_default = skp; 825 /* 826 * For a handful of fs types with no user-controlled 827 * backing store it's okay to trust security labels 828 * in the filesystem. The rest are untrusted. 829 */ 830 if (sb->s_user_ns != &init_user_ns && 831 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC && 832 sb->s_magic != RAMFS_MAGIC) { 833 transmute = true; 834 sp->smk_flags |= SMK_SB_UNTRUSTED; 835 } 836 } 837 838 sp->smk_flags |= SMK_SB_INITIALIZED; 839 840 if (opts) { 841 if (opts->fsdefault) { 842 skp = smk_import_entry(opts->fsdefault, 0); 843 if (IS_ERR(skp)) 844 return PTR_ERR(skp); 845 sp->smk_default = skp; 846 } 847 if (opts->fsfloor) { 848 skp = smk_import_entry(opts->fsfloor, 0); 849 if (IS_ERR(skp)) 850 return PTR_ERR(skp); 851 sp->smk_floor = skp; 852 } 853 if (opts->fshat) { 854 skp = smk_import_entry(opts->fshat, 0); 855 if (IS_ERR(skp)) 856 return PTR_ERR(skp); 857 sp->smk_hat = skp; 858 } 859 if (opts->fsroot) { 860 skp = smk_import_entry(opts->fsroot, 0); 861 if (IS_ERR(skp)) 862 return PTR_ERR(skp); 863 sp->smk_root = skp; 864 } 865 if (opts->fstransmute) { 866 skp = smk_import_entry(opts->fstransmute, 0); 867 if (IS_ERR(skp)) 868 return PTR_ERR(skp); 869 sp->smk_root = skp; 870 transmute = true; 871 } 872 } 873 874 /* 875 * Initialize the root inode. 876 */ 877 init_inode_smack(inode, sp->smk_root); 878 879 if (transmute) { 880 isp = smack_inode(inode); 881 isp->smk_flags |= SMK_INODE_TRANSMUTE; 882 } 883 884 return 0; 885 } 886 887 /** 888 * smack_sb_statfs - Smack check on statfs 889 * @dentry: identifies the file system in question 890 * 891 * Returns 0 if current can read the floor of the filesystem, 892 * and error code otherwise 893 */ 894 static int smack_sb_statfs(struct dentry *dentry) 895 { 896 struct superblock_smack *sbp = smack_superblock(dentry->d_sb); 897 int rc; 898 struct smk_audit_info ad; 899 900 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); 901 smk_ad_setfield_u_fs_path_dentry(&ad, dentry); 902 903 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad); 904 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc); 905 return rc; 906 } 907 908 /* 909 * BPRM hooks 910 */ 911 912 /** 913 * smack_bprm_creds_for_exec - Update bprm->cred if needed for exec 914 * @bprm: the exec information 915 * 916 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise 917 */ 918 static int smack_bprm_creds_for_exec(struct linux_binprm *bprm) 919 { 920 struct inode *inode = file_inode(bprm->file); 921 struct task_smack *bsp = smack_cred(bprm->cred); 922 struct inode_smack *isp; 923 struct superblock_smack *sbsp; 924 int rc; 925 926 isp = smack_inode(inode); 927 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task) 928 return 0; 929 930 sbsp = smack_superblock(inode->i_sb); 931 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) && 932 isp->smk_task != sbsp->smk_root) 933 return 0; 934 935 if (bprm->unsafe & LSM_UNSAFE_PTRACE) { 936 struct task_struct *tracer; 937 rc = 0; 938 939 rcu_read_lock(); 940 tracer = ptrace_parent(current); 941 if (likely(tracer != NULL)) 942 rc = smk_ptrace_rule_check(tracer, 943 isp->smk_task, 944 PTRACE_MODE_ATTACH, 945 __func__); 946 rcu_read_unlock(); 947 948 if (rc != 0) 949 return rc; 950 } 951 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE) 952 return -EPERM; 953 954 bsp->smk_task = isp->smk_task; 955 bprm->per_clear |= PER_CLEAR_ON_SETID; 956 957 /* Decide if this is a secure exec. */ 958 if (bsp->smk_task != bsp->smk_forked) 959 bprm->secureexec = 1; 960 961 return 0; 962 } 963 964 /* 965 * Inode hooks 966 */ 967 968 /** 969 * smack_inode_alloc_security - allocate an inode blob 970 * @inode: the inode in need of a blob 971 * 972 * Returns 0 973 */ 974 static int smack_inode_alloc_security(struct inode *inode) 975 { 976 struct smack_known *skp = smk_of_current(); 977 978 init_inode_smack(inode, skp); 979 return 0; 980 } 981 982 /** 983 * smack_inode_init_security - copy out the smack from an inode 984 * @inode: the newly created inode 985 * @dir: containing directory object 986 * @qstr: unused 987 * @xattrs: where to put the attributes 988 * @xattr_count: current number of LSM-provided xattrs (updated) 989 * 990 * Returns 0 if it all works out, -ENOMEM if there's no memory 991 */ 992 static int smack_inode_init_security(struct inode *inode, struct inode *dir, 993 const struct qstr *qstr, 994 struct xattr *xattrs, int *xattr_count) 995 { 996 struct task_smack *tsp = smack_cred(current_cred()); 997 struct inode_smack *issp = smack_inode(inode); 998 struct smack_known *skp = smk_of_task(tsp); 999 struct smack_known *isp = smk_of_inode(inode); 1000 struct smack_known *dsp = smk_of_inode(dir); 1001 struct xattr *xattr = lsm_get_xattr_slot(xattrs, xattr_count); 1002 int may; 1003 1004 /* 1005 * If equal, transmuting already occurred in 1006 * smack_dentry_create_files_as(). No need to check again. 1007 */ 1008 if (tsp->smk_task != tsp->smk_transmuted) { 1009 rcu_read_lock(); 1010 may = smk_access_entry(skp->smk_known, dsp->smk_known, 1011 &skp->smk_rules); 1012 rcu_read_unlock(); 1013 } 1014 1015 /* 1016 * In addition to having smk_task equal to smk_transmuted, 1017 * if the access rule allows transmutation and the directory 1018 * requests transmutation then by all means transmute. 1019 * Mark the inode as changed. 1020 */ 1021 if ((tsp->smk_task == tsp->smk_transmuted) || 1022 (may > 0 && ((may & MAY_TRANSMUTE) != 0) && 1023 smk_inode_transmutable(dir))) { 1024 struct xattr *xattr_transmute; 1025 1026 /* 1027 * The caller of smack_dentry_create_files_as() 1028 * should have overridden the current cred, so the 1029 * inode label was already set correctly in 1030 * smack_inode_alloc_security(). 1031 */ 1032 if (tsp->smk_task != tsp->smk_transmuted) 1033 isp = issp->smk_inode = dsp; 1034 1035 issp->smk_flags |= SMK_INODE_TRANSMUTE; 1036 xattr_transmute = lsm_get_xattr_slot(xattrs, 1037 xattr_count); 1038 if (xattr_transmute) { 1039 xattr_transmute->value = kmemdup(TRANS_TRUE, 1040 TRANS_TRUE_SIZE, 1041 GFP_NOFS); 1042 if (!xattr_transmute->value) 1043 return -ENOMEM; 1044 1045 xattr_transmute->value_len = TRANS_TRUE_SIZE; 1046 xattr_transmute->name = XATTR_SMACK_TRANSMUTE; 1047 } 1048 } 1049 1050 issp->smk_flags |= SMK_INODE_INSTANT; 1051 1052 if (xattr) { 1053 xattr->value = kstrdup(isp->smk_known, GFP_NOFS); 1054 if (!xattr->value) 1055 return -ENOMEM; 1056 1057 xattr->value_len = strlen(isp->smk_known); 1058 xattr->name = XATTR_SMACK_SUFFIX; 1059 } 1060 1061 return 0; 1062 } 1063 1064 /** 1065 * smack_inode_link - Smack check on link 1066 * @old_dentry: the existing object 1067 * @dir: unused 1068 * @new_dentry: the new object 1069 * 1070 * Returns 0 if access is permitted, an error code otherwise 1071 */ 1072 static int smack_inode_link(struct dentry *old_dentry, struct inode *dir, 1073 struct dentry *new_dentry) 1074 { 1075 struct smack_known *isp; 1076 struct smk_audit_info ad; 1077 int rc; 1078 1079 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); 1080 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry); 1081 1082 isp = smk_of_inode(d_backing_inode(old_dentry)); 1083 rc = smk_curacc(isp, MAY_WRITE, &ad); 1084 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc); 1085 1086 if (rc == 0 && d_is_positive(new_dentry)) { 1087 isp = smk_of_inode(d_backing_inode(new_dentry)); 1088 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry); 1089 rc = smk_curacc(isp, MAY_WRITE, &ad); 1090 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc); 1091 } 1092 1093 return rc; 1094 } 1095 1096 /** 1097 * smack_inode_unlink - Smack check on inode deletion 1098 * @dir: containing directory object 1099 * @dentry: file to unlink 1100 * 1101 * Returns 0 if current can write the containing directory 1102 * and the object, error code otherwise 1103 */ 1104 static int smack_inode_unlink(struct inode *dir, struct dentry *dentry) 1105 { 1106 struct inode *ip = d_backing_inode(dentry); 1107 struct smk_audit_info ad; 1108 int rc; 1109 1110 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); 1111 smk_ad_setfield_u_fs_path_dentry(&ad, dentry); 1112 1113 /* 1114 * You need write access to the thing you're unlinking 1115 */ 1116 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad); 1117 rc = smk_bu_inode(ip, MAY_WRITE, rc); 1118 if (rc == 0) { 1119 /* 1120 * You also need write access to the containing directory 1121 */ 1122 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE); 1123 smk_ad_setfield_u_fs_inode(&ad, dir); 1124 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad); 1125 rc = smk_bu_inode(dir, MAY_WRITE, rc); 1126 } 1127 return rc; 1128 } 1129 1130 /** 1131 * smack_inode_rmdir - Smack check on directory deletion 1132 * @dir: containing directory object 1133 * @dentry: directory to unlink 1134 * 1135 * Returns 0 if current can write the containing directory 1136 * and the directory, error code otherwise 1137 */ 1138 static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry) 1139 { 1140 struct smk_audit_info ad; 1141 int rc; 1142 1143 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); 1144 smk_ad_setfield_u_fs_path_dentry(&ad, dentry); 1145 1146 /* 1147 * You need write access to the thing you're removing 1148 */ 1149 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad); 1150 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc); 1151 if (rc == 0) { 1152 /* 1153 * You also need write access to the containing directory 1154 */ 1155 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE); 1156 smk_ad_setfield_u_fs_inode(&ad, dir); 1157 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad); 1158 rc = smk_bu_inode(dir, MAY_WRITE, rc); 1159 } 1160 1161 return rc; 1162 } 1163 1164 /** 1165 * smack_inode_rename - Smack check on rename 1166 * @old_inode: unused 1167 * @old_dentry: the old object 1168 * @new_inode: unused 1169 * @new_dentry: the new object 1170 * 1171 * Read and write access is required on both the old and 1172 * new directories. 1173 * 1174 * Returns 0 if access is permitted, an error code otherwise 1175 */ 1176 static int smack_inode_rename(struct inode *old_inode, 1177 struct dentry *old_dentry, 1178 struct inode *new_inode, 1179 struct dentry *new_dentry) 1180 { 1181 int rc; 1182 struct smack_known *isp; 1183 struct smk_audit_info ad; 1184 1185 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); 1186 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry); 1187 1188 isp = smk_of_inode(d_backing_inode(old_dentry)); 1189 rc = smk_curacc(isp, MAY_READWRITE, &ad); 1190 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc); 1191 1192 if (rc == 0 && d_is_positive(new_dentry)) { 1193 isp = smk_of_inode(d_backing_inode(new_dentry)); 1194 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry); 1195 rc = smk_curacc(isp, MAY_READWRITE, &ad); 1196 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc); 1197 } 1198 return rc; 1199 } 1200 1201 /** 1202 * smack_inode_permission - Smack version of permission() 1203 * @inode: the inode in question 1204 * @mask: the access requested 1205 * 1206 * This is the important Smack hook. 1207 * 1208 * Returns 0 if access is permitted, an error code otherwise 1209 */ 1210 static int smack_inode_permission(struct inode *inode, int mask) 1211 { 1212 struct superblock_smack *sbsp = smack_superblock(inode->i_sb); 1213 struct smk_audit_info ad; 1214 int no_block = mask & MAY_NOT_BLOCK; 1215 int rc; 1216 1217 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); 1218 /* 1219 * No permission to check. Existence test. Yup, it's there. 1220 */ 1221 if (mask == 0) 1222 return 0; 1223 1224 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) { 1225 if (smk_of_inode(inode) != sbsp->smk_root) 1226 return -EACCES; 1227 } 1228 1229 /* May be droppable after audit */ 1230 if (no_block) 1231 return -ECHILD; 1232 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE); 1233 smk_ad_setfield_u_fs_inode(&ad, inode); 1234 rc = smk_curacc(smk_of_inode(inode), mask, &ad); 1235 rc = smk_bu_inode(inode, mask, rc); 1236 return rc; 1237 } 1238 1239 /** 1240 * smack_inode_setattr - Smack check for setting attributes 1241 * @dentry: the object 1242 * @iattr: for the force flag 1243 * 1244 * Returns 0 if access is permitted, an error code otherwise 1245 */ 1246 static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr) 1247 { 1248 struct smk_audit_info ad; 1249 int rc; 1250 1251 /* 1252 * Need to allow for clearing the setuid bit. 1253 */ 1254 if (iattr->ia_valid & ATTR_FORCE) 1255 return 0; 1256 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); 1257 smk_ad_setfield_u_fs_path_dentry(&ad, dentry); 1258 1259 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad); 1260 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc); 1261 return rc; 1262 } 1263 1264 /** 1265 * smack_inode_getattr - Smack check for getting attributes 1266 * @path: path to extract the info from 1267 * 1268 * Returns 0 if access is permitted, an error code otherwise 1269 */ 1270 static int smack_inode_getattr(const struct path *path) 1271 { 1272 struct smk_audit_info ad; 1273 struct inode *inode = d_backing_inode(path->dentry); 1274 int rc; 1275 1276 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); 1277 smk_ad_setfield_u_fs_path(&ad, *path); 1278 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad); 1279 rc = smk_bu_inode(inode, MAY_READ, rc); 1280 return rc; 1281 } 1282 1283 /** 1284 * smack_inode_setxattr - Smack check for setting xattrs 1285 * @idmap: idmap of the mount 1286 * @dentry: the object 1287 * @name: name of the attribute 1288 * @value: value of the attribute 1289 * @size: size of the value 1290 * @flags: unused 1291 * 1292 * This protects the Smack attribute explicitly. 1293 * 1294 * Returns 0 if access is permitted, an error code otherwise 1295 */ 1296 static int smack_inode_setxattr(struct mnt_idmap *idmap, 1297 struct dentry *dentry, const char *name, 1298 const void *value, size_t size, int flags) 1299 { 1300 struct smk_audit_info ad; 1301 struct smack_known *skp; 1302 int check_priv = 0; 1303 int check_import = 0; 1304 int check_star = 0; 1305 int rc = 0; 1306 1307 /* 1308 * Check label validity here so import won't fail in post_setxattr 1309 */ 1310 if (strcmp(name, XATTR_NAME_SMACK) == 0 || 1311 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 || 1312 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) { 1313 check_priv = 1; 1314 check_import = 1; 1315 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 || 1316 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) { 1317 check_priv = 1; 1318 check_import = 1; 1319 check_star = 1; 1320 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) { 1321 check_priv = 1; 1322 if (!S_ISDIR(d_backing_inode(dentry)->i_mode) || 1323 size != TRANS_TRUE_SIZE || 1324 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0) 1325 rc = -EINVAL; 1326 } else 1327 rc = cap_inode_setxattr(dentry, name, value, size, flags); 1328 1329 if (check_priv && !smack_privileged(CAP_MAC_ADMIN)) 1330 rc = -EPERM; 1331 1332 if (rc == 0 && check_import) { 1333 skp = size ? smk_import_entry(value, size) : NULL; 1334 if (IS_ERR(skp)) 1335 rc = PTR_ERR(skp); 1336 else if (skp == NULL || (check_star && 1337 (skp == &smack_known_star || skp == &smack_known_web))) 1338 rc = -EINVAL; 1339 } 1340 1341 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); 1342 smk_ad_setfield_u_fs_path_dentry(&ad, dentry); 1343 1344 if (rc == 0) { 1345 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad); 1346 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc); 1347 } 1348 1349 return rc; 1350 } 1351 1352 /** 1353 * smack_inode_post_setxattr - Apply the Smack update approved above 1354 * @dentry: object 1355 * @name: attribute name 1356 * @value: attribute value 1357 * @size: attribute size 1358 * @flags: unused 1359 * 1360 * Set the pointer in the inode blob to the entry found 1361 * in the master label list. 1362 */ 1363 static void smack_inode_post_setxattr(struct dentry *dentry, const char *name, 1364 const void *value, size_t size, int flags) 1365 { 1366 struct smack_known *skp; 1367 struct inode_smack *isp = smack_inode(d_backing_inode(dentry)); 1368 1369 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) { 1370 isp->smk_flags |= SMK_INODE_TRANSMUTE; 1371 return; 1372 } 1373 1374 if (strcmp(name, XATTR_NAME_SMACK) == 0) { 1375 skp = smk_import_entry(value, size); 1376 if (!IS_ERR(skp)) 1377 isp->smk_inode = skp; 1378 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) { 1379 skp = smk_import_entry(value, size); 1380 if (!IS_ERR(skp)) 1381 isp->smk_task = skp; 1382 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) { 1383 skp = smk_import_entry(value, size); 1384 if (!IS_ERR(skp)) 1385 isp->smk_mmap = skp; 1386 } 1387 1388 return; 1389 } 1390 1391 /** 1392 * smack_inode_getxattr - Smack check on getxattr 1393 * @dentry: the object 1394 * @name: unused 1395 * 1396 * Returns 0 if access is permitted, an error code otherwise 1397 */ 1398 static int smack_inode_getxattr(struct dentry *dentry, const char *name) 1399 { 1400 struct smk_audit_info ad; 1401 int rc; 1402 1403 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); 1404 smk_ad_setfield_u_fs_path_dentry(&ad, dentry); 1405 1406 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad); 1407 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc); 1408 return rc; 1409 } 1410 1411 /** 1412 * smack_inode_removexattr - Smack check on removexattr 1413 * @idmap: idmap of the mount 1414 * @dentry: the object 1415 * @name: name of the attribute 1416 * 1417 * Removing the Smack attribute requires CAP_MAC_ADMIN 1418 * 1419 * Returns 0 if access is permitted, an error code otherwise 1420 */ 1421 static int smack_inode_removexattr(struct mnt_idmap *idmap, 1422 struct dentry *dentry, const char *name) 1423 { 1424 struct inode_smack *isp; 1425 struct smk_audit_info ad; 1426 int rc = 0; 1427 1428 if (strcmp(name, XATTR_NAME_SMACK) == 0 || 1429 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 || 1430 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 || 1431 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 || 1432 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 || 1433 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) { 1434 if (!smack_privileged(CAP_MAC_ADMIN)) 1435 rc = -EPERM; 1436 } else 1437 rc = cap_inode_removexattr(idmap, dentry, name); 1438 1439 if (rc != 0) 1440 return rc; 1441 1442 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); 1443 smk_ad_setfield_u_fs_path_dentry(&ad, dentry); 1444 1445 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad); 1446 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc); 1447 if (rc != 0) 1448 return rc; 1449 1450 isp = smack_inode(d_backing_inode(dentry)); 1451 /* 1452 * Don't do anything special for these. 1453 * XATTR_NAME_SMACKIPIN 1454 * XATTR_NAME_SMACKIPOUT 1455 */ 1456 if (strcmp(name, XATTR_NAME_SMACK) == 0) { 1457 struct super_block *sbp = dentry->d_sb; 1458 struct superblock_smack *sbsp = smack_superblock(sbp); 1459 1460 isp->smk_inode = sbsp->smk_default; 1461 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) 1462 isp->smk_task = NULL; 1463 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) 1464 isp->smk_mmap = NULL; 1465 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) 1466 isp->smk_flags &= ~SMK_INODE_TRANSMUTE; 1467 1468 return 0; 1469 } 1470 1471 /** 1472 * smack_inode_set_acl - Smack check for setting posix acls 1473 * @idmap: idmap of the mnt this request came from 1474 * @dentry: the object 1475 * @acl_name: name of the posix acl 1476 * @kacl: the posix acls 1477 * 1478 * Returns 0 if access is permitted, an error code otherwise 1479 */ 1480 static int smack_inode_set_acl(struct mnt_idmap *idmap, 1481 struct dentry *dentry, const char *acl_name, 1482 struct posix_acl *kacl) 1483 { 1484 struct smk_audit_info ad; 1485 int rc; 1486 1487 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); 1488 smk_ad_setfield_u_fs_path_dentry(&ad, dentry); 1489 1490 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad); 1491 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc); 1492 return rc; 1493 } 1494 1495 /** 1496 * smack_inode_get_acl - Smack check for getting posix acls 1497 * @idmap: idmap of the mnt this request came from 1498 * @dentry: the object 1499 * @acl_name: name of the posix acl 1500 * 1501 * Returns 0 if access is permitted, an error code otherwise 1502 */ 1503 static int smack_inode_get_acl(struct mnt_idmap *idmap, 1504 struct dentry *dentry, const char *acl_name) 1505 { 1506 struct smk_audit_info ad; 1507 int rc; 1508 1509 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); 1510 smk_ad_setfield_u_fs_path_dentry(&ad, dentry); 1511 1512 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad); 1513 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc); 1514 return rc; 1515 } 1516 1517 /** 1518 * smack_inode_remove_acl - Smack check for getting posix acls 1519 * @idmap: idmap of the mnt this request came from 1520 * @dentry: the object 1521 * @acl_name: name of the posix acl 1522 * 1523 * Returns 0 if access is permitted, an error code otherwise 1524 */ 1525 static int smack_inode_remove_acl(struct mnt_idmap *idmap, 1526 struct dentry *dentry, const char *acl_name) 1527 { 1528 struct smk_audit_info ad; 1529 int rc; 1530 1531 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); 1532 smk_ad_setfield_u_fs_path_dentry(&ad, dentry); 1533 1534 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad); 1535 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc); 1536 return rc; 1537 } 1538 1539 /** 1540 * smack_inode_getsecurity - get smack xattrs 1541 * @idmap: idmap of the mount 1542 * @inode: the object 1543 * @name: attribute name 1544 * @buffer: where to put the result 1545 * @alloc: duplicate memory 1546 * 1547 * Returns the size of the attribute or an error code 1548 */ 1549 static int smack_inode_getsecurity(struct mnt_idmap *idmap, 1550 struct inode *inode, const char *name, 1551 void **buffer, bool alloc) 1552 { 1553 struct socket_smack *ssp; 1554 struct socket *sock; 1555 struct super_block *sbp; 1556 struct inode *ip = inode; 1557 struct smack_known *isp; 1558 struct inode_smack *ispp; 1559 size_t label_len; 1560 char *label = NULL; 1561 1562 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) { 1563 isp = smk_of_inode(inode); 1564 } else if (strcmp(name, XATTR_SMACK_TRANSMUTE) == 0) { 1565 ispp = smack_inode(inode); 1566 if (ispp->smk_flags & SMK_INODE_TRANSMUTE) 1567 label = TRANS_TRUE; 1568 else 1569 label = ""; 1570 } else { 1571 /* 1572 * The rest of the Smack xattrs are only on sockets. 1573 */ 1574 sbp = ip->i_sb; 1575 if (sbp->s_magic != SOCKFS_MAGIC) 1576 return -EOPNOTSUPP; 1577 1578 sock = SOCKET_I(ip); 1579 if (sock == NULL || sock->sk == NULL) 1580 return -EOPNOTSUPP; 1581 1582 ssp = sock->sk->sk_security; 1583 1584 if (strcmp(name, XATTR_SMACK_IPIN) == 0) 1585 isp = ssp->smk_in; 1586 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) 1587 isp = ssp->smk_out; 1588 else 1589 return -EOPNOTSUPP; 1590 } 1591 1592 if (!label) 1593 label = isp->smk_known; 1594 1595 label_len = strlen(label); 1596 1597 if (alloc) { 1598 *buffer = kstrdup(label, GFP_KERNEL); 1599 if (*buffer == NULL) 1600 return -ENOMEM; 1601 } 1602 1603 return label_len; 1604 } 1605 1606 1607 /** 1608 * smack_inode_listsecurity - list the Smack attributes 1609 * @inode: the object 1610 * @buffer: where they go 1611 * @buffer_size: size of buffer 1612 */ 1613 static int smack_inode_listsecurity(struct inode *inode, char *buffer, 1614 size_t buffer_size) 1615 { 1616 int len = sizeof(XATTR_NAME_SMACK); 1617 1618 if (buffer != NULL && len <= buffer_size) 1619 memcpy(buffer, XATTR_NAME_SMACK, len); 1620 1621 return len; 1622 } 1623 1624 /** 1625 * smack_inode_getsecid - Extract inode's security id 1626 * @inode: inode to extract the info from 1627 * @secid: where result will be saved 1628 */ 1629 static void smack_inode_getsecid(struct inode *inode, u32 *secid) 1630 { 1631 struct smack_known *skp = smk_of_inode(inode); 1632 1633 *secid = skp->smk_secid; 1634 } 1635 1636 /* 1637 * File Hooks 1638 */ 1639 1640 /* 1641 * There is no smack_file_permission hook 1642 * 1643 * Should access checks be done on each read or write? 1644 * UNICOS and SELinux say yes. 1645 * Trusted Solaris, Trusted Irix, and just about everyone else says no. 1646 * 1647 * I'll say no for now. Smack does not do the frequent 1648 * label changing that SELinux does. 1649 */ 1650 1651 /** 1652 * smack_file_alloc_security - assign a file security blob 1653 * @file: the object 1654 * 1655 * The security blob for a file is a pointer to the master 1656 * label list, so no allocation is done. 1657 * 1658 * f_security is the owner security information. It 1659 * isn't used on file access checks, it's for send_sigio. 1660 * 1661 * Returns 0 1662 */ 1663 static int smack_file_alloc_security(struct file *file) 1664 { 1665 struct smack_known **blob = smack_file(file); 1666 1667 *blob = smk_of_current(); 1668 return 0; 1669 } 1670 1671 /** 1672 * smack_file_ioctl - Smack check on ioctls 1673 * @file: the object 1674 * @cmd: what to do 1675 * @arg: unused 1676 * 1677 * Relies heavily on the correct use of the ioctl command conventions. 1678 * 1679 * Returns 0 if allowed, error code otherwise 1680 */ 1681 static int smack_file_ioctl(struct file *file, unsigned int cmd, 1682 unsigned long arg) 1683 { 1684 int rc = 0; 1685 struct smk_audit_info ad; 1686 struct inode *inode = file_inode(file); 1687 1688 if (unlikely(IS_PRIVATE(inode))) 1689 return 0; 1690 1691 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); 1692 smk_ad_setfield_u_fs_path(&ad, file->f_path); 1693 1694 if (_IOC_DIR(cmd) & _IOC_WRITE) { 1695 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad); 1696 rc = smk_bu_file(file, MAY_WRITE, rc); 1697 } 1698 1699 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) { 1700 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad); 1701 rc = smk_bu_file(file, MAY_READ, rc); 1702 } 1703 1704 return rc; 1705 } 1706 1707 /** 1708 * smack_file_lock - Smack check on file locking 1709 * @file: the object 1710 * @cmd: unused 1711 * 1712 * Returns 0 if current has lock access, error code otherwise 1713 */ 1714 static int smack_file_lock(struct file *file, unsigned int cmd) 1715 { 1716 struct smk_audit_info ad; 1717 int rc; 1718 struct inode *inode = file_inode(file); 1719 1720 if (unlikely(IS_PRIVATE(inode))) 1721 return 0; 1722 1723 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); 1724 smk_ad_setfield_u_fs_path(&ad, file->f_path); 1725 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad); 1726 rc = smk_bu_file(file, MAY_LOCK, rc); 1727 return rc; 1728 } 1729 1730 /** 1731 * smack_file_fcntl - Smack check on fcntl 1732 * @file: the object 1733 * @cmd: what action to check 1734 * @arg: unused 1735 * 1736 * Generally these operations are harmless. 1737 * File locking operations present an obvious mechanism 1738 * for passing information, so they require write access. 1739 * 1740 * Returns 0 if current has access, error code otherwise 1741 */ 1742 static int smack_file_fcntl(struct file *file, unsigned int cmd, 1743 unsigned long arg) 1744 { 1745 struct smk_audit_info ad; 1746 int rc = 0; 1747 struct inode *inode = file_inode(file); 1748 1749 if (unlikely(IS_PRIVATE(inode))) 1750 return 0; 1751 1752 switch (cmd) { 1753 case F_GETLK: 1754 break; 1755 case F_SETLK: 1756 case F_SETLKW: 1757 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); 1758 smk_ad_setfield_u_fs_path(&ad, file->f_path); 1759 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad); 1760 rc = smk_bu_file(file, MAY_LOCK, rc); 1761 break; 1762 case F_SETOWN: 1763 case F_SETSIG: 1764 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); 1765 smk_ad_setfield_u_fs_path(&ad, file->f_path); 1766 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad); 1767 rc = smk_bu_file(file, MAY_WRITE, rc); 1768 break; 1769 default: 1770 break; 1771 } 1772 1773 return rc; 1774 } 1775 1776 /** 1777 * smack_mmap_file - Check permissions for a mmap operation. 1778 * @file: contains the file structure for file to map (may be NULL). 1779 * @reqprot: contains the protection requested by the application. 1780 * @prot: contains the protection that will be applied by the kernel. 1781 * @flags: contains the operational flags. 1782 * 1783 * The @file may be NULL, e.g. if mapping anonymous memory. 1784 * 1785 * Return 0 if permission is granted. 1786 */ 1787 static int smack_mmap_file(struct file *file, 1788 unsigned long reqprot, unsigned long prot, 1789 unsigned long flags) 1790 { 1791 struct smack_known *skp; 1792 struct smack_known *mkp; 1793 struct smack_rule *srp; 1794 struct task_smack *tsp; 1795 struct smack_known *okp; 1796 struct inode_smack *isp; 1797 struct superblock_smack *sbsp; 1798 int may; 1799 int mmay; 1800 int tmay; 1801 int rc; 1802 1803 if (file == NULL) 1804 return 0; 1805 1806 if (unlikely(IS_PRIVATE(file_inode(file)))) 1807 return 0; 1808 1809 isp = smack_inode(file_inode(file)); 1810 if (isp->smk_mmap == NULL) 1811 return 0; 1812 sbsp = smack_superblock(file_inode(file)->i_sb); 1813 if (sbsp->smk_flags & SMK_SB_UNTRUSTED && 1814 isp->smk_mmap != sbsp->smk_root) 1815 return -EACCES; 1816 mkp = isp->smk_mmap; 1817 1818 tsp = smack_cred(current_cred()); 1819 skp = smk_of_current(); 1820 rc = 0; 1821 1822 rcu_read_lock(); 1823 /* 1824 * For each Smack rule associated with the subject 1825 * label verify that the SMACK64MMAP also has access 1826 * to that rule's object label. 1827 */ 1828 list_for_each_entry_rcu(srp, &skp->smk_rules, list) { 1829 okp = srp->smk_object; 1830 /* 1831 * Matching labels always allows access. 1832 */ 1833 if (mkp->smk_known == okp->smk_known) 1834 continue; 1835 /* 1836 * If there is a matching local rule take 1837 * that into account as well. 1838 */ 1839 may = smk_access_entry(srp->smk_subject->smk_known, 1840 okp->smk_known, 1841 &tsp->smk_rules); 1842 if (may == -ENOENT) 1843 may = srp->smk_access; 1844 else 1845 may &= srp->smk_access; 1846 /* 1847 * If may is zero the SMACK64MMAP subject can't 1848 * possibly have less access. 1849 */ 1850 if (may == 0) 1851 continue; 1852 1853 /* 1854 * Fetch the global list entry. 1855 * If there isn't one a SMACK64MMAP subject 1856 * can't have as much access as current. 1857 */ 1858 mmay = smk_access_entry(mkp->smk_known, okp->smk_known, 1859 &mkp->smk_rules); 1860 if (mmay == -ENOENT) { 1861 rc = -EACCES; 1862 break; 1863 } 1864 /* 1865 * If there is a local entry it modifies the 1866 * potential access, too. 1867 */ 1868 tmay = smk_access_entry(mkp->smk_known, okp->smk_known, 1869 &tsp->smk_rules); 1870 if (tmay != -ENOENT) 1871 mmay &= tmay; 1872 1873 /* 1874 * If there is any access available to current that is 1875 * not available to a SMACK64MMAP subject 1876 * deny access. 1877 */ 1878 if ((may | mmay) != mmay) { 1879 rc = -EACCES; 1880 break; 1881 } 1882 } 1883 1884 rcu_read_unlock(); 1885 1886 return rc; 1887 } 1888 1889 /** 1890 * smack_file_set_fowner - set the file security blob value 1891 * @file: object in question 1892 * 1893 */ 1894 static void smack_file_set_fowner(struct file *file) 1895 { 1896 struct smack_known **blob = smack_file(file); 1897 1898 *blob = smk_of_current(); 1899 } 1900 1901 /** 1902 * smack_file_send_sigiotask - Smack on sigio 1903 * @tsk: The target task 1904 * @fown: the object the signal come from 1905 * @signum: unused 1906 * 1907 * Allow a privileged task to get signals even if it shouldn't 1908 * 1909 * Returns 0 if a subject with the object's smack could 1910 * write to the task, an error code otherwise. 1911 */ 1912 static int smack_file_send_sigiotask(struct task_struct *tsk, 1913 struct fown_struct *fown, int signum) 1914 { 1915 struct smack_known **blob; 1916 struct smack_known *skp; 1917 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred)); 1918 const struct cred *tcred; 1919 struct file *file; 1920 int rc; 1921 struct smk_audit_info ad; 1922 1923 /* 1924 * struct fown_struct is never outside the context of a struct file 1925 */ 1926 file = container_of(fown, struct file, f_owner); 1927 1928 /* we don't log here as rc can be overriden */ 1929 blob = smack_file(file); 1930 skp = *blob; 1931 rc = smk_access(skp, tkp, MAY_DELIVER, NULL); 1932 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc); 1933 1934 rcu_read_lock(); 1935 tcred = __task_cred(tsk); 1936 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred)) 1937 rc = 0; 1938 rcu_read_unlock(); 1939 1940 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK); 1941 smk_ad_setfield_u_tsk(&ad, tsk); 1942 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad); 1943 return rc; 1944 } 1945 1946 /** 1947 * smack_file_receive - Smack file receive check 1948 * @file: the object 1949 * 1950 * Returns 0 if current has access, error code otherwise 1951 */ 1952 static int smack_file_receive(struct file *file) 1953 { 1954 int rc; 1955 int may = 0; 1956 struct smk_audit_info ad; 1957 struct inode *inode = file_inode(file); 1958 struct socket *sock; 1959 struct task_smack *tsp; 1960 struct socket_smack *ssp; 1961 1962 if (unlikely(IS_PRIVATE(inode))) 1963 return 0; 1964 1965 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); 1966 smk_ad_setfield_u_fs_path(&ad, file->f_path); 1967 1968 if (inode->i_sb->s_magic == SOCKFS_MAGIC) { 1969 sock = SOCKET_I(inode); 1970 ssp = sock->sk->sk_security; 1971 tsp = smack_cred(current_cred()); 1972 /* 1973 * If the receiving process can't write to the 1974 * passed socket or if the passed socket can't 1975 * write to the receiving process don't accept 1976 * the passed socket. 1977 */ 1978 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad); 1979 rc = smk_bu_file(file, may, rc); 1980 if (rc < 0) 1981 return rc; 1982 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad); 1983 rc = smk_bu_file(file, may, rc); 1984 return rc; 1985 } 1986 /* 1987 * This code relies on bitmasks. 1988 */ 1989 if (file->f_mode & FMODE_READ) 1990 may = MAY_READ; 1991 if (file->f_mode & FMODE_WRITE) 1992 may |= MAY_WRITE; 1993 1994 rc = smk_curacc(smk_of_inode(inode), may, &ad); 1995 rc = smk_bu_file(file, may, rc); 1996 return rc; 1997 } 1998 1999 /** 2000 * smack_file_open - Smack dentry open processing 2001 * @file: the object 2002 * 2003 * Set the security blob in the file structure. 2004 * Allow the open only if the task has read access. There are 2005 * many read operations (e.g. fstat) that you can do with an 2006 * fd even if you have the file open write-only. 2007 * 2008 * Returns 0 if current has access, error code otherwise 2009 */ 2010 static int smack_file_open(struct file *file) 2011 { 2012 struct task_smack *tsp = smack_cred(file->f_cred); 2013 struct inode *inode = file_inode(file); 2014 struct smk_audit_info ad; 2015 int rc; 2016 2017 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); 2018 smk_ad_setfield_u_fs_path(&ad, file->f_path); 2019 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad); 2020 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc); 2021 2022 return rc; 2023 } 2024 2025 /* 2026 * Task hooks 2027 */ 2028 2029 /** 2030 * smack_cred_alloc_blank - "allocate" blank task-level security credentials 2031 * @cred: the new credentials 2032 * @gfp: the atomicity of any memory allocations 2033 * 2034 * Prepare a blank set of credentials for modification. This must allocate all 2035 * the memory the LSM module might require such that cred_transfer() can 2036 * complete without error. 2037 */ 2038 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp) 2039 { 2040 init_task_smack(smack_cred(cred), NULL, NULL); 2041 return 0; 2042 } 2043 2044 2045 /** 2046 * smack_cred_free - "free" task-level security credentials 2047 * @cred: the credentials in question 2048 * 2049 */ 2050 static void smack_cred_free(struct cred *cred) 2051 { 2052 struct task_smack *tsp = smack_cred(cred); 2053 struct smack_rule *rp; 2054 struct list_head *l; 2055 struct list_head *n; 2056 2057 smk_destroy_label_list(&tsp->smk_relabel); 2058 2059 list_for_each_safe(l, n, &tsp->smk_rules) { 2060 rp = list_entry(l, struct smack_rule, list); 2061 list_del(&rp->list); 2062 kmem_cache_free(smack_rule_cache, rp); 2063 } 2064 } 2065 2066 /** 2067 * smack_cred_prepare - prepare new set of credentials for modification 2068 * @new: the new credentials 2069 * @old: the original credentials 2070 * @gfp: the atomicity of any memory allocations 2071 * 2072 * Prepare a new set of credentials for modification. 2073 */ 2074 static int smack_cred_prepare(struct cred *new, const struct cred *old, 2075 gfp_t gfp) 2076 { 2077 struct task_smack *old_tsp = smack_cred(old); 2078 struct task_smack *new_tsp = smack_cred(new); 2079 int rc; 2080 2081 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task); 2082 2083 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp); 2084 if (rc != 0) 2085 return rc; 2086 2087 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel, 2088 gfp); 2089 return rc; 2090 } 2091 2092 /** 2093 * smack_cred_transfer - Transfer the old credentials to the new credentials 2094 * @new: the new credentials 2095 * @old: the original credentials 2096 * 2097 * Fill in a set of blank credentials from another set of credentials. 2098 */ 2099 static void smack_cred_transfer(struct cred *new, const struct cred *old) 2100 { 2101 struct task_smack *old_tsp = smack_cred(old); 2102 struct task_smack *new_tsp = smack_cred(new); 2103 2104 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task); 2105 } 2106 2107 /** 2108 * smack_cred_getsecid - get the secid corresponding to a creds structure 2109 * @cred: the object creds 2110 * @secid: where to put the result 2111 * 2112 * Sets the secid to contain a u32 version of the smack label. 2113 */ 2114 static void smack_cred_getsecid(const struct cred *cred, u32 *secid) 2115 { 2116 struct smack_known *skp; 2117 2118 rcu_read_lock(); 2119 skp = smk_of_task(smack_cred(cred)); 2120 *secid = skp->smk_secid; 2121 rcu_read_unlock(); 2122 } 2123 2124 /** 2125 * smack_kernel_act_as - Set the subjective context in a set of credentials 2126 * @new: points to the set of credentials to be modified. 2127 * @secid: specifies the security ID to be set 2128 * 2129 * Set the security data for a kernel service. 2130 */ 2131 static int smack_kernel_act_as(struct cred *new, u32 secid) 2132 { 2133 struct task_smack *new_tsp = smack_cred(new); 2134 2135 new_tsp->smk_task = smack_from_secid(secid); 2136 return 0; 2137 } 2138 2139 /** 2140 * smack_kernel_create_files_as - Set the file creation label in a set of creds 2141 * @new: points to the set of credentials to be modified 2142 * @inode: points to the inode to use as a reference 2143 * 2144 * Set the file creation context in a set of credentials to the same 2145 * as the objective context of the specified inode 2146 */ 2147 static int smack_kernel_create_files_as(struct cred *new, 2148 struct inode *inode) 2149 { 2150 struct inode_smack *isp = smack_inode(inode); 2151 struct task_smack *tsp = smack_cred(new); 2152 2153 tsp->smk_forked = isp->smk_inode; 2154 tsp->smk_task = tsp->smk_forked; 2155 return 0; 2156 } 2157 2158 /** 2159 * smk_curacc_on_task - helper to log task related access 2160 * @p: the task object 2161 * @access: the access requested 2162 * @caller: name of the calling function for audit 2163 * 2164 * Return 0 if access is permitted 2165 */ 2166 static int smk_curacc_on_task(struct task_struct *p, int access, 2167 const char *caller) 2168 { 2169 struct smk_audit_info ad; 2170 struct smack_known *skp = smk_of_task_struct_obj(p); 2171 int rc; 2172 2173 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK); 2174 smk_ad_setfield_u_tsk(&ad, p); 2175 rc = smk_curacc(skp, access, &ad); 2176 rc = smk_bu_task(p, access, rc); 2177 return rc; 2178 } 2179 2180 /** 2181 * smack_task_setpgid - Smack check on setting pgid 2182 * @p: the task object 2183 * @pgid: unused 2184 * 2185 * Return 0 if write access is permitted 2186 */ 2187 static int smack_task_setpgid(struct task_struct *p, pid_t pgid) 2188 { 2189 return smk_curacc_on_task(p, MAY_WRITE, __func__); 2190 } 2191 2192 /** 2193 * smack_task_getpgid - Smack access check for getpgid 2194 * @p: the object task 2195 * 2196 * Returns 0 if current can read the object task, error code otherwise 2197 */ 2198 static int smack_task_getpgid(struct task_struct *p) 2199 { 2200 return smk_curacc_on_task(p, MAY_READ, __func__); 2201 } 2202 2203 /** 2204 * smack_task_getsid - Smack access check for getsid 2205 * @p: the object task 2206 * 2207 * Returns 0 if current can read the object task, error code otherwise 2208 */ 2209 static int smack_task_getsid(struct task_struct *p) 2210 { 2211 return smk_curacc_on_task(p, MAY_READ, __func__); 2212 } 2213 2214 /** 2215 * smack_current_getsecid_subj - get the subjective secid of the current task 2216 * @secid: where to put the result 2217 * 2218 * Sets the secid to contain a u32 version of the task's subjective smack label. 2219 */ 2220 static void smack_current_getsecid_subj(u32 *secid) 2221 { 2222 struct smack_known *skp = smk_of_current(); 2223 2224 *secid = skp->smk_secid; 2225 } 2226 2227 /** 2228 * smack_task_getsecid_obj - get the objective secid of the task 2229 * @p: the task 2230 * @secid: where to put the result 2231 * 2232 * Sets the secid to contain a u32 version of the task's objective smack label. 2233 */ 2234 static void smack_task_getsecid_obj(struct task_struct *p, u32 *secid) 2235 { 2236 struct smack_known *skp = smk_of_task_struct_obj(p); 2237 2238 *secid = skp->smk_secid; 2239 } 2240 2241 /** 2242 * smack_task_setnice - Smack check on setting nice 2243 * @p: the task object 2244 * @nice: unused 2245 * 2246 * Return 0 if write access is permitted 2247 */ 2248 static int smack_task_setnice(struct task_struct *p, int nice) 2249 { 2250 return smk_curacc_on_task(p, MAY_WRITE, __func__); 2251 } 2252 2253 /** 2254 * smack_task_setioprio - Smack check on setting ioprio 2255 * @p: the task object 2256 * @ioprio: unused 2257 * 2258 * Return 0 if write access is permitted 2259 */ 2260 static int smack_task_setioprio(struct task_struct *p, int ioprio) 2261 { 2262 return smk_curacc_on_task(p, MAY_WRITE, __func__); 2263 } 2264 2265 /** 2266 * smack_task_getioprio - Smack check on reading ioprio 2267 * @p: the task object 2268 * 2269 * Return 0 if read access is permitted 2270 */ 2271 static int smack_task_getioprio(struct task_struct *p) 2272 { 2273 return smk_curacc_on_task(p, MAY_READ, __func__); 2274 } 2275 2276 /** 2277 * smack_task_setscheduler - Smack check on setting scheduler 2278 * @p: the task object 2279 * 2280 * Return 0 if read access is permitted 2281 */ 2282 static int smack_task_setscheduler(struct task_struct *p) 2283 { 2284 return smk_curacc_on_task(p, MAY_WRITE, __func__); 2285 } 2286 2287 /** 2288 * smack_task_getscheduler - Smack check on reading scheduler 2289 * @p: the task object 2290 * 2291 * Return 0 if read access is permitted 2292 */ 2293 static int smack_task_getscheduler(struct task_struct *p) 2294 { 2295 return smk_curacc_on_task(p, MAY_READ, __func__); 2296 } 2297 2298 /** 2299 * smack_task_movememory - Smack check on moving memory 2300 * @p: the task object 2301 * 2302 * Return 0 if write access is permitted 2303 */ 2304 static int smack_task_movememory(struct task_struct *p) 2305 { 2306 return smk_curacc_on_task(p, MAY_WRITE, __func__); 2307 } 2308 2309 /** 2310 * smack_task_kill - Smack check on signal delivery 2311 * @p: the task object 2312 * @info: unused 2313 * @sig: unused 2314 * @cred: identifies the cred to use in lieu of current's 2315 * 2316 * Return 0 if write access is permitted 2317 * 2318 */ 2319 static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info, 2320 int sig, const struct cred *cred) 2321 { 2322 struct smk_audit_info ad; 2323 struct smack_known *skp; 2324 struct smack_known *tkp = smk_of_task_struct_obj(p); 2325 int rc; 2326 2327 if (!sig) 2328 return 0; /* null signal; existence test */ 2329 2330 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK); 2331 smk_ad_setfield_u_tsk(&ad, p); 2332 /* 2333 * Sending a signal requires that the sender 2334 * can write the receiver. 2335 */ 2336 if (cred == NULL) { 2337 rc = smk_curacc(tkp, MAY_DELIVER, &ad); 2338 rc = smk_bu_task(p, MAY_DELIVER, rc); 2339 return rc; 2340 } 2341 /* 2342 * If the cred isn't NULL we're dealing with some USB IO 2343 * specific behavior. This is not clean. For one thing 2344 * we can't take privilege into account. 2345 */ 2346 skp = smk_of_task(smack_cred(cred)); 2347 rc = smk_access(skp, tkp, MAY_DELIVER, &ad); 2348 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc); 2349 return rc; 2350 } 2351 2352 /** 2353 * smack_task_to_inode - copy task smack into the inode blob 2354 * @p: task to copy from 2355 * @inode: inode to copy to 2356 * 2357 * Sets the smack pointer in the inode security blob 2358 */ 2359 static void smack_task_to_inode(struct task_struct *p, struct inode *inode) 2360 { 2361 struct inode_smack *isp = smack_inode(inode); 2362 struct smack_known *skp = smk_of_task_struct_obj(p); 2363 2364 isp->smk_inode = skp; 2365 isp->smk_flags |= SMK_INODE_INSTANT; 2366 } 2367 2368 /* 2369 * Socket hooks. 2370 */ 2371 2372 /** 2373 * smack_sk_alloc_security - Allocate a socket blob 2374 * @sk: the socket 2375 * @family: unused 2376 * @gfp_flags: memory allocation flags 2377 * 2378 * Assign Smack pointers to current 2379 * 2380 * Returns 0 on success, -ENOMEM is there's no memory 2381 */ 2382 static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags) 2383 { 2384 struct smack_known *skp = smk_of_current(); 2385 struct socket_smack *ssp; 2386 2387 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags); 2388 if (ssp == NULL) 2389 return -ENOMEM; 2390 2391 /* 2392 * Sockets created by kernel threads receive web label. 2393 */ 2394 if (unlikely(current->flags & PF_KTHREAD)) { 2395 ssp->smk_in = &smack_known_web; 2396 ssp->smk_out = &smack_known_web; 2397 } else { 2398 ssp->smk_in = skp; 2399 ssp->smk_out = skp; 2400 } 2401 ssp->smk_packet = NULL; 2402 2403 sk->sk_security = ssp; 2404 2405 return 0; 2406 } 2407 2408 /** 2409 * smack_sk_free_security - Free a socket blob 2410 * @sk: the socket 2411 * 2412 * Clears the blob pointer 2413 */ 2414 static void smack_sk_free_security(struct sock *sk) 2415 { 2416 #ifdef SMACK_IPV6_PORT_LABELING 2417 struct smk_port_label *spp; 2418 2419 if (sk->sk_family == PF_INET6) { 2420 rcu_read_lock(); 2421 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) { 2422 if (spp->smk_sock != sk) 2423 continue; 2424 spp->smk_can_reuse = 1; 2425 break; 2426 } 2427 rcu_read_unlock(); 2428 } 2429 #endif 2430 kfree(sk->sk_security); 2431 } 2432 2433 /** 2434 * smack_sk_clone_security - Copy security context 2435 * @sk: the old socket 2436 * @newsk: the new socket 2437 * 2438 * Copy the security context of the old socket pointer to the cloned 2439 */ 2440 static void smack_sk_clone_security(const struct sock *sk, struct sock *newsk) 2441 { 2442 struct socket_smack *ssp_old = sk->sk_security; 2443 struct socket_smack *ssp_new = newsk->sk_security; 2444 2445 *ssp_new = *ssp_old; 2446 } 2447 2448 /** 2449 * smack_ipv4host_label - check host based restrictions 2450 * @sip: the object end 2451 * 2452 * looks for host based access restrictions 2453 * 2454 * This version will only be appropriate for really small sets of single label 2455 * hosts. The caller is responsible for ensuring that the RCU read lock is 2456 * taken before calling this function. 2457 * 2458 * Returns the label of the far end or NULL if it's not special. 2459 */ 2460 static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip) 2461 { 2462 struct smk_net4addr *snp; 2463 struct in_addr *siap = &sip->sin_addr; 2464 2465 if (siap->s_addr == 0) 2466 return NULL; 2467 2468 list_for_each_entry_rcu(snp, &smk_net4addr_list, list) 2469 /* 2470 * we break after finding the first match because 2471 * the list is sorted from longest to shortest mask 2472 * so we have found the most specific match 2473 */ 2474 if (snp->smk_host.s_addr == 2475 (siap->s_addr & snp->smk_mask.s_addr)) 2476 return snp->smk_label; 2477 2478 return NULL; 2479 } 2480 2481 /* 2482 * smk_ipv6_localhost - Check for local ipv6 host address 2483 * @sip: the address 2484 * 2485 * Returns boolean true if this is the localhost address 2486 */ 2487 static bool smk_ipv6_localhost(struct sockaddr_in6 *sip) 2488 { 2489 __be16 *be16p = (__be16 *)&sip->sin6_addr; 2490 __be32 *be32p = (__be32 *)&sip->sin6_addr; 2491 2492 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 && 2493 ntohs(be16p[7]) == 1) 2494 return true; 2495 return false; 2496 } 2497 2498 /** 2499 * smack_ipv6host_label - check host based restrictions 2500 * @sip: the object end 2501 * 2502 * looks for host based access restrictions 2503 * 2504 * This version will only be appropriate for really small sets of single label 2505 * hosts. The caller is responsible for ensuring that the RCU read lock is 2506 * taken before calling this function. 2507 * 2508 * Returns the label of the far end or NULL if it's not special. 2509 */ 2510 static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip) 2511 { 2512 struct smk_net6addr *snp; 2513 struct in6_addr *sap = &sip->sin6_addr; 2514 int i; 2515 int found = 0; 2516 2517 /* 2518 * It's local. Don't look for a host label. 2519 */ 2520 if (smk_ipv6_localhost(sip)) 2521 return NULL; 2522 2523 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) { 2524 /* 2525 * If the label is NULL the entry has 2526 * been renounced. Ignore it. 2527 */ 2528 if (snp->smk_label == NULL) 2529 continue; 2530 /* 2531 * we break after finding the first match because 2532 * the list is sorted from longest to shortest mask 2533 * so we have found the most specific match 2534 */ 2535 for (found = 1, i = 0; i < 8; i++) { 2536 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) != 2537 snp->smk_host.s6_addr16[i]) { 2538 found = 0; 2539 break; 2540 } 2541 } 2542 if (found) 2543 return snp->smk_label; 2544 } 2545 2546 return NULL; 2547 } 2548 2549 /** 2550 * smack_netlbl_add - Set the secattr on a socket 2551 * @sk: the socket 2552 * 2553 * Attach the outbound smack value (smk_out) to the socket. 2554 * 2555 * Returns 0 on success or an error code 2556 */ 2557 static int smack_netlbl_add(struct sock *sk) 2558 { 2559 struct socket_smack *ssp = sk->sk_security; 2560 struct smack_known *skp = ssp->smk_out; 2561 int rc; 2562 2563 local_bh_disable(); 2564 bh_lock_sock_nested(sk); 2565 2566 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel); 2567 switch (rc) { 2568 case 0: 2569 ssp->smk_state = SMK_NETLBL_LABELED; 2570 break; 2571 case -EDESTADDRREQ: 2572 ssp->smk_state = SMK_NETLBL_REQSKB; 2573 rc = 0; 2574 break; 2575 } 2576 2577 bh_unlock_sock(sk); 2578 local_bh_enable(); 2579 2580 return rc; 2581 } 2582 2583 /** 2584 * smack_netlbl_delete - Remove the secattr from a socket 2585 * @sk: the socket 2586 * 2587 * Remove the outbound smack value from a socket 2588 */ 2589 static void smack_netlbl_delete(struct sock *sk) 2590 { 2591 struct socket_smack *ssp = sk->sk_security; 2592 2593 /* 2594 * Take the label off the socket if one is set. 2595 */ 2596 if (ssp->smk_state != SMK_NETLBL_LABELED) 2597 return; 2598 2599 local_bh_disable(); 2600 bh_lock_sock_nested(sk); 2601 netlbl_sock_delattr(sk); 2602 bh_unlock_sock(sk); 2603 local_bh_enable(); 2604 ssp->smk_state = SMK_NETLBL_UNLABELED; 2605 } 2606 2607 /** 2608 * smk_ipv4_check - Perform IPv4 host access checks 2609 * @sk: the socket 2610 * @sap: the destination address 2611 * 2612 * Set the correct secattr for the given socket based on the destination 2613 * address and perform any outbound access checks needed. 2614 * 2615 * Returns 0 on success or an error code. 2616 * 2617 */ 2618 static int smk_ipv4_check(struct sock *sk, struct sockaddr_in *sap) 2619 { 2620 struct smack_known *skp; 2621 int rc = 0; 2622 struct smack_known *hkp; 2623 struct socket_smack *ssp = sk->sk_security; 2624 struct smk_audit_info ad; 2625 2626 rcu_read_lock(); 2627 hkp = smack_ipv4host_label(sap); 2628 if (hkp != NULL) { 2629 #ifdef CONFIG_AUDIT 2630 struct lsm_network_audit net; 2631 2632 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); 2633 ad.a.u.net->family = sap->sin_family; 2634 ad.a.u.net->dport = sap->sin_port; 2635 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr; 2636 #endif 2637 skp = ssp->smk_out; 2638 rc = smk_access(skp, hkp, MAY_WRITE, &ad); 2639 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc); 2640 /* 2641 * Clear the socket netlabel if it's set. 2642 */ 2643 if (!rc) 2644 smack_netlbl_delete(sk); 2645 } 2646 rcu_read_unlock(); 2647 2648 return rc; 2649 } 2650 2651 /** 2652 * smk_ipv6_check - check Smack access 2653 * @subject: subject Smack label 2654 * @object: object Smack label 2655 * @address: address 2656 * @act: the action being taken 2657 * 2658 * Check an IPv6 access 2659 */ 2660 static int smk_ipv6_check(struct smack_known *subject, 2661 struct smack_known *object, 2662 struct sockaddr_in6 *address, int act) 2663 { 2664 #ifdef CONFIG_AUDIT 2665 struct lsm_network_audit net; 2666 #endif 2667 struct smk_audit_info ad; 2668 int rc; 2669 2670 #ifdef CONFIG_AUDIT 2671 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); 2672 ad.a.u.net->family = PF_INET6; 2673 ad.a.u.net->dport = address->sin6_port; 2674 if (act == SMK_RECEIVING) 2675 ad.a.u.net->v6info.saddr = address->sin6_addr; 2676 else 2677 ad.a.u.net->v6info.daddr = address->sin6_addr; 2678 #endif 2679 rc = smk_access(subject, object, MAY_WRITE, &ad); 2680 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc); 2681 return rc; 2682 } 2683 2684 #ifdef SMACK_IPV6_PORT_LABELING 2685 /** 2686 * smk_ipv6_port_label - Smack port access table management 2687 * @sock: socket 2688 * @address: address 2689 * 2690 * Create or update the port list entry 2691 */ 2692 static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address) 2693 { 2694 struct sock *sk = sock->sk; 2695 struct sockaddr_in6 *addr6; 2696 struct socket_smack *ssp = sock->sk->sk_security; 2697 struct smk_port_label *spp; 2698 unsigned short port = 0; 2699 2700 if (address == NULL) { 2701 /* 2702 * This operation is changing the Smack information 2703 * on the bound socket. Take the changes to the port 2704 * as well. 2705 */ 2706 rcu_read_lock(); 2707 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) { 2708 if (sk != spp->smk_sock) 2709 continue; 2710 spp->smk_in = ssp->smk_in; 2711 spp->smk_out = ssp->smk_out; 2712 rcu_read_unlock(); 2713 return; 2714 } 2715 /* 2716 * A NULL address is only used for updating existing 2717 * bound entries. If there isn't one, it's OK. 2718 */ 2719 rcu_read_unlock(); 2720 return; 2721 } 2722 2723 addr6 = (struct sockaddr_in6 *)address; 2724 port = ntohs(addr6->sin6_port); 2725 /* 2726 * This is a special case that is safely ignored. 2727 */ 2728 if (port == 0) 2729 return; 2730 2731 /* 2732 * Look for an existing port list entry. 2733 * This is an indication that a port is getting reused. 2734 */ 2735 rcu_read_lock(); 2736 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) { 2737 if (spp->smk_port != port || spp->smk_sock_type != sock->type) 2738 continue; 2739 if (spp->smk_can_reuse != 1) { 2740 rcu_read_unlock(); 2741 return; 2742 } 2743 spp->smk_port = port; 2744 spp->smk_sock = sk; 2745 spp->smk_in = ssp->smk_in; 2746 spp->smk_out = ssp->smk_out; 2747 spp->smk_can_reuse = 0; 2748 rcu_read_unlock(); 2749 return; 2750 } 2751 rcu_read_unlock(); 2752 /* 2753 * A new port entry is required. 2754 */ 2755 spp = kzalloc(sizeof(*spp), GFP_KERNEL); 2756 if (spp == NULL) 2757 return; 2758 2759 spp->smk_port = port; 2760 spp->smk_sock = sk; 2761 spp->smk_in = ssp->smk_in; 2762 spp->smk_out = ssp->smk_out; 2763 spp->smk_sock_type = sock->type; 2764 spp->smk_can_reuse = 0; 2765 2766 mutex_lock(&smack_ipv6_lock); 2767 list_add_rcu(&spp->list, &smk_ipv6_port_list); 2768 mutex_unlock(&smack_ipv6_lock); 2769 return; 2770 } 2771 2772 /** 2773 * smk_ipv6_port_check - check Smack port access 2774 * @sk: socket 2775 * @address: address 2776 * @act: the action being taken 2777 * 2778 * Create or update the port list entry 2779 */ 2780 static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address, 2781 int act) 2782 { 2783 struct smk_port_label *spp; 2784 struct socket_smack *ssp = sk->sk_security; 2785 struct smack_known *skp = NULL; 2786 unsigned short port; 2787 struct smack_known *object; 2788 2789 if (act == SMK_RECEIVING) { 2790 skp = smack_ipv6host_label(address); 2791 object = ssp->smk_in; 2792 } else { 2793 skp = ssp->smk_out; 2794 object = smack_ipv6host_label(address); 2795 } 2796 2797 /* 2798 * The other end is a single label host. 2799 */ 2800 if (skp != NULL && object != NULL) 2801 return smk_ipv6_check(skp, object, address, act); 2802 if (skp == NULL) 2803 skp = smack_net_ambient; 2804 if (object == NULL) 2805 object = smack_net_ambient; 2806 2807 /* 2808 * It's remote, so port lookup does no good. 2809 */ 2810 if (!smk_ipv6_localhost(address)) 2811 return smk_ipv6_check(skp, object, address, act); 2812 2813 /* 2814 * It's local so the send check has to have passed. 2815 */ 2816 if (act == SMK_RECEIVING) 2817 return 0; 2818 2819 port = ntohs(address->sin6_port); 2820 rcu_read_lock(); 2821 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) { 2822 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type) 2823 continue; 2824 object = spp->smk_in; 2825 if (act == SMK_CONNECTING) 2826 ssp->smk_packet = spp->smk_out; 2827 break; 2828 } 2829 rcu_read_unlock(); 2830 2831 return smk_ipv6_check(skp, object, address, act); 2832 } 2833 #endif 2834 2835 /** 2836 * smack_inode_setsecurity - set smack xattrs 2837 * @inode: the object 2838 * @name: attribute name 2839 * @value: attribute value 2840 * @size: size of the attribute 2841 * @flags: unused 2842 * 2843 * Sets the named attribute in the appropriate blob 2844 * 2845 * Returns 0 on success, or an error code 2846 */ 2847 static int smack_inode_setsecurity(struct inode *inode, const char *name, 2848 const void *value, size_t size, int flags) 2849 { 2850 struct smack_known *skp; 2851 struct inode_smack *nsp = smack_inode(inode); 2852 struct socket_smack *ssp; 2853 struct socket *sock; 2854 int rc = 0; 2855 2856 if (value == NULL || size > SMK_LONGLABEL || size == 0) 2857 return -EINVAL; 2858 2859 if (strcmp(name, XATTR_SMACK_TRANSMUTE) == 0) { 2860 if (!S_ISDIR(inode->i_mode) || size != TRANS_TRUE_SIZE || 2861 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0) 2862 return -EINVAL; 2863 2864 nsp->smk_flags |= SMK_INODE_TRANSMUTE; 2865 return 0; 2866 } 2867 2868 skp = smk_import_entry(value, size); 2869 if (IS_ERR(skp)) 2870 return PTR_ERR(skp); 2871 2872 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) { 2873 nsp->smk_inode = skp; 2874 nsp->smk_flags |= SMK_INODE_INSTANT; 2875 return 0; 2876 } 2877 /* 2878 * The rest of the Smack xattrs are only on sockets. 2879 */ 2880 if (inode->i_sb->s_magic != SOCKFS_MAGIC) 2881 return -EOPNOTSUPP; 2882 2883 sock = SOCKET_I(inode); 2884 if (sock == NULL || sock->sk == NULL) 2885 return -EOPNOTSUPP; 2886 2887 ssp = sock->sk->sk_security; 2888 2889 if (strcmp(name, XATTR_SMACK_IPIN) == 0) 2890 ssp->smk_in = skp; 2891 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) { 2892 ssp->smk_out = skp; 2893 if (sock->sk->sk_family == PF_INET) { 2894 rc = smack_netlbl_add(sock->sk); 2895 if (rc != 0) 2896 printk(KERN_WARNING 2897 "Smack: \"%s\" netlbl error %d.\n", 2898 __func__, -rc); 2899 } 2900 } else 2901 return -EOPNOTSUPP; 2902 2903 #ifdef SMACK_IPV6_PORT_LABELING 2904 if (sock->sk->sk_family == PF_INET6) 2905 smk_ipv6_port_label(sock, NULL); 2906 #endif 2907 2908 return 0; 2909 } 2910 2911 /** 2912 * smack_socket_post_create - finish socket setup 2913 * @sock: the socket 2914 * @family: protocol family 2915 * @type: unused 2916 * @protocol: unused 2917 * @kern: unused 2918 * 2919 * Sets the netlabel information on the socket 2920 * 2921 * Returns 0 on success, and error code otherwise 2922 */ 2923 static int smack_socket_post_create(struct socket *sock, int family, 2924 int type, int protocol, int kern) 2925 { 2926 struct socket_smack *ssp; 2927 2928 if (sock->sk == NULL) 2929 return 0; 2930 2931 /* 2932 * Sockets created by kernel threads receive web label. 2933 */ 2934 if (unlikely(current->flags & PF_KTHREAD)) { 2935 ssp = sock->sk->sk_security; 2936 ssp->smk_in = &smack_known_web; 2937 ssp->smk_out = &smack_known_web; 2938 } 2939 2940 if (family != PF_INET) 2941 return 0; 2942 /* 2943 * Set the outbound netlbl. 2944 */ 2945 return smack_netlbl_add(sock->sk); 2946 } 2947 2948 /** 2949 * smack_socket_socketpair - create socket pair 2950 * @socka: one socket 2951 * @sockb: another socket 2952 * 2953 * Cross reference the peer labels for SO_PEERSEC 2954 * 2955 * Returns 0 2956 */ 2957 static int smack_socket_socketpair(struct socket *socka, 2958 struct socket *sockb) 2959 { 2960 struct socket_smack *asp = socka->sk->sk_security; 2961 struct socket_smack *bsp = sockb->sk->sk_security; 2962 2963 asp->smk_packet = bsp->smk_out; 2964 bsp->smk_packet = asp->smk_out; 2965 2966 return 0; 2967 } 2968 2969 #ifdef SMACK_IPV6_PORT_LABELING 2970 /** 2971 * smack_socket_bind - record port binding information. 2972 * @sock: the socket 2973 * @address: the port address 2974 * @addrlen: size of the address 2975 * 2976 * Records the label bound to a port. 2977 * 2978 * Returns 0 on success, and error code otherwise 2979 */ 2980 static int smack_socket_bind(struct socket *sock, struct sockaddr *address, 2981 int addrlen) 2982 { 2983 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) { 2984 if (addrlen < SIN6_LEN_RFC2133 || 2985 address->sa_family != AF_INET6) 2986 return -EINVAL; 2987 smk_ipv6_port_label(sock, address); 2988 } 2989 return 0; 2990 } 2991 #endif /* SMACK_IPV6_PORT_LABELING */ 2992 2993 /** 2994 * smack_socket_connect - connect access check 2995 * @sock: the socket 2996 * @sap: the other end 2997 * @addrlen: size of sap 2998 * 2999 * Verifies that a connection may be possible 3000 * 3001 * Returns 0 on success, and error code otherwise 3002 */ 3003 static int smack_socket_connect(struct socket *sock, struct sockaddr *sap, 3004 int addrlen) 3005 { 3006 int rc = 0; 3007 3008 if (sock->sk == NULL) 3009 return 0; 3010 if (sock->sk->sk_family != PF_INET && 3011 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6)) 3012 return 0; 3013 if (addrlen < offsetofend(struct sockaddr, sa_family)) 3014 return 0; 3015 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) { 3016 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap; 3017 struct smack_known *rsp = NULL; 3018 3019 if (addrlen < SIN6_LEN_RFC2133) 3020 return 0; 3021 if (__is_defined(SMACK_IPV6_SECMARK_LABELING)) 3022 rsp = smack_ipv6host_label(sip); 3023 if (rsp != NULL) { 3024 struct socket_smack *ssp = sock->sk->sk_security; 3025 3026 rc = smk_ipv6_check(ssp->smk_out, rsp, sip, 3027 SMK_CONNECTING); 3028 } 3029 #ifdef SMACK_IPV6_PORT_LABELING 3030 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING); 3031 #endif 3032 3033 return rc; 3034 } 3035 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in)) 3036 return 0; 3037 rc = smk_ipv4_check(sock->sk, (struct sockaddr_in *)sap); 3038 return rc; 3039 } 3040 3041 /** 3042 * smack_flags_to_may - convert S_ to MAY_ values 3043 * @flags: the S_ value 3044 * 3045 * Returns the equivalent MAY_ value 3046 */ 3047 static int smack_flags_to_may(int flags) 3048 { 3049 int may = 0; 3050 3051 if (flags & S_IRUGO) 3052 may |= MAY_READ; 3053 if (flags & S_IWUGO) 3054 may |= MAY_WRITE; 3055 if (flags & S_IXUGO) 3056 may |= MAY_EXEC; 3057 3058 return may; 3059 } 3060 3061 /** 3062 * smack_msg_msg_alloc_security - Set the security blob for msg_msg 3063 * @msg: the object 3064 * 3065 * Returns 0 3066 */ 3067 static int smack_msg_msg_alloc_security(struct msg_msg *msg) 3068 { 3069 struct smack_known **blob = smack_msg_msg(msg); 3070 3071 *blob = smk_of_current(); 3072 return 0; 3073 } 3074 3075 /** 3076 * smack_of_ipc - the smack pointer for the ipc 3077 * @isp: the object 3078 * 3079 * Returns a pointer to the smack value 3080 */ 3081 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp) 3082 { 3083 struct smack_known **blob = smack_ipc(isp); 3084 3085 return *blob; 3086 } 3087 3088 /** 3089 * smack_ipc_alloc_security - Set the security blob for ipc 3090 * @isp: the object 3091 * 3092 * Returns 0 3093 */ 3094 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp) 3095 { 3096 struct smack_known **blob = smack_ipc(isp); 3097 3098 *blob = smk_of_current(); 3099 return 0; 3100 } 3101 3102 /** 3103 * smk_curacc_shm : check if current has access on shm 3104 * @isp : the object 3105 * @access : access requested 3106 * 3107 * Returns 0 if current has the requested access, error code otherwise 3108 */ 3109 static int smk_curacc_shm(struct kern_ipc_perm *isp, int access) 3110 { 3111 struct smack_known *ssp = smack_of_ipc(isp); 3112 struct smk_audit_info ad; 3113 int rc; 3114 3115 #ifdef CONFIG_AUDIT 3116 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC); 3117 ad.a.u.ipc_id = isp->id; 3118 #endif 3119 rc = smk_curacc(ssp, access, &ad); 3120 rc = smk_bu_current("shm", ssp, access, rc); 3121 return rc; 3122 } 3123 3124 /** 3125 * smack_shm_associate - Smack access check for shm 3126 * @isp: the object 3127 * @shmflg: access requested 3128 * 3129 * Returns 0 if current has the requested access, error code otherwise 3130 */ 3131 static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg) 3132 { 3133 int may; 3134 3135 may = smack_flags_to_may(shmflg); 3136 return smk_curacc_shm(isp, may); 3137 } 3138 3139 /** 3140 * smack_shm_shmctl - Smack access check for shm 3141 * @isp: the object 3142 * @cmd: what it wants to do 3143 * 3144 * Returns 0 if current has the requested access, error code otherwise 3145 */ 3146 static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd) 3147 { 3148 int may; 3149 3150 switch (cmd) { 3151 case IPC_STAT: 3152 case SHM_STAT: 3153 case SHM_STAT_ANY: 3154 may = MAY_READ; 3155 break; 3156 case IPC_SET: 3157 case SHM_LOCK: 3158 case SHM_UNLOCK: 3159 case IPC_RMID: 3160 may = MAY_READWRITE; 3161 break; 3162 case IPC_INFO: 3163 case SHM_INFO: 3164 /* 3165 * System level information. 3166 */ 3167 return 0; 3168 default: 3169 return -EINVAL; 3170 } 3171 return smk_curacc_shm(isp, may); 3172 } 3173 3174 /** 3175 * smack_shm_shmat - Smack access for shmat 3176 * @isp: the object 3177 * @shmaddr: unused 3178 * @shmflg: access requested 3179 * 3180 * Returns 0 if current has the requested access, error code otherwise 3181 */ 3182 static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr, 3183 int shmflg) 3184 { 3185 int may; 3186 3187 may = smack_flags_to_may(shmflg); 3188 return smk_curacc_shm(isp, may); 3189 } 3190 3191 /** 3192 * smk_curacc_sem : check if current has access on sem 3193 * @isp : the object 3194 * @access : access requested 3195 * 3196 * Returns 0 if current has the requested access, error code otherwise 3197 */ 3198 static int smk_curacc_sem(struct kern_ipc_perm *isp, int access) 3199 { 3200 struct smack_known *ssp = smack_of_ipc(isp); 3201 struct smk_audit_info ad; 3202 int rc; 3203 3204 #ifdef CONFIG_AUDIT 3205 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC); 3206 ad.a.u.ipc_id = isp->id; 3207 #endif 3208 rc = smk_curacc(ssp, access, &ad); 3209 rc = smk_bu_current("sem", ssp, access, rc); 3210 return rc; 3211 } 3212 3213 /** 3214 * smack_sem_associate - Smack access check for sem 3215 * @isp: the object 3216 * @semflg: access requested 3217 * 3218 * Returns 0 if current has the requested access, error code otherwise 3219 */ 3220 static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg) 3221 { 3222 int may; 3223 3224 may = smack_flags_to_may(semflg); 3225 return smk_curacc_sem(isp, may); 3226 } 3227 3228 /** 3229 * smack_sem_semctl - Smack access check for sem 3230 * @isp: the object 3231 * @cmd: what it wants to do 3232 * 3233 * Returns 0 if current has the requested access, error code otherwise 3234 */ 3235 static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd) 3236 { 3237 int may; 3238 3239 switch (cmd) { 3240 case GETPID: 3241 case GETNCNT: 3242 case GETZCNT: 3243 case GETVAL: 3244 case GETALL: 3245 case IPC_STAT: 3246 case SEM_STAT: 3247 case SEM_STAT_ANY: 3248 may = MAY_READ; 3249 break; 3250 case SETVAL: 3251 case SETALL: 3252 case IPC_RMID: 3253 case IPC_SET: 3254 may = MAY_READWRITE; 3255 break; 3256 case IPC_INFO: 3257 case SEM_INFO: 3258 /* 3259 * System level information 3260 */ 3261 return 0; 3262 default: 3263 return -EINVAL; 3264 } 3265 3266 return smk_curacc_sem(isp, may); 3267 } 3268 3269 /** 3270 * smack_sem_semop - Smack checks of semaphore operations 3271 * @isp: the object 3272 * @sops: unused 3273 * @nsops: unused 3274 * @alter: unused 3275 * 3276 * Treated as read and write in all cases. 3277 * 3278 * Returns 0 if access is allowed, error code otherwise 3279 */ 3280 static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops, 3281 unsigned nsops, int alter) 3282 { 3283 return smk_curacc_sem(isp, MAY_READWRITE); 3284 } 3285 3286 /** 3287 * smk_curacc_msq : helper to check if current has access on msq 3288 * @isp : the msq 3289 * @access : access requested 3290 * 3291 * return 0 if current has access, error otherwise 3292 */ 3293 static int smk_curacc_msq(struct kern_ipc_perm *isp, int access) 3294 { 3295 struct smack_known *msp = smack_of_ipc(isp); 3296 struct smk_audit_info ad; 3297 int rc; 3298 3299 #ifdef CONFIG_AUDIT 3300 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC); 3301 ad.a.u.ipc_id = isp->id; 3302 #endif 3303 rc = smk_curacc(msp, access, &ad); 3304 rc = smk_bu_current("msq", msp, access, rc); 3305 return rc; 3306 } 3307 3308 /** 3309 * smack_msg_queue_associate - Smack access check for msg_queue 3310 * @isp: the object 3311 * @msqflg: access requested 3312 * 3313 * Returns 0 if current has the requested access, error code otherwise 3314 */ 3315 static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg) 3316 { 3317 int may; 3318 3319 may = smack_flags_to_may(msqflg); 3320 return smk_curacc_msq(isp, may); 3321 } 3322 3323 /** 3324 * smack_msg_queue_msgctl - Smack access check for msg_queue 3325 * @isp: the object 3326 * @cmd: what it wants to do 3327 * 3328 * Returns 0 if current has the requested access, error code otherwise 3329 */ 3330 static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd) 3331 { 3332 int may; 3333 3334 switch (cmd) { 3335 case IPC_STAT: 3336 case MSG_STAT: 3337 case MSG_STAT_ANY: 3338 may = MAY_READ; 3339 break; 3340 case IPC_SET: 3341 case IPC_RMID: 3342 may = MAY_READWRITE; 3343 break; 3344 case IPC_INFO: 3345 case MSG_INFO: 3346 /* 3347 * System level information 3348 */ 3349 return 0; 3350 default: 3351 return -EINVAL; 3352 } 3353 3354 return smk_curacc_msq(isp, may); 3355 } 3356 3357 /** 3358 * smack_msg_queue_msgsnd - Smack access check for msg_queue 3359 * @isp: the object 3360 * @msg: unused 3361 * @msqflg: access requested 3362 * 3363 * Returns 0 if current has the requested access, error code otherwise 3364 */ 3365 static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg, 3366 int msqflg) 3367 { 3368 int may; 3369 3370 may = smack_flags_to_may(msqflg); 3371 return smk_curacc_msq(isp, may); 3372 } 3373 3374 /** 3375 * smack_msg_queue_msgrcv - Smack access check for msg_queue 3376 * @isp: the object 3377 * @msg: unused 3378 * @target: unused 3379 * @type: unused 3380 * @mode: unused 3381 * 3382 * Returns 0 if current has read and write access, error code otherwise 3383 */ 3384 static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, 3385 struct msg_msg *msg, 3386 struct task_struct *target, long type, 3387 int mode) 3388 { 3389 return smk_curacc_msq(isp, MAY_READWRITE); 3390 } 3391 3392 /** 3393 * smack_ipc_permission - Smack access for ipc_permission() 3394 * @ipp: the object permissions 3395 * @flag: access requested 3396 * 3397 * Returns 0 if current has read and write access, error code otherwise 3398 */ 3399 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag) 3400 { 3401 struct smack_known **blob = smack_ipc(ipp); 3402 struct smack_known *iskp = *blob; 3403 int may = smack_flags_to_may(flag); 3404 struct smk_audit_info ad; 3405 int rc; 3406 3407 #ifdef CONFIG_AUDIT 3408 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC); 3409 ad.a.u.ipc_id = ipp->id; 3410 #endif 3411 rc = smk_curacc(iskp, may, &ad); 3412 rc = smk_bu_current("svipc", iskp, may, rc); 3413 return rc; 3414 } 3415 3416 /** 3417 * smack_ipc_getsecid - Extract smack security id 3418 * @ipp: the object permissions 3419 * @secid: where result will be saved 3420 */ 3421 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid) 3422 { 3423 struct smack_known **blob = smack_ipc(ipp); 3424 struct smack_known *iskp = *blob; 3425 3426 *secid = iskp->smk_secid; 3427 } 3428 3429 /** 3430 * smack_d_instantiate - Make sure the blob is correct on an inode 3431 * @opt_dentry: dentry where inode will be attached 3432 * @inode: the object 3433 * 3434 * Set the inode's security blob if it hasn't been done already. 3435 */ 3436 static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode) 3437 { 3438 struct super_block *sbp; 3439 struct superblock_smack *sbsp; 3440 struct inode_smack *isp; 3441 struct smack_known *skp; 3442 struct smack_known *ckp = smk_of_current(); 3443 struct smack_known *final; 3444 char trattr[TRANS_TRUE_SIZE]; 3445 int transflag = 0; 3446 int rc; 3447 struct dentry *dp; 3448 3449 if (inode == NULL) 3450 return; 3451 3452 isp = smack_inode(inode); 3453 3454 /* 3455 * If the inode is already instantiated 3456 * take the quick way out 3457 */ 3458 if (isp->smk_flags & SMK_INODE_INSTANT) 3459 return; 3460 3461 sbp = inode->i_sb; 3462 sbsp = smack_superblock(sbp); 3463 /* 3464 * We're going to use the superblock default label 3465 * if there's no label on the file. 3466 */ 3467 final = sbsp->smk_default; 3468 3469 /* 3470 * If this is the root inode the superblock 3471 * may be in the process of initialization. 3472 * If that is the case use the root value out 3473 * of the superblock. 3474 */ 3475 if (opt_dentry->d_parent == opt_dentry) { 3476 switch (sbp->s_magic) { 3477 case CGROUP_SUPER_MAGIC: 3478 case CGROUP2_SUPER_MAGIC: 3479 /* 3480 * The cgroup filesystem is never mounted, 3481 * so there's no opportunity to set the mount 3482 * options. 3483 */ 3484 sbsp->smk_root = &smack_known_star; 3485 sbsp->smk_default = &smack_known_star; 3486 isp->smk_inode = sbsp->smk_root; 3487 break; 3488 case TMPFS_MAGIC: 3489 /* 3490 * What about shmem/tmpfs anonymous files with dentry 3491 * obtained from d_alloc_pseudo()? 3492 */ 3493 isp->smk_inode = smk_of_current(); 3494 break; 3495 case PIPEFS_MAGIC: 3496 isp->smk_inode = smk_of_current(); 3497 break; 3498 case SOCKFS_MAGIC: 3499 /* 3500 * Socket access is controlled by the socket 3501 * structures associated with the task involved. 3502 */ 3503 isp->smk_inode = &smack_known_star; 3504 break; 3505 default: 3506 isp->smk_inode = sbsp->smk_root; 3507 break; 3508 } 3509 isp->smk_flags |= SMK_INODE_INSTANT; 3510 return; 3511 } 3512 3513 /* 3514 * This is pretty hackish. 3515 * Casey says that we shouldn't have to do 3516 * file system specific code, but it does help 3517 * with keeping it simple. 3518 */ 3519 switch (sbp->s_magic) { 3520 case SMACK_MAGIC: 3521 case CGROUP_SUPER_MAGIC: 3522 case CGROUP2_SUPER_MAGIC: 3523 /* 3524 * Casey says that it's a little embarrassing 3525 * that the smack file system doesn't do 3526 * extended attributes. 3527 * 3528 * Cgroupfs is special 3529 */ 3530 final = &smack_known_star; 3531 break; 3532 case DEVPTS_SUPER_MAGIC: 3533 /* 3534 * devpts seems content with the label of the task. 3535 * Programs that change smack have to treat the 3536 * pty with respect. 3537 */ 3538 final = ckp; 3539 break; 3540 case PROC_SUPER_MAGIC: 3541 /* 3542 * Casey says procfs appears not to care. 3543 * The superblock default suffices. 3544 */ 3545 break; 3546 case TMPFS_MAGIC: 3547 /* 3548 * Device labels should come from the filesystem, 3549 * but watch out, because they're volitile, 3550 * getting recreated on every reboot. 3551 */ 3552 final = &smack_known_star; 3553 /* 3554 * If a smack value has been set we want to use it, 3555 * but since tmpfs isn't giving us the opportunity 3556 * to set mount options simulate setting the 3557 * superblock default. 3558 */ 3559 fallthrough; 3560 default: 3561 /* 3562 * This isn't an understood special case. 3563 * Get the value from the xattr. 3564 */ 3565 3566 /* 3567 * UNIX domain sockets use lower level socket data. 3568 */ 3569 if (S_ISSOCK(inode->i_mode)) { 3570 final = &smack_known_star; 3571 break; 3572 } 3573 /* 3574 * No xattr support means, alas, no SMACK label. 3575 * Use the aforeapplied default. 3576 * It would be curious if the label of the task 3577 * does not match that assigned. 3578 */ 3579 if (!(inode->i_opflags & IOP_XATTR)) 3580 break; 3581 /* 3582 * Get the dentry for xattr. 3583 */ 3584 dp = dget(opt_dentry); 3585 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp); 3586 if (!IS_ERR_OR_NULL(skp)) 3587 final = skp; 3588 3589 /* 3590 * Transmuting directory 3591 */ 3592 if (S_ISDIR(inode->i_mode)) { 3593 /* 3594 * If this is a new directory and the label was 3595 * transmuted when the inode was initialized 3596 * set the transmute attribute on the directory 3597 * and mark the inode. 3598 * 3599 * If there is a transmute attribute on the 3600 * directory mark the inode. 3601 */ 3602 rc = __vfs_getxattr(dp, inode, 3603 XATTR_NAME_SMACKTRANSMUTE, trattr, 3604 TRANS_TRUE_SIZE); 3605 if (rc >= 0 && strncmp(trattr, TRANS_TRUE, 3606 TRANS_TRUE_SIZE) != 0) 3607 rc = -EINVAL; 3608 if (rc >= 0) 3609 transflag = SMK_INODE_TRANSMUTE; 3610 } 3611 /* 3612 * Don't let the exec or mmap label be "*" or "@". 3613 */ 3614 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp); 3615 if (IS_ERR(skp) || skp == &smack_known_star || 3616 skp == &smack_known_web) 3617 skp = NULL; 3618 isp->smk_task = skp; 3619 3620 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp); 3621 if (IS_ERR(skp) || skp == &smack_known_star || 3622 skp == &smack_known_web) 3623 skp = NULL; 3624 isp->smk_mmap = skp; 3625 3626 dput(dp); 3627 break; 3628 } 3629 3630 if (final == NULL) 3631 isp->smk_inode = ckp; 3632 else 3633 isp->smk_inode = final; 3634 3635 isp->smk_flags |= (SMK_INODE_INSTANT | transflag); 3636 3637 return; 3638 } 3639 3640 /** 3641 * smack_getselfattr - Smack current process attribute 3642 * @attr: which attribute to fetch 3643 * @ctx: buffer to receive the result 3644 * @size: available size in, actual size out 3645 * @flags: unused 3646 * 3647 * Fill the passed user space @ctx with the details of the requested 3648 * attribute. 3649 * 3650 * Returns the number of attributes on success, an error code otherwise. 3651 * There will only ever be one attribute. 3652 */ 3653 static int smack_getselfattr(unsigned int attr, struct lsm_ctx __user *ctx, 3654 size_t *size, u32 flags) 3655 { 3656 int rc; 3657 struct smack_known *skp; 3658 3659 if (attr != LSM_ATTR_CURRENT) 3660 return -EOPNOTSUPP; 3661 3662 skp = smk_of_current(); 3663 rc = lsm_fill_user_ctx(ctx, size, 3664 skp->smk_known, strlen(skp->smk_known) + 1, 3665 LSM_ID_SMACK, 0); 3666 return (!rc ? 1 : rc); 3667 } 3668 3669 /** 3670 * smack_getprocattr - Smack process attribute access 3671 * @p: the object task 3672 * @name: the name of the attribute in /proc/.../attr 3673 * @value: where to put the result 3674 * 3675 * Places a copy of the task Smack into value 3676 * 3677 * Returns the length of the smack label or an error code 3678 */ 3679 static int smack_getprocattr(struct task_struct *p, const char *name, char **value) 3680 { 3681 struct smack_known *skp = smk_of_task_struct_obj(p); 3682 char *cp; 3683 int slen; 3684 3685 if (strcmp(name, "current") != 0) 3686 return -EINVAL; 3687 3688 cp = kstrdup(skp->smk_known, GFP_KERNEL); 3689 if (cp == NULL) 3690 return -ENOMEM; 3691 3692 slen = strlen(cp); 3693 *value = cp; 3694 return slen; 3695 } 3696 3697 /** 3698 * do_setattr - Smack process attribute setting 3699 * @attr: the ID of the attribute 3700 * @value: the value to set 3701 * @size: the size of the value 3702 * 3703 * Sets the Smack value of the task. Only setting self 3704 * is permitted and only with privilege 3705 * 3706 * Returns the length of the smack label or an error code 3707 */ 3708 static int do_setattr(u64 attr, void *value, size_t size) 3709 { 3710 struct task_smack *tsp = smack_cred(current_cred()); 3711 struct cred *new; 3712 struct smack_known *skp; 3713 struct smack_known_list_elem *sklep; 3714 int rc; 3715 3716 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel)) 3717 return -EPERM; 3718 3719 if (value == NULL || size == 0 || size >= SMK_LONGLABEL) 3720 return -EINVAL; 3721 3722 if (attr != LSM_ATTR_CURRENT) 3723 return -EOPNOTSUPP; 3724 3725 skp = smk_import_entry(value, size); 3726 if (IS_ERR(skp)) 3727 return PTR_ERR(skp); 3728 3729 /* 3730 * No process is ever allowed the web ("@") label 3731 * and the star ("*") label. 3732 */ 3733 if (skp == &smack_known_web || skp == &smack_known_star) 3734 return -EINVAL; 3735 3736 if (!smack_privileged(CAP_MAC_ADMIN)) { 3737 rc = -EPERM; 3738 list_for_each_entry(sklep, &tsp->smk_relabel, list) 3739 if (sklep->smk_label == skp) { 3740 rc = 0; 3741 break; 3742 } 3743 if (rc) 3744 return rc; 3745 } 3746 3747 new = prepare_creds(); 3748 if (new == NULL) 3749 return -ENOMEM; 3750 3751 tsp = smack_cred(new); 3752 tsp->smk_task = skp; 3753 /* 3754 * process can change its label only once 3755 */ 3756 smk_destroy_label_list(&tsp->smk_relabel); 3757 3758 commit_creds(new); 3759 return size; 3760 } 3761 3762 /** 3763 * smack_setselfattr - Set a Smack process attribute 3764 * @attr: which attribute to set 3765 * @ctx: buffer containing the data 3766 * @size: size of @ctx 3767 * @flags: unused 3768 * 3769 * Fill the passed user space @ctx with the details of the requested 3770 * attribute. 3771 * 3772 * Returns 0 on success, an error code otherwise. 3773 */ 3774 static int smack_setselfattr(unsigned int attr, struct lsm_ctx *ctx, 3775 size_t size, u32 flags) 3776 { 3777 int rc; 3778 3779 rc = do_setattr(attr, ctx->ctx, ctx->ctx_len); 3780 if (rc > 0) 3781 return 0; 3782 return rc; 3783 } 3784 3785 /** 3786 * smack_setprocattr - Smack process attribute setting 3787 * @name: the name of the attribute in /proc/.../attr 3788 * @value: the value to set 3789 * @size: the size of the value 3790 * 3791 * Sets the Smack value of the task. Only setting self 3792 * is permitted and only with privilege 3793 * 3794 * Returns the length of the smack label or an error code 3795 */ 3796 static int smack_setprocattr(const char *name, void *value, size_t size) 3797 { 3798 int attr = lsm_name_to_attr(name); 3799 3800 if (attr != LSM_ATTR_UNDEF) 3801 return do_setattr(attr, value, size); 3802 return -EINVAL; 3803 } 3804 3805 /** 3806 * smack_unix_stream_connect - Smack access on UDS 3807 * @sock: one sock 3808 * @other: the other sock 3809 * @newsk: unused 3810 * 3811 * Return 0 if a subject with the smack of sock could access 3812 * an object with the smack of other, otherwise an error code 3813 */ 3814 static int smack_unix_stream_connect(struct sock *sock, 3815 struct sock *other, struct sock *newsk) 3816 { 3817 struct smack_known *skp; 3818 struct smack_known *okp; 3819 struct socket_smack *ssp = sock->sk_security; 3820 struct socket_smack *osp = other->sk_security; 3821 struct socket_smack *nsp = newsk->sk_security; 3822 struct smk_audit_info ad; 3823 int rc = 0; 3824 #ifdef CONFIG_AUDIT 3825 struct lsm_network_audit net; 3826 #endif 3827 3828 if (!smack_privileged(CAP_MAC_OVERRIDE)) { 3829 skp = ssp->smk_out; 3830 okp = osp->smk_in; 3831 #ifdef CONFIG_AUDIT 3832 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); 3833 smk_ad_setfield_u_net_sk(&ad, other); 3834 #endif 3835 rc = smk_access(skp, okp, MAY_WRITE, &ad); 3836 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc); 3837 if (rc == 0) { 3838 okp = osp->smk_out; 3839 skp = ssp->smk_in; 3840 rc = smk_access(okp, skp, MAY_WRITE, &ad); 3841 rc = smk_bu_note("UDS connect", okp, skp, 3842 MAY_WRITE, rc); 3843 } 3844 } 3845 3846 /* 3847 * Cross reference the peer labels for SO_PEERSEC. 3848 */ 3849 if (rc == 0) { 3850 nsp->smk_packet = ssp->smk_out; 3851 ssp->smk_packet = osp->smk_out; 3852 } 3853 3854 return rc; 3855 } 3856 3857 /** 3858 * smack_unix_may_send - Smack access on UDS 3859 * @sock: one socket 3860 * @other: the other socket 3861 * 3862 * Return 0 if a subject with the smack of sock could access 3863 * an object with the smack of other, otherwise an error code 3864 */ 3865 static int smack_unix_may_send(struct socket *sock, struct socket *other) 3866 { 3867 struct socket_smack *ssp = sock->sk->sk_security; 3868 struct socket_smack *osp = other->sk->sk_security; 3869 struct smk_audit_info ad; 3870 int rc; 3871 3872 #ifdef CONFIG_AUDIT 3873 struct lsm_network_audit net; 3874 3875 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); 3876 smk_ad_setfield_u_net_sk(&ad, other->sk); 3877 #endif 3878 3879 if (smack_privileged(CAP_MAC_OVERRIDE)) 3880 return 0; 3881 3882 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad); 3883 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc); 3884 return rc; 3885 } 3886 3887 /** 3888 * smack_socket_sendmsg - Smack check based on destination host 3889 * @sock: the socket 3890 * @msg: the message 3891 * @size: the size of the message 3892 * 3893 * Return 0 if the current subject can write to the destination host. 3894 * For IPv4 this is only a question if the destination is a single label host. 3895 * For IPv6 this is a check against the label of the port. 3896 */ 3897 static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg, 3898 int size) 3899 { 3900 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name; 3901 #if IS_ENABLED(CONFIG_IPV6) 3902 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name; 3903 #endif 3904 #ifdef SMACK_IPV6_SECMARK_LABELING 3905 struct socket_smack *ssp = sock->sk->sk_security; 3906 struct smack_known *rsp; 3907 #endif 3908 int rc = 0; 3909 3910 /* 3911 * Perfectly reasonable for this to be NULL 3912 */ 3913 if (sip == NULL) 3914 return 0; 3915 3916 switch (sock->sk->sk_family) { 3917 case AF_INET: 3918 if (msg->msg_namelen < sizeof(struct sockaddr_in) || 3919 sip->sin_family != AF_INET) 3920 return -EINVAL; 3921 rc = smk_ipv4_check(sock->sk, sip); 3922 break; 3923 #if IS_ENABLED(CONFIG_IPV6) 3924 case AF_INET6: 3925 if (msg->msg_namelen < SIN6_LEN_RFC2133 || 3926 sap->sin6_family != AF_INET6) 3927 return -EINVAL; 3928 #ifdef SMACK_IPV6_SECMARK_LABELING 3929 rsp = smack_ipv6host_label(sap); 3930 if (rsp != NULL) 3931 rc = smk_ipv6_check(ssp->smk_out, rsp, sap, 3932 SMK_CONNECTING); 3933 #endif 3934 #ifdef SMACK_IPV6_PORT_LABELING 3935 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING); 3936 #endif 3937 #endif /* IS_ENABLED(CONFIG_IPV6) */ 3938 break; 3939 } 3940 return rc; 3941 } 3942 3943 /** 3944 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack 3945 * @sap: netlabel secattr 3946 * @ssp: socket security information 3947 * 3948 * Returns a pointer to a Smack label entry found on the label list. 3949 */ 3950 static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap, 3951 struct socket_smack *ssp) 3952 { 3953 struct smack_known *skp; 3954 int found = 0; 3955 int acat; 3956 int kcat; 3957 3958 /* 3959 * Netlabel found it in the cache. 3960 */ 3961 if ((sap->flags & NETLBL_SECATTR_CACHE) != 0) 3962 return (struct smack_known *)sap->cache->data; 3963 3964 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) 3965 /* 3966 * Looks like a fallback, which gives us a secid. 3967 */ 3968 return smack_from_secid(sap->attr.secid); 3969 3970 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) { 3971 /* 3972 * Looks like a CIPSO packet. 3973 * If there are flags but no level netlabel isn't 3974 * behaving the way we expect it to. 3975 * 3976 * Look it up in the label table 3977 * Without guidance regarding the smack value 3978 * for the packet fall back on the network 3979 * ambient value. 3980 */ 3981 rcu_read_lock(); 3982 list_for_each_entry_rcu(skp, &smack_known_list, list) { 3983 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl) 3984 continue; 3985 /* 3986 * Compare the catsets. Use the netlbl APIs. 3987 */ 3988 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) { 3989 if ((skp->smk_netlabel.flags & 3990 NETLBL_SECATTR_MLS_CAT) == 0) 3991 found = 1; 3992 break; 3993 } 3994 for (acat = -1, kcat = -1; acat == kcat; ) { 3995 acat = netlbl_catmap_walk(sap->attr.mls.cat, 3996 acat + 1); 3997 kcat = netlbl_catmap_walk( 3998 skp->smk_netlabel.attr.mls.cat, 3999 kcat + 1); 4000 if (acat < 0 || kcat < 0) 4001 break; 4002 } 4003 if (acat == kcat) { 4004 found = 1; 4005 break; 4006 } 4007 } 4008 rcu_read_unlock(); 4009 4010 if (found) 4011 return skp; 4012 4013 if (ssp != NULL && ssp->smk_in == &smack_known_star) 4014 return &smack_known_web; 4015 return &smack_known_star; 4016 } 4017 /* 4018 * Without guidance regarding the smack value 4019 * for the packet fall back on the network 4020 * ambient value. 4021 */ 4022 return smack_net_ambient; 4023 } 4024 4025 #if IS_ENABLED(CONFIG_IPV6) 4026 static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip) 4027 { 4028 u8 nexthdr; 4029 int offset; 4030 int proto = -EINVAL; 4031 struct ipv6hdr _ipv6h; 4032 struct ipv6hdr *ip6; 4033 __be16 frag_off; 4034 struct tcphdr _tcph, *th; 4035 struct udphdr _udph, *uh; 4036 struct dccp_hdr _dccph, *dh; 4037 4038 sip->sin6_port = 0; 4039 4040 offset = skb_network_offset(skb); 4041 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 4042 if (ip6 == NULL) 4043 return -EINVAL; 4044 sip->sin6_addr = ip6->saddr; 4045 4046 nexthdr = ip6->nexthdr; 4047 offset += sizeof(_ipv6h); 4048 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); 4049 if (offset < 0) 4050 return -EINVAL; 4051 4052 proto = nexthdr; 4053 switch (proto) { 4054 case IPPROTO_TCP: 4055 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 4056 if (th != NULL) 4057 sip->sin6_port = th->source; 4058 break; 4059 case IPPROTO_UDP: 4060 case IPPROTO_UDPLITE: 4061 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 4062 if (uh != NULL) 4063 sip->sin6_port = uh->source; 4064 break; 4065 case IPPROTO_DCCP: 4066 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 4067 if (dh != NULL) 4068 sip->sin6_port = dh->dccph_sport; 4069 break; 4070 } 4071 return proto; 4072 } 4073 #endif /* CONFIG_IPV6 */ 4074 4075 /** 4076 * smack_from_skb - Smack data from the secmark in an skb 4077 * @skb: packet 4078 * 4079 * Returns smack_known of the secmark or NULL if that won't work. 4080 */ 4081 #ifdef CONFIG_NETWORK_SECMARK 4082 static struct smack_known *smack_from_skb(struct sk_buff *skb) 4083 { 4084 if (skb == NULL || skb->secmark == 0) 4085 return NULL; 4086 4087 return smack_from_secid(skb->secmark); 4088 } 4089 #else 4090 static inline struct smack_known *smack_from_skb(struct sk_buff *skb) 4091 { 4092 return NULL; 4093 } 4094 #endif 4095 4096 /** 4097 * smack_from_netlbl - Smack data from the IP options in an skb 4098 * @sk: socket data came in on 4099 * @family: address family 4100 * @skb: packet 4101 * 4102 * Find the Smack label in the IP options. If it hasn't been 4103 * added to the netlabel cache, add it here. 4104 * 4105 * Returns smack_known of the IP options or NULL if that won't work. 4106 */ 4107 static struct smack_known *smack_from_netlbl(const struct sock *sk, u16 family, 4108 struct sk_buff *skb) 4109 { 4110 struct netlbl_lsm_secattr secattr; 4111 struct socket_smack *ssp = NULL; 4112 struct smack_known *skp = NULL; 4113 4114 netlbl_secattr_init(&secattr); 4115 4116 if (sk) 4117 ssp = sk->sk_security; 4118 4119 if (netlbl_skbuff_getattr(skb, family, &secattr) == 0) { 4120 skp = smack_from_secattr(&secattr, ssp); 4121 if (secattr.flags & NETLBL_SECATTR_CACHEABLE) 4122 netlbl_cache_add(skb, family, &skp->smk_netlabel); 4123 } 4124 4125 netlbl_secattr_destroy(&secattr); 4126 4127 return skp; 4128 } 4129 4130 /** 4131 * smack_socket_sock_rcv_skb - Smack packet delivery access check 4132 * @sk: socket 4133 * @skb: packet 4134 * 4135 * Returns 0 if the packet should be delivered, an error code otherwise 4136 */ 4137 static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 4138 { 4139 struct socket_smack *ssp = sk->sk_security; 4140 struct smack_known *skp = NULL; 4141 int rc = 0; 4142 struct smk_audit_info ad; 4143 u16 family = sk->sk_family; 4144 #ifdef CONFIG_AUDIT 4145 struct lsm_network_audit net; 4146 #endif 4147 #if IS_ENABLED(CONFIG_IPV6) 4148 struct sockaddr_in6 sadd; 4149 int proto; 4150 4151 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 4152 family = PF_INET; 4153 #endif /* CONFIG_IPV6 */ 4154 4155 switch (family) { 4156 case PF_INET: 4157 /* 4158 * If there is a secmark use it rather than the CIPSO label. 4159 * If there is no secmark fall back to CIPSO. 4160 * The secmark is assumed to reflect policy better. 4161 */ 4162 skp = smack_from_skb(skb); 4163 if (skp == NULL) { 4164 skp = smack_from_netlbl(sk, family, skb); 4165 if (skp == NULL) 4166 skp = smack_net_ambient; 4167 } 4168 4169 #ifdef CONFIG_AUDIT 4170 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); 4171 ad.a.u.net->family = family; 4172 ad.a.u.net->netif = skb->skb_iif; 4173 ipv4_skb_to_auditdata(skb, &ad.a, NULL); 4174 #endif 4175 /* 4176 * Receiving a packet requires that the other end 4177 * be able to write here. Read access is not required. 4178 * This is the simplist possible security model 4179 * for networking. 4180 */ 4181 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad); 4182 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in, 4183 MAY_WRITE, rc); 4184 if (rc != 0) 4185 netlbl_skbuff_err(skb, family, rc, 0); 4186 break; 4187 #if IS_ENABLED(CONFIG_IPV6) 4188 case PF_INET6: 4189 proto = smk_skb_to_addr_ipv6(skb, &sadd); 4190 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE && 4191 proto != IPPROTO_TCP && proto != IPPROTO_DCCP) 4192 break; 4193 #ifdef SMACK_IPV6_SECMARK_LABELING 4194 skp = smack_from_skb(skb); 4195 if (skp == NULL) { 4196 if (smk_ipv6_localhost(&sadd)) 4197 break; 4198 skp = smack_ipv6host_label(&sadd); 4199 if (skp == NULL) 4200 skp = smack_net_ambient; 4201 } 4202 #ifdef CONFIG_AUDIT 4203 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); 4204 ad.a.u.net->family = family; 4205 ad.a.u.net->netif = skb->skb_iif; 4206 ipv6_skb_to_auditdata(skb, &ad.a, NULL); 4207 #endif /* CONFIG_AUDIT */ 4208 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad); 4209 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in, 4210 MAY_WRITE, rc); 4211 #endif /* SMACK_IPV6_SECMARK_LABELING */ 4212 #ifdef SMACK_IPV6_PORT_LABELING 4213 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING); 4214 #endif /* SMACK_IPV6_PORT_LABELING */ 4215 if (rc != 0) 4216 icmpv6_send(skb, ICMPV6_DEST_UNREACH, 4217 ICMPV6_ADM_PROHIBITED, 0); 4218 break; 4219 #endif /* CONFIG_IPV6 */ 4220 } 4221 4222 return rc; 4223 } 4224 4225 /** 4226 * smack_socket_getpeersec_stream - pull in packet label 4227 * @sock: the socket 4228 * @optval: user's destination 4229 * @optlen: size thereof 4230 * @len: max thereof 4231 * 4232 * returns zero on success, an error code otherwise 4233 */ 4234 static int smack_socket_getpeersec_stream(struct socket *sock, 4235 sockptr_t optval, sockptr_t optlen, 4236 unsigned int len) 4237 { 4238 struct socket_smack *ssp; 4239 char *rcp = ""; 4240 u32 slen = 1; 4241 int rc = 0; 4242 4243 ssp = sock->sk->sk_security; 4244 if (ssp->smk_packet != NULL) { 4245 rcp = ssp->smk_packet->smk_known; 4246 slen = strlen(rcp) + 1; 4247 } 4248 if (slen > len) { 4249 rc = -ERANGE; 4250 goto out_len; 4251 } 4252 4253 if (copy_to_sockptr(optval, rcp, slen)) 4254 rc = -EFAULT; 4255 out_len: 4256 if (copy_to_sockptr(optlen, &slen, sizeof(slen))) 4257 rc = -EFAULT; 4258 return rc; 4259 } 4260 4261 4262 /** 4263 * smack_socket_getpeersec_dgram - pull in packet label 4264 * @sock: the peer socket 4265 * @skb: packet data 4266 * @secid: pointer to where to put the secid of the packet 4267 * 4268 * Sets the netlabel socket state on sk from parent 4269 */ 4270 static int smack_socket_getpeersec_dgram(struct socket *sock, 4271 struct sk_buff *skb, u32 *secid) 4272 4273 { 4274 struct socket_smack *ssp = NULL; 4275 struct smack_known *skp; 4276 struct sock *sk = NULL; 4277 int family = PF_UNSPEC; 4278 u32 s = 0; /* 0 is the invalid secid */ 4279 4280 if (skb != NULL) { 4281 if (skb->protocol == htons(ETH_P_IP)) 4282 family = PF_INET; 4283 #if IS_ENABLED(CONFIG_IPV6) 4284 else if (skb->protocol == htons(ETH_P_IPV6)) 4285 family = PF_INET6; 4286 #endif /* CONFIG_IPV6 */ 4287 } 4288 if (family == PF_UNSPEC && sock != NULL) 4289 family = sock->sk->sk_family; 4290 4291 switch (family) { 4292 case PF_UNIX: 4293 ssp = sock->sk->sk_security; 4294 s = ssp->smk_out->smk_secid; 4295 break; 4296 case PF_INET: 4297 skp = smack_from_skb(skb); 4298 if (skp) { 4299 s = skp->smk_secid; 4300 break; 4301 } 4302 /* 4303 * Translate what netlabel gave us. 4304 */ 4305 if (sock != NULL) 4306 sk = sock->sk; 4307 skp = smack_from_netlbl(sk, family, skb); 4308 if (skp != NULL) 4309 s = skp->smk_secid; 4310 break; 4311 case PF_INET6: 4312 #ifdef SMACK_IPV6_SECMARK_LABELING 4313 skp = smack_from_skb(skb); 4314 if (skp) 4315 s = skp->smk_secid; 4316 #endif 4317 break; 4318 } 4319 *secid = s; 4320 if (s == 0) 4321 return -EINVAL; 4322 return 0; 4323 } 4324 4325 /** 4326 * smack_sock_graft - Initialize a newly created socket with an existing sock 4327 * @sk: child sock 4328 * @parent: parent socket 4329 * 4330 * Set the smk_{in,out} state of an existing sock based on the process that 4331 * is creating the new socket. 4332 */ 4333 static void smack_sock_graft(struct sock *sk, struct socket *parent) 4334 { 4335 struct socket_smack *ssp; 4336 struct smack_known *skp = smk_of_current(); 4337 4338 if (sk == NULL || 4339 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)) 4340 return; 4341 4342 ssp = sk->sk_security; 4343 ssp->smk_in = skp; 4344 ssp->smk_out = skp; 4345 /* cssp->smk_packet is already set in smack_inet_csk_clone() */ 4346 } 4347 4348 /** 4349 * smack_inet_conn_request - Smack access check on connect 4350 * @sk: socket involved 4351 * @skb: packet 4352 * @req: unused 4353 * 4354 * Returns 0 if a task with the packet label could write to 4355 * the socket, otherwise an error code 4356 */ 4357 static int smack_inet_conn_request(const struct sock *sk, struct sk_buff *skb, 4358 struct request_sock *req) 4359 { 4360 u16 family = sk->sk_family; 4361 struct smack_known *skp; 4362 struct socket_smack *ssp = sk->sk_security; 4363 struct sockaddr_in addr; 4364 struct iphdr *hdr; 4365 struct smack_known *hskp; 4366 int rc; 4367 struct smk_audit_info ad; 4368 #ifdef CONFIG_AUDIT 4369 struct lsm_network_audit net; 4370 #endif 4371 4372 #if IS_ENABLED(CONFIG_IPV6) 4373 if (family == PF_INET6) { 4374 /* 4375 * Handle mapped IPv4 packets arriving 4376 * via IPv6 sockets. Don't set up netlabel 4377 * processing on IPv6. 4378 */ 4379 if (skb->protocol == htons(ETH_P_IP)) 4380 family = PF_INET; 4381 else 4382 return 0; 4383 } 4384 #endif /* CONFIG_IPV6 */ 4385 4386 /* 4387 * If there is a secmark use it rather than the CIPSO label. 4388 * If there is no secmark fall back to CIPSO. 4389 * The secmark is assumed to reflect policy better. 4390 */ 4391 skp = smack_from_skb(skb); 4392 if (skp == NULL) { 4393 skp = smack_from_netlbl(sk, family, skb); 4394 if (skp == NULL) 4395 skp = &smack_known_huh; 4396 } 4397 4398 #ifdef CONFIG_AUDIT 4399 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); 4400 ad.a.u.net->family = family; 4401 ad.a.u.net->netif = skb->skb_iif; 4402 ipv4_skb_to_auditdata(skb, &ad.a, NULL); 4403 #endif 4404 /* 4405 * Receiving a packet requires that the other end be able to write 4406 * here. Read access is not required. 4407 */ 4408 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad); 4409 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc); 4410 if (rc != 0) 4411 return rc; 4412 4413 /* 4414 * Save the peer's label in the request_sock so we can later setup 4415 * smk_packet in the child socket so that SO_PEERCRED can report it. 4416 */ 4417 req->peer_secid = skp->smk_secid; 4418 4419 /* 4420 * We need to decide if we want to label the incoming connection here 4421 * if we do we only need to label the request_sock and the stack will 4422 * propagate the wire-label to the sock when it is created. 4423 */ 4424 hdr = ip_hdr(skb); 4425 addr.sin_addr.s_addr = hdr->saddr; 4426 rcu_read_lock(); 4427 hskp = smack_ipv4host_label(&addr); 4428 rcu_read_unlock(); 4429 4430 if (hskp == NULL) 4431 rc = netlbl_req_setattr(req, &skp->smk_netlabel); 4432 else 4433 netlbl_req_delattr(req); 4434 4435 return rc; 4436 } 4437 4438 /** 4439 * smack_inet_csk_clone - Copy the connection information to the new socket 4440 * @sk: the new socket 4441 * @req: the connection's request_sock 4442 * 4443 * Transfer the connection's peer label to the newly created socket. 4444 */ 4445 static void smack_inet_csk_clone(struct sock *sk, 4446 const struct request_sock *req) 4447 { 4448 struct socket_smack *ssp = sk->sk_security; 4449 struct smack_known *skp; 4450 4451 if (req->peer_secid != 0) { 4452 skp = smack_from_secid(req->peer_secid); 4453 ssp->smk_packet = skp; 4454 } else 4455 ssp->smk_packet = NULL; 4456 } 4457 4458 /* 4459 * Key management security hooks 4460 * 4461 * Casey has not tested key support very heavily. 4462 * The permission check is most likely too restrictive. 4463 * If you care about keys please have a look. 4464 */ 4465 #ifdef CONFIG_KEYS 4466 4467 /** 4468 * smack_key_alloc - Set the key security blob 4469 * @key: object 4470 * @cred: the credentials to use 4471 * @flags: unused 4472 * 4473 * No allocation required 4474 * 4475 * Returns 0 4476 */ 4477 static int smack_key_alloc(struct key *key, const struct cred *cred, 4478 unsigned long flags) 4479 { 4480 struct smack_known *skp = smk_of_task(smack_cred(cred)); 4481 4482 key->security = skp; 4483 return 0; 4484 } 4485 4486 /** 4487 * smack_key_free - Clear the key security blob 4488 * @key: the object 4489 * 4490 * Clear the blob pointer 4491 */ 4492 static void smack_key_free(struct key *key) 4493 { 4494 key->security = NULL; 4495 } 4496 4497 /** 4498 * smack_key_permission - Smack access on a key 4499 * @key_ref: gets to the object 4500 * @cred: the credentials to use 4501 * @need_perm: requested key permission 4502 * 4503 * Return 0 if the task has read and write to the object, 4504 * an error code otherwise 4505 */ 4506 static int smack_key_permission(key_ref_t key_ref, 4507 const struct cred *cred, 4508 enum key_need_perm need_perm) 4509 { 4510 struct key *keyp; 4511 struct smk_audit_info ad; 4512 struct smack_known *tkp = smk_of_task(smack_cred(cred)); 4513 int request = 0; 4514 int rc; 4515 4516 /* 4517 * Validate requested permissions 4518 */ 4519 switch (need_perm) { 4520 case KEY_NEED_READ: 4521 case KEY_NEED_SEARCH: 4522 case KEY_NEED_VIEW: 4523 request |= MAY_READ; 4524 break; 4525 case KEY_NEED_WRITE: 4526 case KEY_NEED_LINK: 4527 case KEY_NEED_SETATTR: 4528 request |= MAY_WRITE; 4529 break; 4530 case KEY_NEED_UNSPECIFIED: 4531 case KEY_NEED_UNLINK: 4532 case KEY_SYSADMIN_OVERRIDE: 4533 case KEY_AUTHTOKEN_OVERRIDE: 4534 case KEY_DEFER_PERM_CHECK: 4535 return 0; 4536 default: 4537 return -EINVAL; 4538 } 4539 4540 keyp = key_ref_to_ptr(key_ref); 4541 if (keyp == NULL) 4542 return -EINVAL; 4543 /* 4544 * If the key hasn't been initialized give it access so that 4545 * it may do so. 4546 */ 4547 if (keyp->security == NULL) 4548 return 0; 4549 /* 4550 * This should not occur 4551 */ 4552 if (tkp == NULL) 4553 return -EACCES; 4554 4555 if (smack_privileged(CAP_MAC_OVERRIDE)) 4556 return 0; 4557 4558 #ifdef CONFIG_AUDIT 4559 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY); 4560 ad.a.u.key_struct.key = keyp->serial; 4561 ad.a.u.key_struct.key_desc = keyp->description; 4562 #endif 4563 rc = smk_access(tkp, keyp->security, request, &ad); 4564 rc = smk_bu_note("key access", tkp, keyp->security, request, rc); 4565 return rc; 4566 } 4567 4568 /* 4569 * smack_key_getsecurity - Smack label tagging the key 4570 * @key points to the key to be queried 4571 * @_buffer points to a pointer that should be set to point to the 4572 * resulting string (if no label or an error occurs). 4573 * Return the length of the string (including terminating NUL) or -ve if 4574 * an error. 4575 * May also return 0 (and a NULL buffer pointer) if there is no label. 4576 */ 4577 static int smack_key_getsecurity(struct key *key, char **_buffer) 4578 { 4579 struct smack_known *skp = key->security; 4580 size_t length; 4581 char *copy; 4582 4583 if (key->security == NULL) { 4584 *_buffer = NULL; 4585 return 0; 4586 } 4587 4588 copy = kstrdup(skp->smk_known, GFP_KERNEL); 4589 if (copy == NULL) 4590 return -ENOMEM; 4591 length = strlen(copy) + 1; 4592 4593 *_buffer = copy; 4594 return length; 4595 } 4596 4597 4598 #ifdef CONFIG_KEY_NOTIFICATIONS 4599 /** 4600 * smack_watch_key - Smack access to watch a key for notifications. 4601 * @key: The key to be watched 4602 * 4603 * Return 0 if the @watch->cred has permission to read from the key object and 4604 * an error otherwise. 4605 */ 4606 static int smack_watch_key(struct key *key) 4607 { 4608 struct smk_audit_info ad; 4609 struct smack_known *tkp = smk_of_current(); 4610 int rc; 4611 4612 if (key == NULL) 4613 return -EINVAL; 4614 /* 4615 * If the key hasn't been initialized give it access so that 4616 * it may do so. 4617 */ 4618 if (key->security == NULL) 4619 return 0; 4620 /* 4621 * This should not occur 4622 */ 4623 if (tkp == NULL) 4624 return -EACCES; 4625 4626 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred())) 4627 return 0; 4628 4629 #ifdef CONFIG_AUDIT 4630 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY); 4631 ad.a.u.key_struct.key = key->serial; 4632 ad.a.u.key_struct.key_desc = key->description; 4633 #endif 4634 rc = smk_access(tkp, key->security, MAY_READ, &ad); 4635 rc = smk_bu_note("key watch", tkp, key->security, MAY_READ, rc); 4636 return rc; 4637 } 4638 #endif /* CONFIG_KEY_NOTIFICATIONS */ 4639 #endif /* CONFIG_KEYS */ 4640 4641 #ifdef CONFIG_WATCH_QUEUE 4642 /** 4643 * smack_post_notification - Smack access to post a notification to a queue 4644 * @w_cred: The credentials of the watcher. 4645 * @cred: The credentials of the event source (may be NULL). 4646 * @n: The notification message to be posted. 4647 */ 4648 static int smack_post_notification(const struct cred *w_cred, 4649 const struct cred *cred, 4650 struct watch_notification *n) 4651 { 4652 struct smk_audit_info ad; 4653 struct smack_known *subj, *obj; 4654 int rc; 4655 4656 /* Always let maintenance notifications through. */ 4657 if (n->type == WATCH_TYPE_META) 4658 return 0; 4659 4660 if (!cred) 4661 return 0; 4662 subj = smk_of_task(smack_cred(cred)); 4663 obj = smk_of_task(smack_cred(w_cred)); 4664 4665 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NOTIFICATION); 4666 rc = smk_access(subj, obj, MAY_WRITE, &ad); 4667 rc = smk_bu_note("notification", subj, obj, MAY_WRITE, rc); 4668 return rc; 4669 } 4670 #endif /* CONFIG_WATCH_QUEUE */ 4671 4672 /* 4673 * Smack Audit hooks 4674 * 4675 * Audit requires a unique representation of each Smack specific 4676 * rule. This unique representation is used to distinguish the 4677 * object to be audited from remaining kernel objects and also 4678 * works as a glue between the audit hooks. 4679 * 4680 * Since repository entries are added but never deleted, we'll use 4681 * the smack_known label address related to the given audit rule as 4682 * the needed unique representation. This also better fits the smack 4683 * model where nearly everything is a label. 4684 */ 4685 #ifdef CONFIG_AUDIT 4686 4687 /** 4688 * smack_audit_rule_init - Initialize a smack audit rule 4689 * @field: audit rule fields given from user-space (audit.h) 4690 * @op: required testing operator (=, !=, >, <, ...) 4691 * @rulestr: smack label to be audited 4692 * @vrule: pointer to save our own audit rule representation 4693 * 4694 * Prepare to audit cases where (@field @op @rulestr) is true. 4695 * The label to be audited is created if necessay. 4696 */ 4697 static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) 4698 { 4699 struct smack_known *skp; 4700 char **rule = (char **)vrule; 4701 *rule = NULL; 4702 4703 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER) 4704 return -EINVAL; 4705 4706 if (op != Audit_equal && op != Audit_not_equal) 4707 return -EINVAL; 4708 4709 skp = smk_import_entry(rulestr, 0); 4710 if (IS_ERR(skp)) 4711 return PTR_ERR(skp); 4712 4713 *rule = skp->smk_known; 4714 4715 return 0; 4716 } 4717 4718 /** 4719 * smack_audit_rule_known - Distinguish Smack audit rules 4720 * @krule: rule of interest, in Audit kernel representation format 4721 * 4722 * This is used to filter Smack rules from remaining Audit ones. 4723 * If it's proved that this rule belongs to us, the 4724 * audit_rule_match hook will be called to do the final judgement. 4725 */ 4726 static int smack_audit_rule_known(struct audit_krule *krule) 4727 { 4728 struct audit_field *f; 4729 int i; 4730 4731 for (i = 0; i < krule->field_count; i++) { 4732 f = &krule->fields[i]; 4733 4734 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER) 4735 return 1; 4736 } 4737 4738 return 0; 4739 } 4740 4741 /** 4742 * smack_audit_rule_match - Audit given object ? 4743 * @secid: security id for identifying the object to test 4744 * @field: audit rule flags given from user-space 4745 * @op: required testing operator 4746 * @vrule: smack internal rule presentation 4747 * 4748 * The core Audit hook. It's used to take the decision of 4749 * whether to audit or not to audit a given object. 4750 */ 4751 static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule) 4752 { 4753 struct smack_known *skp; 4754 char *rule = vrule; 4755 4756 if (unlikely(!rule)) { 4757 WARN_ONCE(1, "Smack: missing rule\n"); 4758 return -ENOENT; 4759 } 4760 4761 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER) 4762 return 0; 4763 4764 skp = smack_from_secid(secid); 4765 4766 /* 4767 * No need to do string comparisons. If a match occurs, 4768 * both pointers will point to the same smack_known 4769 * label. 4770 */ 4771 if (op == Audit_equal) 4772 return (rule == skp->smk_known); 4773 if (op == Audit_not_equal) 4774 return (rule != skp->smk_known); 4775 4776 return 0; 4777 } 4778 4779 /* 4780 * There is no need for a smack_audit_rule_free hook. 4781 * No memory was allocated. 4782 */ 4783 4784 #endif /* CONFIG_AUDIT */ 4785 4786 /** 4787 * smack_ismaclabel - check if xattr @name references a smack MAC label 4788 * @name: Full xattr name to check. 4789 */ 4790 static int smack_ismaclabel(const char *name) 4791 { 4792 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0); 4793 } 4794 4795 4796 /** 4797 * smack_secid_to_secctx - return the smack label for a secid 4798 * @secid: incoming integer 4799 * @secdata: destination 4800 * @seclen: how long it is 4801 * 4802 * Exists for networking code. 4803 */ 4804 static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) 4805 { 4806 struct smack_known *skp = smack_from_secid(secid); 4807 4808 if (secdata) 4809 *secdata = skp->smk_known; 4810 *seclen = strlen(skp->smk_known); 4811 return 0; 4812 } 4813 4814 /** 4815 * smack_secctx_to_secid - return the secid for a smack label 4816 * @secdata: smack label 4817 * @seclen: how long result is 4818 * @secid: outgoing integer 4819 * 4820 * Exists for audit and networking code. 4821 */ 4822 static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) 4823 { 4824 struct smack_known *skp = smk_find_entry(secdata); 4825 4826 if (skp) 4827 *secid = skp->smk_secid; 4828 else 4829 *secid = 0; 4830 return 0; 4831 } 4832 4833 /* 4834 * There used to be a smack_release_secctx hook 4835 * that did nothing back when hooks were in a vector. 4836 * Now that there's a list such a hook adds cost. 4837 */ 4838 4839 static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) 4840 { 4841 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, 4842 ctxlen, 0); 4843 } 4844 4845 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) 4846 { 4847 return __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, XATTR_NAME_SMACK, 4848 ctx, ctxlen, 0); 4849 } 4850 4851 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) 4852 { 4853 struct smack_known *skp = smk_of_inode(inode); 4854 4855 *ctx = skp->smk_known; 4856 *ctxlen = strlen(skp->smk_known); 4857 return 0; 4858 } 4859 4860 static int smack_inode_copy_up(struct dentry *dentry, struct cred **new) 4861 { 4862 4863 struct task_smack *tsp; 4864 struct smack_known *skp; 4865 struct inode_smack *isp; 4866 struct cred *new_creds = *new; 4867 4868 if (new_creds == NULL) { 4869 new_creds = prepare_creds(); 4870 if (new_creds == NULL) 4871 return -ENOMEM; 4872 } 4873 4874 tsp = smack_cred(new_creds); 4875 4876 /* 4877 * Get label from overlay inode and set it in create_sid 4878 */ 4879 isp = smack_inode(d_inode(dentry)); 4880 skp = isp->smk_inode; 4881 tsp->smk_task = skp; 4882 *new = new_creds; 4883 return 0; 4884 } 4885 4886 static int smack_inode_copy_up_xattr(const char *name) 4887 { 4888 /* 4889 * Return 1 if this is the smack access Smack attribute. 4890 */ 4891 if (strcmp(name, XATTR_NAME_SMACK) == 0) 4892 return 1; 4893 4894 return -EOPNOTSUPP; 4895 } 4896 4897 static int smack_dentry_create_files_as(struct dentry *dentry, int mode, 4898 struct qstr *name, 4899 const struct cred *old, 4900 struct cred *new) 4901 { 4902 struct task_smack *otsp = smack_cred(old); 4903 struct task_smack *ntsp = smack_cred(new); 4904 struct inode_smack *isp; 4905 int may; 4906 4907 /* 4908 * Use the process credential unless all of 4909 * the transmuting criteria are met 4910 */ 4911 ntsp->smk_task = otsp->smk_task; 4912 4913 /* 4914 * the attribute of the containing directory 4915 */ 4916 isp = smack_inode(d_inode(dentry->d_parent)); 4917 4918 if (isp->smk_flags & SMK_INODE_TRANSMUTE) { 4919 rcu_read_lock(); 4920 may = smk_access_entry(otsp->smk_task->smk_known, 4921 isp->smk_inode->smk_known, 4922 &otsp->smk_task->smk_rules); 4923 rcu_read_unlock(); 4924 4925 /* 4926 * If the directory is transmuting and the rule 4927 * providing access is transmuting use the containing 4928 * directory label instead of the process label. 4929 */ 4930 if (may > 0 && (may & MAY_TRANSMUTE)) { 4931 ntsp->smk_task = isp->smk_inode; 4932 ntsp->smk_transmuted = ntsp->smk_task; 4933 } 4934 } 4935 return 0; 4936 } 4937 4938 #ifdef CONFIG_IO_URING 4939 /** 4940 * smack_uring_override_creds - Is io_uring cred override allowed? 4941 * @new: the target creds 4942 * 4943 * Check to see if the current task is allowed to override it's credentials 4944 * to service an io_uring operation. 4945 */ 4946 static int smack_uring_override_creds(const struct cred *new) 4947 { 4948 struct task_smack *tsp = smack_cred(current_cred()); 4949 struct task_smack *nsp = smack_cred(new); 4950 4951 /* 4952 * Allow the degenerate case where the new Smack value is 4953 * the same as the current Smack value. 4954 */ 4955 if (tsp->smk_task == nsp->smk_task) 4956 return 0; 4957 4958 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred())) 4959 return 0; 4960 4961 return -EPERM; 4962 } 4963 4964 /** 4965 * smack_uring_sqpoll - check if a io_uring polling thread can be created 4966 * 4967 * Check to see if the current task is allowed to create a new io_uring 4968 * kernel polling thread. 4969 */ 4970 static int smack_uring_sqpoll(void) 4971 { 4972 if (smack_privileged_cred(CAP_MAC_ADMIN, current_cred())) 4973 return 0; 4974 4975 return -EPERM; 4976 } 4977 4978 /** 4979 * smack_uring_cmd - check on file operations for io_uring 4980 * @ioucmd: the command in question 4981 * 4982 * Make a best guess about whether a io_uring "command" should 4983 * be allowed. Use the same logic used for determining if the 4984 * file could be opened for read in the absence of better criteria. 4985 */ 4986 static int smack_uring_cmd(struct io_uring_cmd *ioucmd) 4987 { 4988 struct file *file = ioucmd->file; 4989 struct smk_audit_info ad; 4990 struct task_smack *tsp; 4991 struct inode *inode; 4992 int rc; 4993 4994 if (!file) 4995 return -EINVAL; 4996 4997 tsp = smack_cred(file->f_cred); 4998 inode = file_inode(file); 4999 5000 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); 5001 smk_ad_setfield_u_fs_path(&ad, file->f_path); 5002 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad); 5003 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc); 5004 5005 return rc; 5006 } 5007 5008 #endif /* CONFIG_IO_URING */ 5009 5010 struct lsm_blob_sizes smack_blob_sizes __ro_after_init = { 5011 .lbs_cred = sizeof(struct task_smack), 5012 .lbs_file = sizeof(struct smack_known *), 5013 .lbs_inode = sizeof(struct inode_smack), 5014 .lbs_ipc = sizeof(struct smack_known *), 5015 .lbs_msg_msg = sizeof(struct smack_known *), 5016 .lbs_superblock = sizeof(struct superblock_smack), 5017 .lbs_xattr_count = SMACK_INODE_INIT_XATTRS, 5018 }; 5019 5020 static const struct lsm_id smack_lsmid = { 5021 .name = "smack", 5022 .id = LSM_ID_SMACK, 5023 }; 5024 5025 static struct security_hook_list smack_hooks[] __ro_after_init = { 5026 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check), 5027 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme), 5028 LSM_HOOK_INIT(syslog, smack_syslog), 5029 5030 LSM_HOOK_INIT(fs_context_submount, smack_fs_context_submount), 5031 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup), 5032 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param), 5033 5034 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security), 5035 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts), 5036 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts), 5037 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs), 5038 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts), 5039 5040 LSM_HOOK_INIT(bprm_creds_for_exec, smack_bprm_creds_for_exec), 5041 5042 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security), 5043 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security), 5044 LSM_HOOK_INIT(inode_link, smack_inode_link), 5045 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink), 5046 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir), 5047 LSM_HOOK_INIT(inode_rename, smack_inode_rename), 5048 LSM_HOOK_INIT(inode_permission, smack_inode_permission), 5049 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr), 5050 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr), 5051 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr), 5052 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr), 5053 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr), 5054 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr), 5055 LSM_HOOK_INIT(inode_set_acl, smack_inode_set_acl), 5056 LSM_HOOK_INIT(inode_get_acl, smack_inode_get_acl), 5057 LSM_HOOK_INIT(inode_remove_acl, smack_inode_remove_acl), 5058 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity), 5059 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity), 5060 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity), 5061 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid), 5062 5063 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security), 5064 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl), 5065 LSM_HOOK_INIT(file_ioctl_compat, smack_file_ioctl), 5066 LSM_HOOK_INIT(file_lock, smack_file_lock), 5067 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl), 5068 LSM_HOOK_INIT(mmap_file, smack_mmap_file), 5069 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr), 5070 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner), 5071 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask), 5072 LSM_HOOK_INIT(file_receive, smack_file_receive), 5073 5074 LSM_HOOK_INIT(file_open, smack_file_open), 5075 5076 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank), 5077 LSM_HOOK_INIT(cred_free, smack_cred_free), 5078 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare), 5079 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer), 5080 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid), 5081 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as), 5082 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as), 5083 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid), 5084 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid), 5085 LSM_HOOK_INIT(task_getsid, smack_task_getsid), 5086 LSM_HOOK_INIT(current_getsecid_subj, smack_current_getsecid_subj), 5087 LSM_HOOK_INIT(task_getsecid_obj, smack_task_getsecid_obj), 5088 LSM_HOOK_INIT(task_setnice, smack_task_setnice), 5089 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio), 5090 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio), 5091 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler), 5092 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler), 5093 LSM_HOOK_INIT(task_movememory, smack_task_movememory), 5094 LSM_HOOK_INIT(task_kill, smack_task_kill), 5095 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode), 5096 5097 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission), 5098 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid), 5099 5100 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security), 5101 5102 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security), 5103 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate), 5104 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl), 5105 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd), 5106 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv), 5107 5108 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security), 5109 LSM_HOOK_INIT(shm_associate, smack_shm_associate), 5110 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl), 5111 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat), 5112 5113 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security), 5114 LSM_HOOK_INIT(sem_associate, smack_sem_associate), 5115 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl), 5116 LSM_HOOK_INIT(sem_semop, smack_sem_semop), 5117 5118 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate), 5119 5120 LSM_HOOK_INIT(getselfattr, smack_getselfattr), 5121 LSM_HOOK_INIT(setselfattr, smack_setselfattr), 5122 LSM_HOOK_INIT(getprocattr, smack_getprocattr), 5123 LSM_HOOK_INIT(setprocattr, smack_setprocattr), 5124 5125 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect), 5126 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send), 5127 5128 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create), 5129 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair), 5130 #ifdef SMACK_IPV6_PORT_LABELING 5131 LSM_HOOK_INIT(socket_bind, smack_socket_bind), 5132 #endif 5133 LSM_HOOK_INIT(socket_connect, smack_socket_connect), 5134 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg), 5135 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb), 5136 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream), 5137 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram), 5138 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security), 5139 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security), 5140 LSM_HOOK_INIT(sk_clone_security, smack_sk_clone_security), 5141 LSM_HOOK_INIT(sock_graft, smack_sock_graft), 5142 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request), 5143 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone), 5144 5145 /* key management security hooks */ 5146 #ifdef CONFIG_KEYS 5147 LSM_HOOK_INIT(key_alloc, smack_key_alloc), 5148 LSM_HOOK_INIT(key_free, smack_key_free), 5149 LSM_HOOK_INIT(key_permission, smack_key_permission), 5150 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity), 5151 #ifdef CONFIG_KEY_NOTIFICATIONS 5152 LSM_HOOK_INIT(watch_key, smack_watch_key), 5153 #endif 5154 #endif /* CONFIG_KEYS */ 5155 5156 #ifdef CONFIG_WATCH_QUEUE 5157 LSM_HOOK_INIT(post_notification, smack_post_notification), 5158 #endif 5159 5160 /* Audit hooks */ 5161 #ifdef CONFIG_AUDIT 5162 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init), 5163 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known), 5164 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match), 5165 #endif /* CONFIG_AUDIT */ 5166 5167 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel), 5168 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx), 5169 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid), 5170 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx), 5171 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx), 5172 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx), 5173 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up), 5174 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr), 5175 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as), 5176 #ifdef CONFIG_IO_URING 5177 LSM_HOOK_INIT(uring_override_creds, smack_uring_override_creds), 5178 LSM_HOOK_INIT(uring_sqpoll, smack_uring_sqpoll), 5179 LSM_HOOK_INIT(uring_cmd, smack_uring_cmd), 5180 #endif 5181 }; 5182 5183 5184 static __init void init_smack_known_list(void) 5185 { 5186 /* 5187 * Initialize rule list locks 5188 */ 5189 mutex_init(&smack_known_huh.smk_rules_lock); 5190 mutex_init(&smack_known_hat.smk_rules_lock); 5191 mutex_init(&smack_known_floor.smk_rules_lock); 5192 mutex_init(&smack_known_star.smk_rules_lock); 5193 mutex_init(&smack_known_web.smk_rules_lock); 5194 /* 5195 * Initialize rule lists 5196 */ 5197 INIT_LIST_HEAD(&smack_known_huh.smk_rules); 5198 INIT_LIST_HEAD(&smack_known_hat.smk_rules); 5199 INIT_LIST_HEAD(&smack_known_star.smk_rules); 5200 INIT_LIST_HEAD(&smack_known_floor.smk_rules); 5201 INIT_LIST_HEAD(&smack_known_web.smk_rules); 5202 /* 5203 * Create the known labels list 5204 */ 5205 smk_insert_entry(&smack_known_huh); 5206 smk_insert_entry(&smack_known_hat); 5207 smk_insert_entry(&smack_known_star); 5208 smk_insert_entry(&smack_known_floor); 5209 smk_insert_entry(&smack_known_web); 5210 } 5211 5212 /** 5213 * smack_init - initialize the smack system 5214 * 5215 * Returns 0 on success, -ENOMEM is there's no memory 5216 */ 5217 static __init int smack_init(void) 5218 { 5219 struct cred *cred = (struct cred *) current->cred; 5220 struct task_smack *tsp; 5221 5222 smack_rule_cache = KMEM_CACHE(smack_rule, 0); 5223 if (!smack_rule_cache) 5224 return -ENOMEM; 5225 5226 /* 5227 * Set the security state for the initial task. 5228 */ 5229 tsp = smack_cred(cred); 5230 init_task_smack(tsp, &smack_known_floor, &smack_known_floor); 5231 5232 /* 5233 * Register with LSM 5234 */ 5235 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), &smack_lsmid); 5236 smack_enabled = 1; 5237 5238 pr_info("Smack: Initializing.\n"); 5239 #ifdef CONFIG_SECURITY_SMACK_NETFILTER 5240 pr_info("Smack: Netfilter enabled.\n"); 5241 #endif 5242 #ifdef SMACK_IPV6_PORT_LABELING 5243 pr_info("Smack: IPv6 port labeling enabled.\n"); 5244 #endif 5245 #ifdef SMACK_IPV6_SECMARK_LABELING 5246 pr_info("Smack: IPv6 Netfilter enabled.\n"); 5247 #endif 5248 5249 /* initialize the smack_known_list */ 5250 init_smack_known_list(); 5251 5252 return 0; 5253 } 5254 5255 /* 5256 * Smack requires early initialization in order to label 5257 * all processes and objects when they are created. 5258 */ 5259 DEFINE_LSM(smack) = { 5260 .name = "smack", 5261 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, 5262 .blobs = &smack_blob_sizes, 5263 .init = smack_init, 5264 }; 5265