.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* Updated: Karl MacMillan <kmacmillan@tresys.com> |
---|
2 | 3 | * |
---|
3 | 4 | * Added conditional policy language extensions |
---|
.. | .. |
---|
9 | 10 | * Copyright (C) 2007 Hewlett-Packard Development Company, L.P. |
---|
10 | 11 | * Copyright (C) 2003 - 2004 Tresys Technology, LLC |
---|
11 | 12 | * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> |
---|
12 | | - * This program is free software; you can redistribute it and/or modify |
---|
13 | | - * it under the terms of the GNU General Public License as published by |
---|
14 | | - * the Free Software Foundation, version 2. |
---|
15 | 13 | */ |
---|
16 | 14 | |
---|
17 | 15 | #include <linux/kernel.h> |
---|
.. | .. |
---|
19 | 17 | #include <linux/slab.h> |
---|
20 | 18 | #include <linux/vmalloc.h> |
---|
21 | 19 | #include <linux/fs.h> |
---|
| 20 | +#include <linux/fs_context.h> |
---|
22 | 21 | #include <linux/mount.h> |
---|
23 | 22 | #include <linux/mutex.h> |
---|
| 23 | +#include <linux/namei.h> |
---|
24 | 24 | #include <linux/init.h> |
---|
25 | 25 | #include <linux/string.h> |
---|
26 | 26 | #include <linux/security.h> |
---|
.. | .. |
---|
75 | 75 | unsigned long last_class_ino; |
---|
76 | 76 | bool policy_opened; |
---|
77 | 77 | struct dentry *policycap_dir; |
---|
78 | | - struct mutex mutex; |
---|
79 | 78 | unsigned long last_ino; |
---|
80 | 79 | struct selinux_state *state; |
---|
81 | 80 | struct super_block *sb; |
---|
.. | .. |
---|
89 | 88 | if (!fsi) |
---|
90 | 89 | return -ENOMEM; |
---|
91 | 90 | |
---|
92 | | - mutex_init(&fsi->mutex); |
---|
93 | 91 | fsi->last_ino = SEL_INO_NEXT - 1; |
---|
94 | 92 | fsi->state = &selinux_state; |
---|
95 | 93 | fsi->sb = sb; |
---|
.. | .. |
---|
117 | 115 | #define SEL_CLASS_INO_OFFSET 0x04000000 |
---|
118 | 116 | #define SEL_POLICYCAP_INO_OFFSET 0x08000000 |
---|
119 | 117 | #define SEL_INO_MASK 0x00ffffff |
---|
| 118 | + |
---|
| 119 | +#define BOOL_DIR_NAME "booleans" |
---|
| 120 | +#define CLASS_DIR_NAME "class" |
---|
| 121 | +#define POLICYCAP_DIR_NAME "policy_capabilities" |
---|
120 | 122 | |
---|
121 | 123 | #define TMPBUFLEN 12 |
---|
122 | 124 | static ssize_t sel_read_enforce(struct file *filp, char __user *buf, |
---|
.. | .. |
---|
169 | 171 | goto out; |
---|
170 | 172 | audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS, |
---|
171 | 173 | "enforcing=%d old_enforcing=%d auid=%u ses=%u" |
---|
172 | | - " enabled=%d old-enabled=%d lsm=selinux res=1", |
---|
| 174 | + " enabled=1 old-enabled=1 lsm=selinux res=1", |
---|
173 | 175 | new_value, old_value, |
---|
174 | 176 | from_kuid(&init_user_ns, audit_get_loginuid(current)), |
---|
175 | | - audit_get_sessionid(current), |
---|
176 | | - selinux_enabled, selinux_enabled); |
---|
| 177 | + audit_get_sessionid(current)); |
---|
177 | 178 | enforcing_set(state, new_value); |
---|
178 | 179 | if (new_value) |
---|
179 | 180 | avc_ss_reset(state->avc, 0); |
---|
180 | 181 | selnl_notify_setenforce(new_value); |
---|
181 | 182 | selinux_status_update_setenforce(state, new_value); |
---|
182 | 183 | if (!new_value) |
---|
183 | | - call_lsm_notifier(LSM_POLICY_CHANGE, NULL); |
---|
| 184 | + call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL); |
---|
184 | 185 | } |
---|
185 | 186 | length = count; |
---|
186 | 187 | out: |
---|
.. | .. |
---|
283 | 284 | int new_value; |
---|
284 | 285 | int enforcing; |
---|
285 | 286 | |
---|
| 287 | + /* NOTE: we are now officially considering runtime disable as |
---|
| 288 | + * deprecated, and using it will become increasingly painful |
---|
| 289 | + * (e.g. sleeping/blocking) as we progress through future |
---|
| 290 | + * kernel releases until eventually it is removed |
---|
| 291 | + */ |
---|
| 292 | + pr_err("SELinux: Runtime disable is deprecated, use selinux=0 on the kernel cmdline.\n"); |
---|
| 293 | + |
---|
286 | 294 | if (count >= PAGE_SIZE) |
---|
287 | 295 | return -ENOMEM; |
---|
288 | 296 | |
---|
.. | .. |
---|
305 | 313 | goto out; |
---|
306 | 314 | audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS, |
---|
307 | 315 | "enforcing=%d old_enforcing=%d auid=%u ses=%u" |
---|
308 | | - " enabled=%d old-enabled=%d lsm=selinux res=1", |
---|
| 316 | + " enabled=0 old-enabled=1 lsm=selinux res=1", |
---|
309 | 317 | enforcing, enforcing, |
---|
310 | 318 | from_kuid(&init_user_ns, audit_get_loginuid(current)), |
---|
311 | | - audit_get_sessionid(current), 0, 1); |
---|
| 319 | + audit_get_sessionid(current)); |
---|
312 | 320 | } |
---|
313 | 321 | |
---|
314 | 322 | length = count; |
---|
.. | .. |
---|
341 | 349 | }; |
---|
342 | 350 | |
---|
343 | 351 | /* declaration for sel_write_load */ |
---|
344 | | -static int sel_make_bools(struct selinux_fs_info *fsi); |
---|
345 | | -static int sel_make_classes(struct selinux_fs_info *fsi); |
---|
346 | | -static int sel_make_policycap(struct selinux_fs_info *fsi); |
---|
| 352 | +static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir, |
---|
| 353 | + unsigned int *bool_num, char ***bool_pending_names, |
---|
| 354 | + unsigned int **bool_pending_values); |
---|
| 355 | +static int sel_make_classes(struct selinux_policy *newpolicy, |
---|
| 356 | + struct dentry *class_dir, |
---|
| 357 | + unsigned long *last_class_ino); |
---|
347 | 358 | |
---|
348 | 359 | /* declaration for sel_make_class_dirs */ |
---|
349 | 360 | static struct dentry *sel_make_dir(struct dentry *dir, const char *name, |
---|
350 | 361 | unsigned long *ino); |
---|
| 362 | + |
---|
| 363 | +/* declaration for sel_make_policy_nodes */ |
---|
| 364 | +static struct dentry *sel_make_disconnected_dir(struct super_block *sb, |
---|
| 365 | + unsigned long *ino); |
---|
| 366 | + |
---|
| 367 | +/* declaration for sel_make_policy_nodes */ |
---|
| 368 | +static void sel_remove_entries(struct dentry *de); |
---|
351 | 369 | |
---|
352 | 370 | static ssize_t sel_read_mls(struct file *filp, char __user *buf, |
---|
353 | 371 | size_t count, loff_t *ppos) |
---|
.. | .. |
---|
380 | 398 | |
---|
381 | 399 | BUG_ON(filp->private_data); |
---|
382 | 400 | |
---|
383 | | - mutex_lock(&fsi->mutex); |
---|
| 401 | + mutex_lock(&fsi->state->policy_mutex); |
---|
384 | 402 | |
---|
385 | 403 | rc = avc_has_perm(&selinux_state, |
---|
386 | 404 | current_sid(), SECINITSID_SECURITY, |
---|
.. | .. |
---|
397 | 415 | if (!plm) |
---|
398 | 416 | goto err; |
---|
399 | 417 | |
---|
400 | | - if (i_size_read(inode) != security_policydb_len(state)) { |
---|
401 | | - inode_lock(inode); |
---|
402 | | - i_size_write(inode, security_policydb_len(state)); |
---|
403 | | - inode_unlock(inode); |
---|
404 | | - } |
---|
405 | | - |
---|
406 | 418 | rc = security_read_policy(state, &plm->data, &plm->len); |
---|
407 | 419 | if (rc) |
---|
408 | 420 | goto err; |
---|
| 421 | + |
---|
| 422 | + if ((size_t)i_size_read(inode) != plm->len) { |
---|
| 423 | + inode_lock(inode); |
---|
| 424 | + i_size_write(inode, plm->len); |
---|
| 425 | + inode_unlock(inode); |
---|
| 426 | + } |
---|
409 | 427 | |
---|
410 | 428 | fsi->policy_opened = 1; |
---|
411 | 429 | |
---|
412 | 430 | filp->private_data = plm; |
---|
413 | 431 | |
---|
414 | | - mutex_unlock(&fsi->mutex); |
---|
| 432 | + mutex_unlock(&fsi->state->policy_mutex); |
---|
415 | 433 | |
---|
416 | 434 | return 0; |
---|
417 | 435 | err: |
---|
418 | | - mutex_unlock(&fsi->mutex); |
---|
| 436 | + mutex_unlock(&fsi->state->policy_mutex); |
---|
419 | 437 | |
---|
420 | 438 | if (plm) |
---|
421 | 439 | vfree(plm->data); |
---|
.. | .. |
---|
503 | 521 | .llseek = generic_file_llseek, |
---|
504 | 522 | }; |
---|
505 | 523 | |
---|
506 | | -static int sel_make_policy_nodes(struct selinux_fs_info *fsi) |
---|
| 524 | +static void sel_remove_old_bool_data(unsigned int bool_num, char **bool_names, |
---|
| 525 | + unsigned int *bool_values) |
---|
507 | 526 | { |
---|
508 | | - int ret; |
---|
| 527 | + u32 i; |
---|
509 | 528 | |
---|
510 | | - ret = sel_make_bools(fsi); |
---|
| 529 | + /* bool_dir cleanup */ |
---|
| 530 | + for (i = 0; i < bool_num; i++) |
---|
| 531 | + kfree(bool_names[i]); |
---|
| 532 | + kfree(bool_names); |
---|
| 533 | + kfree(bool_values); |
---|
| 534 | +} |
---|
| 535 | + |
---|
| 536 | +static int sel_make_policy_nodes(struct selinux_fs_info *fsi, |
---|
| 537 | + struct selinux_policy *newpolicy) |
---|
| 538 | +{ |
---|
| 539 | + int ret = 0; |
---|
| 540 | + struct dentry *tmp_parent, *tmp_bool_dir, *tmp_class_dir, *old_dentry; |
---|
| 541 | + unsigned int tmp_bool_num, old_bool_num; |
---|
| 542 | + char **tmp_bool_names, **old_bool_names; |
---|
| 543 | + unsigned int *tmp_bool_values, *old_bool_values; |
---|
| 544 | + unsigned long tmp_ino = fsi->last_ino; /* Don't increment last_ino in this function */ |
---|
| 545 | + |
---|
| 546 | + tmp_parent = sel_make_disconnected_dir(fsi->sb, &tmp_ino); |
---|
| 547 | + if (IS_ERR(tmp_parent)) |
---|
| 548 | + return PTR_ERR(tmp_parent); |
---|
| 549 | + |
---|
| 550 | + tmp_ino = fsi->bool_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */ |
---|
| 551 | + tmp_bool_dir = sel_make_dir(tmp_parent, BOOL_DIR_NAME, &tmp_ino); |
---|
| 552 | + if (IS_ERR(tmp_bool_dir)) { |
---|
| 553 | + ret = PTR_ERR(tmp_bool_dir); |
---|
| 554 | + goto out; |
---|
| 555 | + } |
---|
| 556 | + |
---|
| 557 | + tmp_ino = fsi->class_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */ |
---|
| 558 | + tmp_class_dir = sel_make_dir(tmp_parent, CLASS_DIR_NAME, &tmp_ino); |
---|
| 559 | + if (IS_ERR(tmp_class_dir)) { |
---|
| 560 | + ret = PTR_ERR(tmp_class_dir); |
---|
| 561 | + goto out; |
---|
| 562 | + } |
---|
| 563 | + |
---|
| 564 | + ret = sel_make_bools(newpolicy, tmp_bool_dir, &tmp_bool_num, |
---|
| 565 | + &tmp_bool_names, &tmp_bool_values); |
---|
511 | 566 | if (ret) { |
---|
512 | 567 | pr_err("SELinux: failed to load policy booleans\n"); |
---|
513 | | - return ret; |
---|
| 568 | + goto out; |
---|
514 | 569 | } |
---|
515 | 570 | |
---|
516 | | - ret = sel_make_classes(fsi); |
---|
| 571 | + ret = sel_make_classes(newpolicy, tmp_class_dir, |
---|
| 572 | + &fsi->last_class_ino); |
---|
517 | 573 | if (ret) { |
---|
518 | 574 | pr_err("SELinux: failed to load policy classes\n"); |
---|
519 | | - return ret; |
---|
| 575 | + goto out; |
---|
520 | 576 | } |
---|
521 | 577 | |
---|
522 | | - ret = sel_make_policycap(fsi); |
---|
523 | | - if (ret) { |
---|
524 | | - pr_err("SELinux: failed to load policy capabilities\n"); |
---|
525 | | - return ret; |
---|
526 | | - } |
---|
| 578 | + /* booleans */ |
---|
| 579 | + old_dentry = fsi->bool_dir; |
---|
| 580 | + lock_rename(tmp_bool_dir, old_dentry); |
---|
| 581 | + d_exchange(tmp_bool_dir, fsi->bool_dir); |
---|
527 | 582 | |
---|
528 | | - return 0; |
---|
| 583 | + old_bool_num = fsi->bool_num; |
---|
| 584 | + old_bool_names = fsi->bool_pending_names; |
---|
| 585 | + old_bool_values = fsi->bool_pending_values; |
---|
| 586 | + |
---|
| 587 | + fsi->bool_num = tmp_bool_num; |
---|
| 588 | + fsi->bool_pending_names = tmp_bool_names; |
---|
| 589 | + fsi->bool_pending_values = tmp_bool_values; |
---|
| 590 | + |
---|
| 591 | + sel_remove_old_bool_data(old_bool_num, old_bool_names, old_bool_values); |
---|
| 592 | + |
---|
| 593 | + fsi->bool_dir = tmp_bool_dir; |
---|
| 594 | + unlock_rename(tmp_bool_dir, old_dentry); |
---|
| 595 | + |
---|
| 596 | + /* classes */ |
---|
| 597 | + old_dentry = fsi->class_dir; |
---|
| 598 | + lock_rename(tmp_class_dir, old_dentry); |
---|
| 599 | + d_exchange(tmp_class_dir, fsi->class_dir); |
---|
| 600 | + fsi->class_dir = tmp_class_dir; |
---|
| 601 | + unlock_rename(tmp_class_dir, old_dentry); |
---|
| 602 | + |
---|
| 603 | +out: |
---|
| 604 | + /* Since the other temporary dirs are children of tmp_parent |
---|
| 605 | + * this will handle all the cleanup in the case of a failure before |
---|
| 606 | + * the swapover |
---|
| 607 | + */ |
---|
| 608 | + sel_remove_entries(tmp_parent); |
---|
| 609 | + dput(tmp_parent); /* d_genocide() only handles the children */ |
---|
| 610 | + |
---|
| 611 | + return ret; |
---|
529 | 612 | } |
---|
530 | 613 | |
---|
531 | 614 | static ssize_t sel_write_load(struct file *file, const char __user *buf, |
---|
.. | .. |
---|
533 | 616 | |
---|
534 | 617 | { |
---|
535 | 618 | struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; |
---|
| 619 | + struct selinux_load_state load_state; |
---|
536 | 620 | ssize_t length; |
---|
537 | 621 | void *data = NULL; |
---|
538 | 622 | |
---|
539 | | - mutex_lock(&fsi->mutex); |
---|
| 623 | + mutex_lock(&fsi->state->policy_mutex); |
---|
540 | 624 | |
---|
541 | 625 | length = avc_has_perm(&selinux_state, |
---|
542 | 626 | current_sid(), SECINITSID_SECURITY, |
---|
.. | .. |
---|
549 | 633 | if (*ppos != 0) |
---|
550 | 634 | goto out; |
---|
551 | 635 | |
---|
552 | | - length = -EFBIG; |
---|
553 | | - if (count > 64 * 1024 * 1024) |
---|
554 | | - goto out; |
---|
555 | | - |
---|
556 | 636 | length = -ENOMEM; |
---|
557 | 637 | data = vmalloc(count); |
---|
558 | 638 | if (!data) |
---|
.. | .. |
---|
562 | 642 | if (copy_from_user(data, buf, count) != 0) |
---|
563 | 643 | goto out; |
---|
564 | 644 | |
---|
565 | | - length = security_load_policy(fsi->state, data, count); |
---|
| 645 | + length = security_load_policy(fsi->state, data, count, &load_state); |
---|
566 | 646 | if (length) { |
---|
567 | 647 | pr_warn_ratelimited("SELinux: failed to load policy\n"); |
---|
568 | 648 | goto out; |
---|
569 | 649 | } |
---|
570 | 650 | |
---|
571 | | - length = sel_make_policy_nodes(fsi); |
---|
572 | | - if (length) |
---|
573 | | - goto out1; |
---|
| 651 | + length = sel_make_policy_nodes(fsi, load_state.policy); |
---|
| 652 | + if (length) { |
---|
| 653 | + selinux_policy_cancel(fsi->state, &load_state); |
---|
| 654 | + goto out; |
---|
| 655 | + } |
---|
| 656 | + |
---|
| 657 | + selinux_policy_commit(fsi->state, &load_state); |
---|
574 | 658 | |
---|
575 | 659 | length = count; |
---|
576 | 660 | |
---|
577 | | -out1: |
---|
578 | 661 | audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD, |
---|
579 | 662 | "auid=%u ses=%u lsm=selinux res=1", |
---|
580 | 663 | from_kuid(&init_user_ns, audit_get_loginuid(current)), |
---|
581 | 664 | audit_get_sessionid(current)); |
---|
582 | 665 | out: |
---|
583 | | - mutex_unlock(&fsi->mutex); |
---|
| 666 | + mutex_unlock(&fsi->state->policy_mutex); |
---|
584 | 667 | vfree(data); |
---|
585 | 668 | return length; |
---|
586 | 669 | } |
---|
.. | .. |
---|
633 | 716 | char tmpbuf[TMPBUFLEN]; |
---|
634 | 717 | ssize_t length; |
---|
635 | 718 | |
---|
636 | | - length = scnprintf(tmpbuf, TMPBUFLEN, "%u", fsi->state->checkreqprot); |
---|
| 719 | + length = scnprintf(tmpbuf, TMPBUFLEN, "%u", |
---|
| 720 | + checkreqprot_get(fsi->state)); |
---|
637 | 721 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
---|
638 | 722 | } |
---|
639 | 723 | |
---|
.. | .. |
---|
667 | 751 | if (sscanf(page, "%u", &new_value) != 1) |
---|
668 | 752 | goto out; |
---|
669 | 753 | |
---|
670 | | - fsi->state->checkreqprot = new_value ? 1 : 0; |
---|
| 754 | + if (new_value) { |
---|
| 755 | + char comm[sizeof(current->comm)]; |
---|
| 756 | + |
---|
| 757 | + memcpy(comm, current->comm, sizeof(comm)); |
---|
| 758 | + pr_warn_once("SELinux: %s (%d) set checkreqprot to 1. This is deprecated and will be rejected in a future kernel release.\n", |
---|
| 759 | + comm, current->pid); |
---|
| 760 | + } |
---|
| 761 | + |
---|
| 762 | + checkreqprot_set(fsi->state, (new_value ? 1 : 0)); |
---|
671 | 763 | length = count; |
---|
672 | 764 | out: |
---|
673 | 765 | kfree(page); |
---|
.. | .. |
---|
1177 | 1269 | unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; |
---|
1178 | 1270 | const char *name = filep->f_path.dentry->d_name.name; |
---|
1179 | 1271 | |
---|
1180 | | - mutex_lock(&fsi->mutex); |
---|
| 1272 | + mutex_lock(&fsi->state->policy_mutex); |
---|
1181 | 1273 | |
---|
1182 | 1274 | ret = -EINVAL; |
---|
1183 | 1275 | if (index >= fsi->bool_num || strcmp(name, |
---|
.. | .. |
---|
1196 | 1288 | } |
---|
1197 | 1289 | length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing, |
---|
1198 | 1290 | fsi->bool_pending_values[index]); |
---|
1199 | | - mutex_unlock(&fsi->mutex); |
---|
| 1291 | + mutex_unlock(&fsi->state->policy_mutex); |
---|
1200 | 1292 | ret = simple_read_from_buffer(buf, count, ppos, page, length); |
---|
1201 | 1293 | out_free: |
---|
1202 | 1294 | free_page((unsigned long)page); |
---|
1203 | 1295 | return ret; |
---|
1204 | 1296 | |
---|
1205 | 1297 | out_unlock: |
---|
1206 | | - mutex_unlock(&fsi->mutex); |
---|
| 1298 | + mutex_unlock(&fsi->state->policy_mutex); |
---|
1207 | 1299 | goto out_free; |
---|
1208 | 1300 | } |
---|
1209 | 1301 | |
---|
.. | .. |
---|
1228 | 1320 | if (IS_ERR(page)) |
---|
1229 | 1321 | return PTR_ERR(page); |
---|
1230 | 1322 | |
---|
1231 | | - mutex_lock(&fsi->mutex); |
---|
| 1323 | + mutex_lock(&fsi->state->policy_mutex); |
---|
1232 | 1324 | |
---|
1233 | 1325 | length = avc_has_perm(&selinux_state, |
---|
1234 | 1326 | current_sid(), SECINITSID_SECURITY, |
---|
.. | .. |
---|
1253 | 1345 | length = count; |
---|
1254 | 1346 | |
---|
1255 | 1347 | out: |
---|
1256 | | - mutex_unlock(&fsi->mutex); |
---|
| 1348 | + mutex_unlock(&fsi->state->policy_mutex); |
---|
1257 | 1349 | kfree(page); |
---|
1258 | 1350 | return length; |
---|
1259 | 1351 | } |
---|
.. | .. |
---|
1284 | 1376 | if (IS_ERR(page)) |
---|
1285 | 1377 | return PTR_ERR(page); |
---|
1286 | 1378 | |
---|
1287 | | - mutex_lock(&fsi->mutex); |
---|
| 1379 | + mutex_lock(&fsi->state->policy_mutex); |
---|
1288 | 1380 | |
---|
1289 | 1381 | length = avc_has_perm(&selinux_state, |
---|
1290 | 1382 | current_sid(), SECINITSID_SECURITY, |
---|
.. | .. |
---|
1306 | 1398 | length = count; |
---|
1307 | 1399 | |
---|
1308 | 1400 | out: |
---|
1309 | | - mutex_unlock(&fsi->mutex); |
---|
| 1401 | + mutex_unlock(&fsi->state->policy_mutex); |
---|
1310 | 1402 | kfree(page); |
---|
1311 | 1403 | return length; |
---|
1312 | 1404 | } |
---|
.. | .. |
---|
1322 | 1414 | shrink_dcache_parent(de); |
---|
1323 | 1415 | } |
---|
1324 | 1416 | |
---|
1325 | | -#define BOOL_DIR_NAME "booleans" |
---|
1326 | | - |
---|
1327 | | -static int sel_make_bools(struct selinux_fs_info *fsi) |
---|
| 1417 | +static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir, |
---|
| 1418 | + unsigned int *bool_num, char ***bool_pending_names, |
---|
| 1419 | + unsigned int **bool_pending_values) |
---|
1328 | 1420 | { |
---|
1329 | | - int i, ret; |
---|
| 1421 | + int ret; |
---|
1330 | 1422 | ssize_t len; |
---|
1331 | 1423 | struct dentry *dentry = NULL; |
---|
1332 | | - struct dentry *dir = fsi->bool_dir; |
---|
1333 | 1424 | struct inode *inode = NULL; |
---|
1334 | 1425 | struct inode_security_struct *isec; |
---|
1335 | 1426 | char **names = NULL, *page; |
---|
1336 | | - int num; |
---|
| 1427 | + u32 i, num; |
---|
1337 | 1428 | int *values = NULL; |
---|
1338 | 1429 | u32 sid; |
---|
1339 | | - |
---|
1340 | | - /* remove any existing files */ |
---|
1341 | | - for (i = 0; i < fsi->bool_num; i++) |
---|
1342 | | - kfree(fsi->bool_pending_names[i]); |
---|
1343 | | - kfree(fsi->bool_pending_names); |
---|
1344 | | - kfree(fsi->bool_pending_values); |
---|
1345 | | - fsi->bool_num = 0; |
---|
1346 | | - fsi->bool_pending_names = NULL; |
---|
1347 | | - fsi->bool_pending_values = NULL; |
---|
1348 | | - |
---|
1349 | | - sel_remove_entries(dir); |
---|
1350 | 1430 | |
---|
1351 | 1431 | ret = -ENOMEM; |
---|
1352 | 1432 | page = (char *)get_zeroed_page(GFP_KERNEL); |
---|
1353 | 1433 | if (!page) |
---|
1354 | 1434 | goto out; |
---|
1355 | 1435 | |
---|
1356 | | - ret = security_get_bools(fsi->state, &num, &names, &values); |
---|
| 1436 | + ret = security_get_bools(newpolicy, &num, &names, &values); |
---|
1357 | 1437 | if (ret) |
---|
1358 | 1438 | goto out; |
---|
1359 | 1439 | |
---|
1360 | 1440 | for (i = 0; i < num; i++) { |
---|
1361 | 1441 | ret = -ENOMEM; |
---|
1362 | | - dentry = d_alloc_name(dir, names[i]); |
---|
| 1442 | + dentry = d_alloc_name(bool_dir, names[i]); |
---|
1363 | 1443 | if (!dentry) |
---|
1364 | 1444 | goto out; |
---|
1365 | 1445 | |
---|
1366 | 1446 | ret = -ENOMEM; |
---|
1367 | | - inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR); |
---|
| 1447 | + inode = sel_make_inode(bool_dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR); |
---|
1368 | 1448 | if (!inode) { |
---|
1369 | 1449 | dput(dentry); |
---|
1370 | 1450 | goto out; |
---|
.. | .. |
---|
1378 | 1458 | goto out; |
---|
1379 | 1459 | } |
---|
1380 | 1460 | |
---|
1381 | | - isec = (struct inode_security_struct *)inode->i_security; |
---|
1382 | | - ret = security_genfs_sid(fsi->state, "selinuxfs", page, |
---|
| 1461 | + isec = selinux_inode(inode); |
---|
| 1462 | + ret = selinux_policy_genfs_sid(newpolicy, "selinuxfs", page, |
---|
1383 | 1463 | SECCLASS_FILE, &sid); |
---|
1384 | 1464 | if (ret) { |
---|
1385 | 1465 | pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n", |
---|
.. | .. |
---|
1393 | 1473 | inode->i_ino = i|SEL_BOOL_INO_OFFSET; |
---|
1394 | 1474 | d_add(dentry, inode); |
---|
1395 | 1475 | } |
---|
1396 | | - fsi->bool_num = num; |
---|
1397 | | - fsi->bool_pending_names = names; |
---|
1398 | | - fsi->bool_pending_values = values; |
---|
| 1476 | + *bool_num = num; |
---|
| 1477 | + *bool_pending_names = names; |
---|
| 1478 | + *bool_pending_values = values; |
---|
1399 | 1479 | |
---|
1400 | 1480 | free_page((unsigned long)page); |
---|
1401 | 1481 | return 0; |
---|
.. | .. |
---|
1408 | 1488 | kfree(names); |
---|
1409 | 1489 | } |
---|
1410 | 1490 | kfree(values); |
---|
1411 | | - sel_remove_entries(dir); |
---|
| 1491 | + sel_remove_entries(bool_dir); |
---|
1412 | 1492 | |
---|
1413 | 1493 | return ret; |
---|
1414 | 1494 | } |
---|
.. | .. |
---|
1692 | 1772 | for (i = 1; i <= SECINITSID_NUM; i++) { |
---|
1693 | 1773 | struct inode *inode; |
---|
1694 | 1774 | struct dentry *dentry; |
---|
1695 | | - dentry = d_alloc_name(dir, security_get_initial_sid_context(i)); |
---|
| 1775 | + const char *s = security_get_initial_sid_context(i); |
---|
| 1776 | + |
---|
| 1777 | + if (!s) |
---|
| 1778 | + continue; |
---|
| 1779 | + dentry = d_alloc_name(dir, s); |
---|
1696 | 1780 | if (!dentry) |
---|
1697 | 1781 | return -ENOMEM; |
---|
1698 | 1782 | |
---|
.. | .. |
---|
1735 | 1819 | { |
---|
1736 | 1820 | unsigned long ino = file_inode(file)->i_ino; |
---|
1737 | 1821 | char res[TMPBUFLEN]; |
---|
1738 | | - ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino)); |
---|
| 1822 | + ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_class(ino)); |
---|
1739 | 1823 | return simple_read_from_buffer(buf, count, ppos, res, len); |
---|
1740 | 1824 | } |
---|
1741 | 1825 | |
---|
.. | .. |
---|
1749 | 1833 | { |
---|
1750 | 1834 | unsigned long ino = file_inode(file)->i_ino; |
---|
1751 | 1835 | char res[TMPBUFLEN]; |
---|
1752 | | - ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino)); |
---|
| 1836 | + ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino)); |
---|
1753 | 1837 | return simple_read_from_buffer(buf, count, ppos, res, len); |
---|
1754 | 1838 | } |
---|
1755 | 1839 | |
---|
.. | .. |
---|
1778 | 1862 | .llseek = generic_file_llseek, |
---|
1779 | 1863 | }; |
---|
1780 | 1864 | |
---|
1781 | | -static int sel_make_perm_files(char *objclass, int classvalue, |
---|
1782 | | - struct dentry *dir) |
---|
| 1865 | +static int sel_make_perm_files(struct selinux_policy *newpolicy, |
---|
| 1866 | + char *objclass, int classvalue, |
---|
| 1867 | + struct dentry *dir) |
---|
1783 | 1868 | { |
---|
1784 | | - struct selinux_fs_info *fsi = dir->d_sb->s_fs_info; |
---|
1785 | 1869 | int i, rc, nperms; |
---|
1786 | 1870 | char **perms; |
---|
1787 | 1871 | |
---|
1788 | | - rc = security_get_permissions(fsi->state, objclass, &perms, &nperms); |
---|
| 1872 | + rc = security_get_permissions(newpolicy, objclass, &perms, &nperms); |
---|
1789 | 1873 | if (rc) |
---|
1790 | 1874 | return rc; |
---|
1791 | 1875 | |
---|
.. | .. |
---|
1818 | 1902 | return rc; |
---|
1819 | 1903 | } |
---|
1820 | 1904 | |
---|
1821 | | -static int sel_make_class_dir_entries(char *classname, int index, |
---|
1822 | | - struct dentry *dir) |
---|
| 1905 | +static int sel_make_class_dir_entries(struct selinux_policy *newpolicy, |
---|
| 1906 | + char *classname, int index, |
---|
| 1907 | + struct dentry *dir) |
---|
1823 | 1908 | { |
---|
1824 | 1909 | struct super_block *sb = dir->d_sb; |
---|
1825 | 1910 | struct selinux_fs_info *fsi = sb->s_fs_info; |
---|
.. | .. |
---|
1845 | 1930 | if (IS_ERR(dentry)) |
---|
1846 | 1931 | return PTR_ERR(dentry); |
---|
1847 | 1932 | |
---|
1848 | | - rc = sel_make_perm_files(classname, index, dentry); |
---|
| 1933 | + rc = sel_make_perm_files(newpolicy, classname, index, dentry); |
---|
1849 | 1934 | |
---|
1850 | 1935 | return rc; |
---|
1851 | 1936 | } |
---|
1852 | 1937 | |
---|
1853 | | -static int sel_make_classes(struct selinux_fs_info *fsi) |
---|
| 1938 | +static int sel_make_classes(struct selinux_policy *newpolicy, |
---|
| 1939 | + struct dentry *class_dir, |
---|
| 1940 | + unsigned long *last_class_ino) |
---|
1854 | 1941 | { |
---|
1855 | 1942 | |
---|
1856 | 1943 | int rc, nclasses, i; |
---|
1857 | 1944 | char **classes; |
---|
1858 | 1945 | |
---|
1859 | | - /* delete any existing entries */ |
---|
1860 | | - sel_remove_entries(fsi->class_dir); |
---|
1861 | | - |
---|
1862 | | - rc = security_get_classes(fsi->state, &classes, &nclasses); |
---|
| 1946 | + rc = security_get_classes(newpolicy, &classes, &nclasses); |
---|
1863 | 1947 | if (rc) |
---|
1864 | 1948 | return rc; |
---|
1865 | 1949 | |
---|
1866 | 1950 | /* +2 since classes are 1-indexed */ |
---|
1867 | | - fsi->last_class_ino = sel_class_to_ino(nclasses + 2); |
---|
| 1951 | + *last_class_ino = sel_class_to_ino(nclasses + 2); |
---|
1868 | 1952 | |
---|
1869 | 1953 | for (i = 0; i < nclasses; i++) { |
---|
1870 | 1954 | struct dentry *class_name_dir; |
---|
1871 | 1955 | |
---|
1872 | | - class_name_dir = sel_make_dir(fsi->class_dir, classes[i], |
---|
1873 | | - &fsi->last_class_ino); |
---|
| 1956 | + class_name_dir = sel_make_dir(class_dir, classes[i], |
---|
| 1957 | + last_class_ino); |
---|
1874 | 1958 | if (IS_ERR(class_name_dir)) { |
---|
1875 | 1959 | rc = PTR_ERR(class_name_dir); |
---|
1876 | 1960 | goto out; |
---|
1877 | 1961 | } |
---|
1878 | 1962 | |
---|
1879 | 1963 | /* i+1 since class values are 1-indexed */ |
---|
1880 | | - rc = sel_make_class_dir_entries(classes[i], i + 1, |
---|
| 1964 | + rc = sel_make_class_dir_entries(newpolicy, classes[i], i + 1, |
---|
1881 | 1965 | class_name_dir); |
---|
1882 | 1966 | if (rc) |
---|
1883 | 1967 | goto out; |
---|
.. | .. |
---|
1895 | 1979 | unsigned int iter; |
---|
1896 | 1980 | struct dentry *dentry = NULL; |
---|
1897 | 1981 | struct inode *inode = NULL; |
---|
1898 | | - |
---|
1899 | | - sel_remove_entries(fsi->policycap_dir); |
---|
1900 | 1982 | |
---|
1901 | 1983 | for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) { |
---|
1902 | 1984 | if (iter < ARRAY_SIZE(selinux_policycap_names)) |
---|
.. | .. |
---|
1949 | 2031 | return dentry; |
---|
1950 | 2032 | } |
---|
1951 | 2033 | |
---|
| 2034 | +static struct dentry *sel_make_disconnected_dir(struct super_block *sb, |
---|
| 2035 | + unsigned long *ino) |
---|
| 2036 | +{ |
---|
| 2037 | + struct inode *inode = sel_make_inode(sb, S_IFDIR | S_IRUGO | S_IXUGO); |
---|
| 2038 | + |
---|
| 2039 | + if (!inode) |
---|
| 2040 | + return ERR_PTR(-ENOMEM); |
---|
| 2041 | + |
---|
| 2042 | + inode->i_op = &simple_dir_inode_operations; |
---|
| 2043 | + inode->i_fop = &simple_dir_operations; |
---|
| 2044 | + inode->i_ino = ++(*ino); |
---|
| 2045 | + /* directory inodes start off with i_nlink == 2 (for "." entry) */ |
---|
| 2046 | + inc_nlink(inode); |
---|
| 2047 | + return d_obtain_alias(inode); |
---|
| 2048 | +} |
---|
| 2049 | + |
---|
1952 | 2050 | #define NULL_FILE_NAME "null" |
---|
1953 | 2051 | |
---|
1954 | | -static int sel_fill_super(struct super_block *sb, void *data, int silent) |
---|
| 2052 | +static int sel_fill_super(struct super_block *sb, struct fs_context *fc) |
---|
1955 | 2053 | { |
---|
1956 | 2054 | struct selinux_fs_info *fsi; |
---|
1957 | 2055 | int ret; |
---|
.. | .. |
---|
2011 | 2109 | } |
---|
2012 | 2110 | |
---|
2013 | 2111 | inode->i_ino = ++fsi->last_ino; |
---|
2014 | | - isec = (struct inode_security_struct *)inode->i_security; |
---|
| 2112 | + isec = selinux_inode(inode); |
---|
2015 | 2113 | isec->sid = SECINITSID_DEVNULL; |
---|
2016 | 2114 | isec->sclass = SECCLASS_CHR_FILE; |
---|
2017 | 2115 | isec->initialized = LABEL_INITIALIZED; |
---|
.. | .. |
---|
2026 | 2124 | } |
---|
2027 | 2125 | |
---|
2028 | 2126 | ret = sel_make_avc_files(dentry); |
---|
| 2127 | + if (ret) |
---|
| 2128 | + goto err; |
---|
2029 | 2129 | |
---|
2030 | 2130 | dentry = sel_make_dir(sb->s_root, "ss", &fsi->last_ino); |
---|
2031 | 2131 | if (IS_ERR(dentry)) { |
---|
.. | .. |
---|
2047 | 2147 | if (ret) |
---|
2048 | 2148 | goto err; |
---|
2049 | 2149 | |
---|
2050 | | - fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino); |
---|
| 2150 | + fsi->class_dir = sel_make_dir(sb->s_root, CLASS_DIR_NAME, &fsi->last_ino); |
---|
2051 | 2151 | if (IS_ERR(fsi->class_dir)) { |
---|
2052 | 2152 | ret = PTR_ERR(fsi->class_dir); |
---|
2053 | 2153 | fsi->class_dir = NULL; |
---|
2054 | 2154 | goto err; |
---|
2055 | 2155 | } |
---|
2056 | 2156 | |
---|
2057 | | - fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities", |
---|
| 2157 | + fsi->policycap_dir = sel_make_dir(sb->s_root, POLICYCAP_DIR_NAME, |
---|
2058 | 2158 | &fsi->last_ino); |
---|
2059 | 2159 | if (IS_ERR(fsi->policycap_dir)) { |
---|
2060 | 2160 | ret = PTR_ERR(fsi->policycap_dir); |
---|
.. | .. |
---|
2062 | 2162 | goto err; |
---|
2063 | 2163 | } |
---|
2064 | 2164 | |
---|
2065 | | - ret = sel_make_policy_nodes(fsi); |
---|
2066 | | - if (ret) |
---|
| 2165 | + ret = sel_make_policycap(fsi); |
---|
| 2166 | + if (ret) { |
---|
| 2167 | + pr_err("SELinux: failed to load policy capabilities\n"); |
---|
2067 | 2168 | goto err; |
---|
| 2169 | + } |
---|
| 2170 | + |
---|
2068 | 2171 | return 0; |
---|
2069 | 2172 | err: |
---|
2070 | 2173 | pr_err("SELinux: %s: failed while creating inodes\n", |
---|
.. | .. |
---|
2075 | 2178 | return ret; |
---|
2076 | 2179 | } |
---|
2077 | 2180 | |
---|
2078 | | -static struct dentry *sel_mount(struct file_system_type *fs_type, |
---|
2079 | | - int flags, const char *dev_name, void *data) |
---|
| 2181 | +static int sel_get_tree(struct fs_context *fc) |
---|
2080 | 2182 | { |
---|
2081 | | - return mount_single(fs_type, flags, data, sel_fill_super); |
---|
| 2183 | + return get_tree_single(fc, sel_fill_super); |
---|
| 2184 | +} |
---|
| 2185 | + |
---|
| 2186 | +static const struct fs_context_operations sel_context_ops = { |
---|
| 2187 | + .get_tree = sel_get_tree, |
---|
| 2188 | +}; |
---|
| 2189 | + |
---|
| 2190 | +static int sel_init_fs_context(struct fs_context *fc) |
---|
| 2191 | +{ |
---|
| 2192 | + fc->ops = &sel_context_ops; |
---|
| 2193 | + return 0; |
---|
2082 | 2194 | } |
---|
2083 | 2195 | |
---|
2084 | 2196 | static void sel_kill_sb(struct super_block *sb) |
---|
.. | .. |
---|
2089 | 2201 | |
---|
2090 | 2202 | static struct file_system_type sel_fs_type = { |
---|
2091 | 2203 | .name = "selinuxfs", |
---|
2092 | | - .mount = sel_mount, |
---|
| 2204 | + .init_fs_context = sel_init_fs_context, |
---|
2093 | 2205 | .kill_sb = sel_kill_sb, |
---|
2094 | 2206 | }; |
---|
2095 | 2207 | |
---|
.. | .. |
---|
2102 | 2214 | sizeof(NULL_FILE_NAME)-1); |
---|
2103 | 2215 | int err; |
---|
2104 | 2216 | |
---|
2105 | | - if (!selinux_enabled) |
---|
| 2217 | + if (!selinux_enabled_boot) |
---|
2106 | 2218 | return 0; |
---|
2107 | 2219 | |
---|
2108 | 2220 | err = sysfs_create_mount_point(fs_kobj, "selinux"); |
---|