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