1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * AppArmor security module 4 * 5 * This file contains AppArmor mediation of files 6 * 7 * Copyright (C) 1998-2008 Novell/SUSE 8 * Copyright 2009-2017 Canonical Ltd. 9 */ 10 11 #include <linux/fs.h> 12 #include <linux/mount.h> 13 #include <linux/namei.h> 14 #include <uapi/linux/mount.h> 15 16 #include "include/apparmor.h" 17 #include "include/audit.h" 18 #include "include/cred.h" 19 #include "include/domain.h" 20 #include "include/file.h" 21 #include "include/match.h" 22 #include "include/mount.h" 23 #include "include/path.h" 24 #include "include/policy.h" 25 26 27 static void audit_mnt_flags(struct audit_buffer *ab, unsigned long flags) 28 { 29 if (flags & MS_RDONLY) 30 audit_log_format(ab, "ro"); 31 else 32 audit_log_format(ab, "rw"); 33 if (flags & MS_NOSUID) 34 audit_log_format(ab, ", nosuid"); 35 if (flags & MS_NODEV) 36 audit_log_format(ab, ", nodev"); 37 if (flags & MS_NOEXEC) 38 audit_log_format(ab, ", noexec"); 39 if (flags & MS_SYNCHRONOUS) 40 audit_log_format(ab, ", sync"); 41 if (flags & MS_REMOUNT) 42 audit_log_format(ab, ", remount"); 43 if (flags & MS_MANDLOCK) 44 audit_log_format(ab, ", mand"); 45 if (flags & MS_DIRSYNC) 46 audit_log_format(ab, ", dirsync"); 47 if (flags & MS_NOATIME) 48 audit_log_format(ab, ", noatime"); 49 if (flags & MS_NODIRATIME) 50 audit_log_format(ab, ", nodiratime"); 51 if (flags & MS_BIND) 52 audit_log_format(ab, flags & MS_REC ? ", rbind" : ", bind"); 53 if (flags & MS_MOVE) 54 audit_log_format(ab, ", move"); 55 if (flags & MS_SILENT) 56 audit_log_format(ab, ", silent"); 57 if (flags & MS_POSIXACL) 58 audit_log_format(ab, ", acl"); 59 if (flags & MS_UNBINDABLE) 60 audit_log_format(ab, flags & MS_REC ? ", runbindable" : 61 ", unbindable"); 62 if (flags & MS_PRIVATE) 63 audit_log_format(ab, flags & MS_REC ? ", rprivate" : 64 ", private"); 65 if (flags & MS_SLAVE) 66 audit_log_format(ab, flags & MS_REC ? ", rslave" : 67 ", slave"); 68 if (flags & MS_SHARED) 69 audit_log_format(ab, flags & MS_REC ? ", rshared" : 70 ", shared"); 71 if (flags & MS_RELATIME) 72 audit_log_format(ab, ", relatime"); 73 if (flags & MS_I_VERSION) 74 audit_log_format(ab, ", iversion"); 75 if (flags & MS_STRICTATIME) 76 audit_log_format(ab, ", strictatime"); 77 if (flags & MS_NOUSER) 78 audit_log_format(ab, ", nouser"); 79 } 80 81 /** 82 * audit_cb - call back for mount specific audit fields 83 * @ab: audit_buffer (NOT NULL) 84 * @va: audit struct to audit values of (NOT NULL) 85 */ 86 static void audit_cb(struct audit_buffer *ab, void *va) 87 { 88 struct common_audit_data *sa = va; 89 90 if (aad(sa)->mnt.type) { 91 audit_log_format(ab, " fstype="); 92 audit_log_untrustedstring(ab, aad(sa)->mnt.type); 93 } 94 if (aad(sa)->mnt.src_name) { 95 audit_log_format(ab, " srcname="); 96 audit_log_untrustedstring(ab, aad(sa)->mnt.src_name); 97 } 98 if (aad(sa)->mnt.trans) { 99 audit_log_format(ab, " trans="); 100 audit_log_untrustedstring(ab, aad(sa)->mnt.trans); 101 } 102 if (aad(sa)->mnt.flags) { 103 audit_log_format(ab, " flags=\""); 104 audit_mnt_flags(ab, aad(sa)->mnt.flags); 105 audit_log_format(ab, "\""); 106 } 107 if (aad(sa)->mnt.data) { 108 audit_log_format(ab, " options="); 109 audit_log_untrustedstring(ab, aad(sa)->mnt.data); 110 } 111 } 112 113 /** 114 * audit_mount - handle the auditing of mount operations 115 * @profile: the profile being enforced (NOT NULL) 116 * @op: operation being mediated (NOT NULL) 117 * @name: name of object being mediated (MAYBE NULL) 118 * @src_name: src_name of object being mediated (MAYBE_NULL) 119 * @type: type of filesystem (MAYBE_NULL) 120 * @trans: name of trans (MAYBE NULL) 121 * @flags: filesystem independent mount flags 122 * @data: filesystem mount flags 123 * @request: permissions requested 124 * @perms: the permissions computed for the request (NOT NULL) 125 * @info: extra information message (MAYBE NULL) 126 * @error: 0 if operation allowed else failure error code 127 * 128 * Returns: %0 or error on failure 129 */ 130 static int audit_mount(struct aa_profile *profile, const char *op, 131 const char *name, const char *src_name, 132 const char *type, const char *trans, 133 unsigned long flags, const void *data, u32 request, 134 struct aa_perms *perms, const char *info, int error) 135 { 136 int audit_type = AUDIT_APPARMOR_AUTO; 137 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, op); 138 139 if (likely(!error)) { 140 u32 mask = perms->audit; 141 142 if (unlikely(AUDIT_MODE(profile) == AUDIT_ALL)) 143 mask = 0xffff; 144 145 /* mask off perms that are not being force audited */ 146 request &= mask; 147 148 if (likely(!request)) 149 return 0; 150 audit_type = AUDIT_APPARMOR_AUDIT; 151 } else { 152 /* only report permissions that were denied */ 153 request = request & ~perms->allow; 154 155 if (request & perms->kill) 156 audit_type = AUDIT_APPARMOR_KILL; 157 158 /* quiet known rejects, assumes quiet and kill do not overlap */ 159 if ((request & perms->quiet) && 160 AUDIT_MODE(profile) != AUDIT_NOQUIET && 161 AUDIT_MODE(profile) != AUDIT_ALL) 162 request &= ~perms->quiet; 163 164 if (!request) 165 return error; 166 } 167 168 aad(&sa)->name = name; 169 aad(&sa)->mnt.src_name = src_name; 170 aad(&sa)->mnt.type = type; 171 aad(&sa)->mnt.trans = trans; 172 aad(&sa)->mnt.flags = flags; 173 if (data && (perms->audit & AA_AUDIT_DATA)) 174 aad(&sa)->mnt.data = data; 175 aad(&sa)->info = info; 176 aad(&sa)->error = error; 177 178 return aa_audit(audit_type, profile, &sa, audit_cb); 179 } 180 181 /** 182 * match_mnt_flags - Do an ordered match on mount flags 183 * @dfa: dfa to match against 184 * @state: state to start in 185 * @flags: mount flags to match against 186 * 187 * Mount flags are encoded as an ordered match. This is done instead of 188 * checking against a simple bitmask, to allow for logical operations 189 * on the flags. 190 * 191 * Returns: next state after flags match 192 */ 193 static unsigned int match_mnt_flags(struct aa_dfa *dfa, unsigned int state, 194 unsigned long flags) 195 { 196 unsigned int i; 197 198 for (i = 0; i <= 31 ; ++i) { 199 if ((1 << i) & flags) 200 state = aa_dfa_next(dfa, state, i + 1); 201 } 202 203 return state; 204 } 205 206 static const char * const mnt_info_table[] = { 207 "match succeeded", 208 "failed mntpnt match", 209 "failed srcname match", 210 "failed type match", 211 "failed flags match", 212 "failed data match", 213 "failed perms check" 214 }; 215 216 /* 217 * Returns 0 on success else element that match failed in, this is the 218 * index into the mnt_info_table above 219 */ 220 static int do_match_mnt(struct aa_policydb *policy, unsigned int start, 221 const char *mntpnt, const char *devname, 222 const char *type, unsigned long flags, 223 void *data, bool binary, struct aa_perms *perms) 224 { 225 unsigned int state; 226 227 AA_BUG(!policy); 228 AA_BUG(!policy->dfa); 229 AA_BUG(!policy->perms); 230 AA_BUG(!perms); 231 232 state = aa_dfa_match(policy->dfa, start, mntpnt); 233 state = aa_dfa_null_transition(policy->dfa, state); 234 if (!state) 235 return 1; 236 237 if (devname) 238 state = aa_dfa_match(policy->dfa, state, devname); 239 state = aa_dfa_null_transition(policy->dfa, state); 240 if (!state) 241 return 2; 242 243 if (type) 244 state = aa_dfa_match(policy->dfa, state, type); 245 state = aa_dfa_null_transition(policy->dfa, state); 246 if (!state) 247 return 3; 248 249 state = match_mnt_flags(policy->dfa, state, flags); 250 if (!state) 251 return 4; 252 *perms = *aa_lookup_perms(policy, state); 253 if (perms->allow & AA_MAY_MOUNT) 254 return 0; 255 256 /* only match data if not binary and the DFA flags data is expected */ 257 if (data && !binary && (perms->allow & AA_MNT_CONT_MATCH)) { 258 state = aa_dfa_null_transition(policy->dfa, state); 259 if (!state) 260 return 4; 261 262 state = aa_dfa_match(policy->dfa, state, data); 263 if (!state) 264 return 5; 265 *perms = *aa_lookup_perms(policy, state); 266 if (perms->allow & AA_MAY_MOUNT) 267 return 0; 268 } 269 270 /* failed at perms check, don't confuse with flags match */ 271 return 6; 272 } 273 274 275 static int path_flags(struct aa_profile *profile, const struct path *path) 276 { 277 AA_BUG(!profile); 278 AA_BUG(!path); 279 280 return profile->path_flags | 281 (S_ISDIR(path->dentry->d_inode->i_mode) ? PATH_IS_DIR : 0); 282 } 283 284 /** 285 * match_mnt_path_str - handle path matching for mount 286 * @profile: the confining profile 287 * @mntpath: for the mntpnt (NOT NULL) 288 * @buffer: buffer to be used to lookup mntpath 289 * @devname: string for the devname/src_name (MAY BE NULL OR ERRPTR) 290 * @type: string for the dev type (MAYBE NULL) 291 * @flags: mount flags to match 292 * @data: fs mount data (MAYBE NULL) 293 * @binary: whether @data is binary 294 * @devinfo: error str if (IS_ERR(@devname)) 295 * 296 * Returns: 0 on success else error 297 */ 298 static int match_mnt_path_str(struct aa_profile *profile, 299 const struct path *mntpath, char *buffer, 300 const char *devname, const char *type, 301 unsigned long flags, void *data, bool binary, 302 const char *devinfo) 303 { 304 struct aa_perms perms = { }; 305 const char *mntpnt = NULL, *info = NULL; 306 int pos, error; 307 308 AA_BUG(!profile); 309 AA_BUG(!mntpath); 310 AA_BUG(!buffer); 311 312 if (!PROFILE_MEDIATES(profile, AA_CLASS_MOUNT)) 313 return 0; 314 315 error = aa_path_name(mntpath, path_flags(profile, mntpath), buffer, 316 &mntpnt, &info, profile->disconnected); 317 if (error) 318 goto audit; 319 if (IS_ERR(devname)) { 320 error = PTR_ERR(devname); 321 devname = NULL; 322 info = devinfo; 323 goto audit; 324 } 325 326 error = -EACCES; 327 pos = do_match_mnt(&profile->policy, 328 profile->policy.start[AA_CLASS_MOUNT], 329 mntpnt, devname, type, flags, data, binary, &perms); 330 if (pos) { 331 info = mnt_info_table[pos]; 332 goto audit; 333 } 334 error = 0; 335 336 audit: 337 return audit_mount(profile, OP_MOUNT, mntpnt, devname, type, NULL, 338 flags, data, AA_MAY_MOUNT, &perms, info, error); 339 } 340 341 /** 342 * match_mnt - handle path matching for mount 343 * @profile: the confining profile 344 * @path: for the mntpnt (NOT NULL) 345 * @buffer: buffer to be used to lookup mntpath 346 * @devpath: path devname/src_name (MAYBE NULL) 347 * @devbuffer: buffer to be used to lookup devname/src_name 348 * @type: string for the dev type (MAYBE NULL) 349 * @flags: mount flags to match 350 * @data: fs mount data (MAYBE NULL) 351 * @binary: whether @data is binary 352 * 353 * Returns: 0 on success else error 354 */ 355 static int match_mnt(struct aa_profile *profile, const struct path *path, 356 char *buffer, const struct path *devpath, char *devbuffer, 357 const char *type, unsigned long flags, void *data, 358 bool binary) 359 { 360 const char *devname = NULL, *info = NULL; 361 int error = -EACCES; 362 363 AA_BUG(!profile); 364 AA_BUG(devpath && !devbuffer); 365 366 if (!PROFILE_MEDIATES(profile, AA_CLASS_MOUNT)) 367 return 0; 368 369 if (devpath) { 370 error = aa_path_name(devpath, path_flags(profile, devpath), 371 devbuffer, &devname, &info, 372 profile->disconnected); 373 if (error) 374 devname = ERR_PTR(error); 375 } 376 377 return match_mnt_path_str(profile, path, buffer, devname, type, flags, 378 data, binary, info); 379 } 380 381 int aa_remount(struct aa_label *label, const struct path *path, 382 unsigned long flags, void *data) 383 { 384 struct aa_profile *profile; 385 char *buffer = NULL; 386 bool binary; 387 int error; 388 389 AA_BUG(!label); 390 AA_BUG(!path); 391 392 binary = path->dentry->d_sb->s_type->fs_flags & FS_BINARY_MOUNTDATA; 393 394 buffer = aa_get_buffer(false); 395 if (!buffer) 396 return -ENOMEM; 397 error = fn_for_each_confined(label, profile, 398 match_mnt(profile, path, buffer, NULL, NULL, NULL, 399 flags, data, binary)); 400 aa_put_buffer(buffer); 401 402 return error; 403 } 404 405 int aa_bind_mount(struct aa_label *label, const struct path *path, 406 const char *dev_name, unsigned long flags) 407 { 408 struct aa_profile *profile; 409 char *buffer = NULL, *old_buffer = NULL; 410 struct path old_path; 411 int error; 412 413 AA_BUG(!label); 414 AA_BUG(!path); 415 416 if (!dev_name || !*dev_name) 417 return -EINVAL; 418 419 flags &= MS_REC | MS_BIND; 420 421 error = kern_path(dev_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 422 if (error) 423 return error; 424 425 buffer = aa_get_buffer(false); 426 old_buffer = aa_get_buffer(false); 427 error = -ENOMEM; 428 if (!buffer || !old_buffer) 429 goto out; 430 431 error = fn_for_each_confined(label, profile, 432 match_mnt(profile, path, buffer, &old_path, old_buffer, 433 NULL, flags, NULL, false)); 434 out: 435 aa_put_buffer(buffer); 436 aa_put_buffer(old_buffer); 437 path_put(&old_path); 438 439 return error; 440 } 441 442 int aa_mount_change_type(struct aa_label *label, const struct path *path, 443 unsigned long flags) 444 { 445 struct aa_profile *profile; 446 char *buffer = NULL; 447 int error; 448 449 AA_BUG(!label); 450 AA_BUG(!path); 451 452 /* These are the flags allowed by do_change_type() */ 453 flags &= (MS_REC | MS_SILENT | MS_SHARED | MS_PRIVATE | MS_SLAVE | 454 MS_UNBINDABLE); 455 456 buffer = aa_get_buffer(false); 457 if (!buffer) 458 return -ENOMEM; 459 error = fn_for_each_confined(label, profile, 460 match_mnt(profile, path, buffer, NULL, NULL, NULL, 461 flags, NULL, false)); 462 aa_put_buffer(buffer); 463 464 return error; 465 } 466 467 int aa_move_mount(struct aa_label *label, const struct path *path, 468 const char *orig_name) 469 { 470 struct aa_profile *profile; 471 char *buffer = NULL, *old_buffer = NULL; 472 struct path old_path; 473 int error; 474 475 AA_BUG(!label); 476 AA_BUG(!path); 477 478 if (!orig_name || !*orig_name) 479 return -EINVAL; 480 481 error = kern_path(orig_name, LOOKUP_FOLLOW, &old_path); 482 if (error) 483 return error; 484 485 buffer = aa_get_buffer(false); 486 old_buffer = aa_get_buffer(false); 487 error = -ENOMEM; 488 if (!buffer || !old_buffer) 489 goto out; 490 error = fn_for_each_confined(label, profile, 491 match_mnt(profile, path, buffer, &old_path, old_buffer, 492 NULL, MS_MOVE, NULL, false)); 493 out: 494 aa_put_buffer(buffer); 495 aa_put_buffer(old_buffer); 496 path_put(&old_path); 497 498 return error; 499 } 500 501 int aa_new_mount(struct aa_label *label, const char *dev_name, 502 const struct path *path, const char *type, unsigned long flags, 503 void *data) 504 { 505 struct aa_profile *profile; 506 char *buffer = NULL, *dev_buffer = NULL; 507 bool binary = true; 508 int error; 509 int requires_dev = 0; 510 struct path tmp_path, *dev_path = NULL; 511 512 AA_BUG(!label); 513 AA_BUG(!path); 514 515 if (type) { 516 struct file_system_type *fstype; 517 518 fstype = get_fs_type(type); 519 if (!fstype) 520 return -ENODEV; 521 binary = fstype->fs_flags & FS_BINARY_MOUNTDATA; 522 requires_dev = fstype->fs_flags & FS_REQUIRES_DEV; 523 put_filesystem(fstype); 524 525 if (requires_dev) { 526 if (!dev_name || !*dev_name) 527 return -ENOENT; 528 529 error = kern_path(dev_name, LOOKUP_FOLLOW, &tmp_path); 530 if (error) 531 return error; 532 dev_path = &tmp_path; 533 } 534 } 535 536 buffer = aa_get_buffer(false); 537 if (!buffer) { 538 error = -ENOMEM; 539 goto out; 540 } 541 if (dev_path) { 542 dev_buffer = aa_get_buffer(false); 543 if (!dev_buffer) { 544 error = -ENOMEM; 545 goto out; 546 } 547 error = fn_for_each_confined(label, profile, 548 match_mnt(profile, path, buffer, dev_path, dev_buffer, 549 type, flags, data, binary)); 550 } else { 551 error = fn_for_each_confined(label, profile, 552 match_mnt_path_str(profile, path, buffer, dev_name, 553 type, flags, data, binary, NULL)); 554 } 555 556 out: 557 aa_put_buffer(buffer); 558 aa_put_buffer(dev_buffer); 559 if (dev_path) 560 path_put(dev_path); 561 562 return error; 563 } 564 565 static int profile_umount(struct aa_profile *profile, const struct path *path, 566 char *buffer) 567 { 568 struct aa_perms perms = { }; 569 const char *name = NULL, *info = NULL; 570 unsigned int state; 571 int error; 572 573 AA_BUG(!profile); 574 AA_BUG(!path); 575 576 if (!PROFILE_MEDIATES(profile, AA_CLASS_MOUNT)) 577 return 0; 578 579 error = aa_path_name(path, path_flags(profile, path), buffer, &name, 580 &info, profile->disconnected); 581 if (error) 582 goto audit; 583 584 state = aa_dfa_match(profile->policy.dfa, 585 profile->policy.start[AA_CLASS_MOUNT], 586 name); 587 perms = *aa_lookup_perms(&profile->policy, state); 588 if (AA_MAY_UMOUNT & ~perms.allow) 589 error = -EACCES; 590 591 audit: 592 return audit_mount(profile, OP_UMOUNT, name, NULL, NULL, NULL, 0, NULL, 593 AA_MAY_UMOUNT, &perms, info, error); 594 } 595 596 int aa_umount(struct aa_label *label, struct vfsmount *mnt, int flags) 597 { 598 struct aa_profile *profile; 599 char *buffer = NULL; 600 int error; 601 struct path path = { .mnt = mnt, .dentry = mnt->mnt_root }; 602 603 AA_BUG(!label); 604 AA_BUG(!mnt); 605 606 buffer = aa_get_buffer(false); 607 if (!buffer) 608 return -ENOMEM; 609 610 error = fn_for_each_confined(label, profile, 611 profile_umount(profile, &path, buffer)); 612 aa_put_buffer(buffer); 613 614 return error; 615 } 616 617 /* helper fn for transition on pivotroot 618 * 619 * Returns: label for transition or ERR_PTR. Does not return NULL 620 */ 621 static struct aa_label *build_pivotroot(struct aa_profile *profile, 622 const struct path *new_path, 623 char *new_buffer, 624 const struct path *old_path, 625 char *old_buffer) 626 { 627 const char *old_name, *new_name = NULL, *info = NULL; 628 const char *trans_name = NULL; 629 struct aa_perms perms = { }; 630 unsigned int state; 631 int error; 632 633 AA_BUG(!profile); 634 AA_BUG(!new_path); 635 AA_BUG(!old_path); 636 637 if (profile_unconfined(profile) || 638 !PROFILE_MEDIATES(profile, AA_CLASS_MOUNT)) 639 return aa_get_newest_label(&profile->label); 640 641 error = aa_path_name(old_path, path_flags(profile, old_path), 642 old_buffer, &old_name, &info, 643 profile->disconnected); 644 if (error) 645 goto audit; 646 error = aa_path_name(new_path, path_flags(profile, new_path), 647 new_buffer, &new_name, &info, 648 profile->disconnected); 649 if (error) 650 goto audit; 651 652 error = -EACCES; 653 state = aa_dfa_match(profile->policy.dfa, 654 profile->policy.start[AA_CLASS_MOUNT], 655 new_name); 656 state = aa_dfa_null_transition(profile->policy.dfa, state); 657 state = aa_dfa_match(profile->policy.dfa, state, old_name); 658 perms = *aa_lookup_perms(&profile->policy, state); 659 660 if (AA_MAY_PIVOTROOT & perms.allow) 661 error = 0; 662 663 audit: 664 error = audit_mount(profile, OP_PIVOTROOT, new_name, old_name, 665 NULL, trans_name, 0, NULL, AA_MAY_PIVOTROOT, 666 &perms, info, error); 667 if (error) 668 return ERR_PTR(error); 669 670 return aa_get_newest_label(&profile->label); 671 } 672 673 int aa_pivotroot(struct aa_label *label, const struct path *old_path, 674 const struct path *new_path) 675 { 676 struct aa_profile *profile; 677 struct aa_label *target = NULL; 678 char *old_buffer = NULL, *new_buffer = NULL, *info = NULL; 679 int error; 680 681 AA_BUG(!label); 682 AA_BUG(!old_path); 683 AA_BUG(!new_path); 684 685 old_buffer = aa_get_buffer(false); 686 new_buffer = aa_get_buffer(false); 687 error = -ENOMEM; 688 if (!old_buffer || !new_buffer) 689 goto out; 690 target = fn_label_build(label, profile, GFP_KERNEL, 691 build_pivotroot(profile, new_path, new_buffer, 692 old_path, old_buffer)); 693 if (!target) { 694 info = "label build failed"; 695 error = -ENOMEM; 696 goto fail; 697 } else if (!IS_ERR(target)) { 698 error = aa_replace_current_label(target); 699 if (error) { 700 /* TODO: audit target */ 701 aa_put_label(target); 702 goto out; 703 } 704 aa_put_label(target); 705 } else 706 /* already audited error */ 707 error = PTR_ERR(target); 708 out: 709 aa_put_buffer(old_buffer); 710 aa_put_buffer(new_buffer); 711 712 return error; 713 714 fail: 715 /* TODO: add back in auditing of new_name and old_name */ 716 error = fn_for_each(label, profile, 717 audit_mount(profile, OP_PIVOTROOT, NULL /*new_name */, 718 NULL /* old_name */, 719 NULL, NULL, 720 0, NULL, AA_MAY_PIVOTROOT, &nullperms, info, 721 error)); 722 goto out; 723 } 724