1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
// SPDX-License-Identifier: Apache-2.0 or MIT
//
// Copyright 2021 Sony Group Corporation
//
use crate::api::ensure_supported_api;
use crate::error::{Result, SeccompError};
use libseccomp_sys::*;
use std::os::unix::io::AsRawFd;
use std::ptr::NonNull;
use crate::*;
const MINUS_EEXIST: i32 = -libc::EEXIST;
/// **Represents a filter context in the libseccomp.**
#[derive(Debug)]
pub struct ScmpFilterContext {
ctx: NonNull<libc::c_void>,
}
impl ScmpFilterContext {
/// Creates and returns a new filter context.
///
/// This initializes the internal seccomp filter state and should
/// be called before any other functions in this crate to ensure the filter
/// state is initialized.
///
/// This function returns a valid filter context.
///
/// This function corresponds to
/// [`seccomp_init`](https://man7.org/linux/man-pages/man3/seccomp_init.3.html).
///
/// # Arguments
///
/// * `default_action` - A default action to be taken for syscalls which match no rules in the filter
///
/// # Errors
///
/// If the filter context can not be created, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn new_filter(default_action: ScmpAction) -> Result<Self> {
let ctx_ptr = unsafe { seccomp_init(default_action.to_sys()) };
let ctx = NonNull::new(ctx_ptr)
.ok_or_else(|| SeccompError::with_msg("Could not create new filter"))?;
Ok(Self { ctx })
}
/// Merges two filters.
///
/// In order to merge two seccomp filters, both filters must have the same
/// attribute values and no overlapping architectures.
/// If successful, the `src` seccomp filter is released and all internal memory
/// associated with the filter is freed.
///
/// This function corresponds to
/// [`seccomp_merge`](https://man7.org/linux/man-pages/man3/seccomp_merge.3.html).
///
/// # Arguments
///
/// * `src` - A seccomp filter that will be merged into the filter this is called on.
///
/// # Errors
///
/// If merging the filters fails, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx1 = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// let mut ctx2 = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// if !ctx1.is_arch_present(ScmpArch::X8664)? {
/// ctx1.add_arch(ScmpArch::X8664)?;
/// ctx1.remove_arch(ScmpArch::Native)?;
/// }
/// if !ctx2.is_arch_present(ScmpArch::Aarch64)? {
/// ctx2.add_arch(ScmpArch::Aarch64)?;
/// ctx2.remove_arch(ScmpArch::Native)?;
/// }
/// ctx1.merge(ctx2)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn merge(&mut self, src: Self) -> Result<()> {
cvt(unsafe { seccomp_merge(self.ctx.as_ptr(), src.ctx.as_ptr()) })?;
// The src filter is already released.
std::mem::forget(src);
Ok(())
}
/// Checks if an architecture is present in a filter.
///
/// If a filter contains an architecture, it uses its default action for
/// syscalls which do not match rules in it, and its rules can match syscalls
/// for that ABI.
/// If a filter does not contain an architecture, all syscalls made to that
/// kernel ABI will fail with the filter's default Bad Architecture Action
/// (by default, killing the proc).
///
/// This function returns `Ok(true)` if the architecture is present in the filter,
/// `Ok(false)` otherwise.
///
/// This function corresponds to
/// [`seccomp_arch_exist`](https://man7.org/linux/man-pages/man3/seccomp_arch_exist.3.html).
///
/// # Arguments
///
/// * `arch` - An architecture token
///
/// # Errors
///
/// If this function is called with an invalid filter or an issue is
/// encountered calling to the libseccomp API, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// ctx.add_arch(ScmpArch::Aarch64)?;
/// assert!(ctx.is_arch_present(ScmpArch::Aarch64)?);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn is_arch_present(&self, arch: ScmpArch) -> Result<bool> {
match unsafe { seccomp_arch_exist(self.ctx.as_ptr(), arch.to_sys()) } {
0 => Ok(true),
MINUS_EEXIST => Ok(false),
errno => Err(SeccompError::from_errno(errno)),
}
}
/// Adds an architecture to the filter.
///
/// This function returns `Ok(true)` if the architecture was added to the
/// filter and `Ok(false)` if the architecture was already present in the
/// filter.
///
/// This function corresponds to
/// [`seccomp_arch_add`](https://man7.org/linux/man-pages/man3/seccomp_arch_add.3.html).
///
/// # Arguments
///
/// * `arch` - An architecture token
///
/// # Errors
///
/// If this function is called with an invalid filter or an issue is
/// encountered adding the architecture, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// ctx.add_arch(ScmpArch::X86)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn add_arch(&mut self, arch: ScmpArch) -> Result<bool> {
match unsafe { seccomp_arch_add(self.ctx.as_ptr(), arch.to_sys()) } {
0 => Ok(true),
MINUS_EEXIST => Ok(false),
errno => Err(SeccompError::from_errno(errno)),
}
}
/// Removes an architecture from the filter.
///
/// This function returns `Ok(true)` if the architecture was removed from
/// the filter and `Ok(false)` if the architecture wasn't present in the
/// filter.
///
/// This function corresponds to
/// [`seccomp_arch_remove`](https://man7.org/linux/man-pages/man3/seccomp_arch_remove.3.html).
///
/// # Arguments
///
/// * `arch` - An architecture token
///
/// # Errors
///
/// If this function is called with an invalid filter or an issue is
/// encountered removing the architecture, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// ctx.add_arch(ScmpArch::X86)?;
/// ctx.remove_arch(ScmpArch::X86)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn remove_arch(&mut self, arch: ScmpArch) -> Result<bool> {
match unsafe { seccomp_arch_remove(self.ctx.as_ptr(), arch.to_sys()) } {
0 => Ok(true),
MINUS_EEXIST => Ok(false),
errno => Err(SeccompError::from_errno(errno)),
}
}
/// Adds a single rule for an unconditional action on a syscall.
///
/// If the specified rule needs to be rewritten due to architecture specifics,
/// it will be rewritten without notification.
///
/// This function corresponds to
/// [`seccomp_rule_add`](https://man7.org/linux/man-pages/man3/seccomp_rule_add.3.html).
///
/// # Arguments
///
/// * `action` - An action to be taken on the call being made
/// * `syscall` - The number of syscall
///
/// # Errors
///
/// If this function is called with an invalid filter or an issue is
/// encountered adding the rule, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// let syscall = ScmpSyscall::from_name("ptrace")?;
/// ctx.add_rule(ScmpAction::Errno(libc::EPERM), syscall)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn add_rule<S: Into<ScmpSyscall>>(&mut self, action: ScmpAction, syscall: S) -> Result<()> {
self.add_rule_conditional(action, syscall, &[])
}
/// Adds a single rule for a conditional action on a syscall.
///
/// If the specified rule needs to be rewritten due to architecture specifics,
/// it will be rewritten without notification.
/// Comparators are AND'd together (i.e. all must match for the rule to match).
/// You can only compare each argument once in a single rule.
///
/// This function corresponds to
/// [`seccomp_rule_add_array`](https://man7.org/linux/man-pages/man3/seccomp_rule_add_array.3.html).
///
/// # Arguments
///
/// * `action` - An action to be taken on the call being made
/// * `syscall` - The number of syscall
/// * `comparators` - An array of the rule in a seccomp filter
///
/// # Errors
///
/// If this function is called with an invalid filter or an issue is
/// encountered adding the rule, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// let syscall = ScmpSyscall::from_name("open")?;
/// ctx.add_rule_conditional(
/// ScmpAction::Errno(libc::EPERM),
/// syscall,
/// &[scmp_cmp!($arg1 & (libc::O_TRUNC as u64) == libc::O_TRUNC as u64)],
/// )?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn add_rule_conditional<S: Into<ScmpSyscall>>(
&mut self,
action: ScmpAction,
syscall: S,
comparators: &[ScmpArgCompare],
) -> Result<()> {
cvt(unsafe {
seccomp_rule_add_array(
self.ctx.as_ptr(),
action.to_sys(),
syscall.into().to_sys(),
comparators.len() as u32,
comparators.as_ptr().cast::<scmp_arg_cmp>(),
)
})
}
/// Adds a single rule for an unconditional action on a syscall.
///
/// The functions will attempt to add the rule exactly as specified so it may
/// behave differently on different architectures.
/// If the specified rule can not be represented on the architecture,
/// the function will fail.
///
/// This function corresponds to
/// [`seccomp_rule_add_exact`](https://man7.org/linux/man-pages/man3/seccomp_rule_add_exact.3.html).
///
/// # Arguments
///
/// * `action` - An action to be taken on the call being made
/// * `syscall` - The number of syscall
///
/// # Errors
///
/// If this function is called with an invalid filter or an issue is
/// encountered adding the rule, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// let syscall = ScmpSyscall::from_name("dup3")?;
/// ctx.add_rule_exact(ScmpAction::KillThread, syscall)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn add_rule_exact<S: Into<ScmpSyscall>>(
&mut self,
action: ScmpAction,
syscall: S,
) -> Result<()> {
self.add_rule_conditional_exact(action, syscall, &[])
}
/// Adds a single rule for a conditional action on a syscall.
///
/// The functions will attempt to add the rule exactly as specified so it may
/// behave differently on different architectures.
/// If the specified rule can not be represented on the architecture,
/// the function will fail.
///
/// This function corresponds to
/// [`seccomp_rule_add_exact_array`](https://man7.org/linux/man-pages/man3/seccomp_rule_add_exact_array.3.html).
///
/// # Arguments
///
/// * `action` - An action to be taken on the call being made
/// * `syscall` - The number of syscall
/// * `comparators` - An array of the rule in a seccomp filter
///
/// # Errors
///
/// If this function is called with an invalid filter or an issue is
/// encountered adding the rule, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// let syscall = ScmpSyscall::from_name("socket")?;
/// ctx.add_rule_conditional_exact(
/// ScmpAction::Errno(libc::EPERM),
/// syscall,
/// &[scmp_cmp!($arg1 != libc::AF_UNIX as u64)],
/// )?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn add_rule_conditional_exact<S: Into<ScmpSyscall>>(
&mut self,
action: ScmpAction,
syscall: S,
comparators: &[ScmpArgCompare],
) -> Result<()> {
cvt(unsafe {
seccomp_rule_add_exact_array(
self.ctx.as_ptr(),
action.to_sys(),
syscall.into().to_sys(),
comparators.len() as u32,
comparators.as_ptr().cast::<scmp_arg_cmp>(),
)
})
}
/// Loads a filter context into the kernel.
///
/// If the function succeeds, the new filter will be active when the function returns.
///
/// This function corresponds to
/// [`seccomp_load`](https://man7.org/linux/man-pages/man3/seccomp_load.3.html).
///
/// # Errors
///
/// If this function is called with an invalid filter or an issue is
/// encountered loading the rule, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// let syscall = ScmpSyscall::from_name("dup3")?;
/// ctx.add_rule(ScmpAction::KillThread, syscall)?;
/// ctx.load()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn load(&self) -> Result<()> {
cvt(unsafe { seccomp_load(self.ctx.as_ptr()) })
}
/// Sets a syscall's priority.
///
/// This provides a priority hint to the seccomp filter generator in the libseccomp
/// such that higher priority syscalls are placed earlier in the seccomp filter code
/// so that they incur less overhead at the expense of lower priority syscalls.
///
/// This function corresponds to
/// [`seccomp_syscall_priority`](https://man7.org/linux/man-pages/man3/seccomp_syscall_priority.3.html).
///
/// # Arguments
///
/// * `syscall` - The number of syscall
/// * `priority` - The priority parameter that is an 8-bit value ranging from 0 to 255;
/// a higher value represents a higher priority.
///
/// # Errors
///
/// If this function is called with an invalid filter or the number of syscall
/// is invalid, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// let syscall = ScmpSyscall::from_name("open")?;
/// ctx.set_syscall_priority(syscall, 100)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn set_syscall_priority<S: Into<ScmpSyscall>>(
&mut self,
syscall: S,
priority: u8,
) -> Result<()> {
cvt(unsafe {
seccomp_syscall_priority(self.ctx.as_ptr(), syscall.into().to_sys(), priority)
})
}
/// Gets a raw filter attribute value.
///
/// The seccomp filter attributes are tunable values that affect how the library behaves
/// when generating and loading the seccomp filter into the kernel.
///
/// This function corresponds to
/// [`seccomp_attr_get`](https://man7.org/linux/man-pages/man3/seccomp_attr_get.3.html).
///
/// # Arguments
///
/// * `attr` - A seccomp filter attribute
///
/// # Errors
///
/// If this function is called with an invalid filter or an issue is
/// encountered retrieving the attribute, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// assert_ne!(ctx.get_filter_attr(ScmpFilterAttr::CtlNnp)?, 0);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn get_filter_attr(&self, attr: ScmpFilterAttr) -> Result<u32> {
let mut attribute: u32 = 0;
cvt(unsafe { seccomp_attr_get(self.ctx.as_ptr(), attr.to_sys(), &mut attribute) })?;
Ok(attribute)
}
/// Gets the default action as specified in the call to
/// [`new_filter()`](ScmpFilterContext::new_filter) or [`reset()`](ScmpFilterContext::reset).
///
/// This function corresponds to
/// [`seccomp_attr_get`](https://man7.org/linux/man-pages/man3/seccomp_attr_get.3.html).
///
/// # Errors
///
/// If this function is called with an invalid filter or an issue is
/// encountered getting the action, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// let action = ctx.get_act_default()?;
/// assert_eq!(action, ScmpAction::Allow);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn get_act_default(&self) -> Result<ScmpAction> {
let ret = self.get_filter_attr(ScmpFilterAttr::ActDefault)?;
ScmpAction::from_sys(ret)
}
/// Gets the default action taken when the loaded filter does not match the architecture
/// of the executing application.
///
/// This function corresponds to
/// [`seccomp_attr_get`](https://man7.org/linux/man-pages/man3/seccomp_attr_get.3.html).
///
/// # Errors
///
/// If this function is called with an invalid filter or an issue is
/// encountered getting the action, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// let action = ctx.get_act_badarch()?;
/// assert_eq!(action, ScmpAction::KillThread);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn get_act_badarch(&self) -> Result<ScmpAction> {
let ret = self.get_filter_attr(ScmpFilterAttr::ActBadArch)?;
ScmpAction::from_sys(ret)
}
/// Gets the current state of the [`ScmpFilterAttr::CtlNnp`] attribute.
///
/// This function returns `Ok(true)` if the [`ScmpFilterAttr::CtlNnp`] attribute is set to on the filter being
/// loaded, `Ok(false)` otherwise.
///
/// This function corresponds to
/// [`seccomp_attr_get`](https://man7.org/linux/man-pages/man3/seccomp_attr_get.3.html).
///
/// # Errors
///
/// If this function is called with an invalid filter or an issue is
/// encountered getting the current state, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// ctx.set_ctl_nnp(false)?;
/// assert!(!ctx.get_ctl_nnp()?);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn get_ctl_nnp(&self) -> Result<bool> {
let ret = self.get_filter_attr(ScmpFilterAttr::CtlNnp)?;
Ok(ret != 0)
}
/// Deprecated alias for [`ScmpFilterContext::get_ctl_nnp()`].
#[deprecated(since = "0.2.3", note = "Use ScmpFilterContext::get_ctl_nnp().")]
pub fn get_no_new_privs_bit(&self) -> Result<bool> {
self.get_ctl_nnp()
}
/// Gets the current state of the [`ScmpFilterAttr::CtlTsync`] attribute.
///
/// This function returns `Ok(true)` if the [`ScmpFilterAttr::CtlTsync`] attribute set to on the filter being
/// loaded, `Ok(false)` otherwise.
///
/// This function corresponds to
/// [`seccomp_attr_get`](https://man7.org/linux/man-pages/man3/seccomp_attr_get.3.html).
///
/// # Errors
///
/// If this function is called with an invalid filter, an issue is encountered
/// getting the current state, or the libseccomp API level is less than 2, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// ctx.set_ctl_tsync(true)?;
/// assert!(ctx.get_ctl_tsync()?);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn get_ctl_tsync(&self) -> Result<bool> {
ensure_supported_api("get_ctl_tsync", 2, ScmpVersion::from((2, 2, 0)))?;
let ret = self.get_filter_attr(ScmpFilterAttr::CtlTsync)?;
Ok(ret != 0)
}
/// Gets the current state of the [`ScmpFilterAttr::CtlLog`] attribute.
///
/// This function returns `Ok(true)` if the [`ScmpFilterAttr::CtlLog`] attribute set to on the filter being
/// loaded, `Ok(false)` otherwise.
///
/// This function corresponds to
/// [`seccomp_attr_get`](https://man7.org/linux/man-pages/man3/seccomp_attr_get.3.html).
///
/// # Errors
///
/// If this function is called with an invalid filter, an issue is encountered
/// getting the current state, or the libseccomp API level is less than 3, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// ctx.set_ctl_log(true)?;
/// assert!(ctx.get_ctl_log()?);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn get_ctl_log(&self) -> Result<bool> {
ensure_supported_api("get_ctl_log", 3, ScmpVersion::from((2, 4, 0)))?;
let ret = self.get_filter_attr(ScmpFilterAttr::CtlLog)?;
Ok(ret != 0)
}
/// Gets the current state of the [`ScmpFilterAttr::CtlSsb`] attribute.
///
/// This function returns `Ok(true)` if the [`ScmpFilterAttr::CtlSsb`] attribute set to on the filter being
/// loaded, `Ok(false)` otherwise.
/// The [`ScmpFilterAttr::CtlSsb`] attribute is only usable when the libseccomp API level 4 or higher
/// is supported.
///
/// This function corresponds to
/// [`seccomp_attr_get`](https://man7.org/linux/man-pages/man3/seccomp_attr_get.3.html).
///
/// # Errors
///
/// If this function is called with an invalid filter, an issue is encountered
/// getting the current state, or the libseccomp API level is less than 4, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// # if check_api(4, ScmpVersion::from((2, 5, 0))).unwrap() {
/// ctx.set_ctl_ssb(false)?;
/// assert!(!ctx.get_ctl_ssb()?);
/// # }
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn get_ctl_ssb(&self) -> Result<bool> {
ensure_supported_api("get_ctl_ssb", 4, ScmpVersion::from((2, 5, 0)))?;
let ret = self.get_filter_attr(ScmpFilterAttr::CtlSsb)?;
Ok(ret != 0)
}
/// Gets the current optimization level of the [`ScmpFilterAttr::CtlOptimize`] attribute.
///
/// See [`set_ctl_optimize()`](ScmpFilterContext::set_ctl_optimize) for more details about
/// the optimization level.
/// The [`ScmpFilterAttr::CtlOptimize`] attribute is only usable when the libseccomp API level 4 or higher
/// is supported.
///
/// This function corresponds to
/// [`seccomp_attr_get`](https://man7.org/linux/man-pages/man3/seccomp_attr_get.3.html).
///
/// # Errors
///
/// If this function is called with an invalid filter, an issue is encountered
/// getting the current state, or the libseccomp API level is less than 4, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// # if check_api(4, ScmpVersion::from((2, 5, 0)))? {
/// ctx.set_ctl_optimize(2)?;
/// assert_eq!(ctx.get_ctl_optimize()?, 2);
/// # }
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn get_ctl_optimize(&self) -> Result<u32> {
ensure_supported_api("get_ctl_optimize", 4, ScmpVersion::from((2, 5, 0)))?;
let ret = self.get_filter_attr(ScmpFilterAttr::CtlOptimize)?;
Ok(ret)
}
/// Gets the current state of the [`ScmpFilterAttr::ApiSysRawRc`] attribute.
///
/// This function returns `Ok(true)` if the [`ScmpFilterAttr::ApiSysRawRc`] attribute set to on the filter
/// being loaded, `Ok(false)` otherwise.
/// The [`ScmpFilterAttr::ApiSysRawRc`] attribute is only usable when the libseccomp API level 4 or higher
/// is supported.
///
/// This function corresponds to
/// [`seccomp_attr_get`](https://man7.org/linux/man-pages/man3/seccomp_attr_get.3.html).
///
/// # Errors
///
/// If this function is called with an invalid filter, an issue is encountered
/// getting the current state, or the libseccomp API level is less than 4, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// # if check_api(4, ScmpVersion::from((2, 5, 0)))? {
/// ctx.set_api_sysrawrc(true)?;
/// assert!(ctx.get_api_sysrawrc()?);
/// # }
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn get_api_sysrawrc(&self) -> Result<bool> {
ensure_supported_api("get_api_sysrawrc", 4, ScmpVersion::from((2, 5, 0)))?;
let ret = self.get_filter_attr(ScmpFilterAttr::ApiSysRawRc)?;
Ok(ret != 0)
}
/// Sets a raw filter attribute value.
///
/// The seccomp filter attributes are tunable values that affect how the library behaves
/// when generating and loading the seccomp filter into the kernel.
///
/// This function corresponds to
/// [`seccomp_attr_set`](https://man7.org/linux/man-pages/man3/seccomp_attr_set.3.html).
///
/// # Arguments
///
/// * `attr` - A seccomp filter attribute
/// * `value` - A value of or the parameter of the attribute
///
/// See the [`seccomp_attr_set(3)`] man page for details on available attribute values.
///
/// [`seccomp_attr_set(3)`]: https://www.man7.org/linux/man-pages/man3/seccomp_attr_set.3.html
///
/// # Errors
///
/// If this function is called with an invalid filter or an issue is
/// encountered setting the attribute, an error will be returned.
pub fn set_filter_attr(&mut self, attr: ScmpFilterAttr, value: u32) -> Result<()> {
cvt(unsafe { seccomp_attr_set(self.ctx.as_ptr(), attr.to_sys(), value) })
}
/// Sets the default action taken when the loaded filter does not match the architecture
/// of the executing application.
///
/// Defaults to on (`action` == [`ScmpAction::KillThread`]).
///
/// This function corresponds to
/// [`seccomp_attr_set`](https://man7.org/linux/man-pages/man3/seccomp_attr_set.3.html).
///
/// # Arguments
///
/// * `action` - An action to be taken on a syscall for an architecture not in the filter.
///
/// # Errors
///
/// If this function is called with an invalid filter or an issue is
/// encountered setting the attribute, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// ctx.set_act_badarch(ScmpAction::KillProcess)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn set_act_badarch(&mut self, action: ScmpAction) -> Result<()> {
self.set_filter_attr(ScmpFilterAttr::ActBadArch, action.to_sys())
}
/// Sets the state of the [`ScmpFilterAttr::CtlNnp`] attribute which will be applied
/// on filter load.
///
/// Settings this to off (`state` == `false`) means that loading the seccomp filter
/// into the kernel fill fail if the `CAP_SYS_ADMIN` is missing.
///
/// Defaults to on (`state` == `true`).
///
/// This function corresponds to
/// [`seccomp_attr_set`](https://man7.org/linux/man-pages/man3/seccomp_attr_set.3.html).
///
/// # Arguments
///
/// * `state` - A state flag to specify whether the [`ScmpFilterAttr::CtlNnp`] attribute should be enabled
///
/// # Errors
///
/// If this function is called with an invalid filter or an issue is
/// encountered setting the attribute, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// ctx.set_ctl_nnp(false)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn set_ctl_nnp(&mut self, state: bool) -> Result<()> {
self.set_filter_attr(ScmpFilterAttr::CtlNnp, state.into())
}
/// Deprecated alias for [`ScmpFilterContext::set_ctl_nnp()`].
#[deprecated(since = "0.2.3", note = "Use ScmpFilterContext::set_ctl_nnp().")]
pub fn set_no_new_privs_bit(&mut self, state: bool) -> Result<()> {
self.set_ctl_nnp(state)
}
/// Sets the state of the [`ScmpFilterAttr::CtlTsync`] attribute which will be applied
/// on filter load.
///
/// Settings this to on (`state` == `true`) means that the kernel should attempt to synchronize the filters
/// across all threads on [`ScmpFilterContext::load()`].
/// If the kernel is unable to synchronize all of the thread then the load operation will fail.
/// The [`ScmpFilterAttr::CtlTsync`] attribute is only usable when the libseccomp API level 2 or higher
/// is supported.
/// If the libseccomp API level is less than 6, the [`ScmpFilterAttr::CtlTsync`] attribute is unusable
/// with the userspace notification API simultaneously.
///
/// Defaults to off (`state` == `false`).
///
/// This function corresponds to
/// [`seccomp_attr_set`](https://man7.org/linux/man-pages/man3/seccomp_attr_set.3.html).
///
/// # Arguments
///
/// * `state` - A state flag to specify whether the [`ScmpFilterAttr::CtlTsync`] attribute should be enabled
///
/// # Errors
///
/// If this function is called with an invalid filter, an issue is encountered
/// setting the attribute, or the libseccomp API level is less than 2, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// ctx.set_ctl_tsync(true)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn set_ctl_tsync(&mut self, state: bool) -> Result<()> {
ensure_supported_api("set_ctl_tsync", 2, ScmpVersion::from((2, 2, 0)))?;
self.set_filter_attr(ScmpFilterAttr::CtlTsync, state.into())
}
/// Sets the state of the [`ScmpFilterAttr::CtlLog`] attribute which will be applied on filter load.
///
/// Settings this to on (`state` == `true`) means that the kernel should log all filter
/// actions taken except for the [`ScmpAction::Allow`] action.
/// The [`ScmpFilterAttr::CtlLog`] attribute is only usable when the libseccomp API level 3 or higher
/// is supported.
///
/// Defaults to off (`state` == `false`).
///
/// This function corresponds to
/// [`seccomp_attr_set`](https://man7.org/linux/man-pages/man3/seccomp_attr_set.3.html).
///
/// # Arguments
///
/// * `state` - A state flag to specify whether the [`ScmpFilterAttr::CtlLog`] attribute should
/// be enabled
///
/// # Errors
///
/// If this function is called with an invalid filter, an issue is encountered
/// setting the attribute, or the libseccomp API level is less than 3, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// ctx.set_ctl_log(true)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn set_ctl_log(&mut self, state: bool) -> Result<()> {
ensure_supported_api("set_ctl_log", 3, ScmpVersion::from((2, 4, 0)))?;
self.set_filter_attr(ScmpFilterAttr::CtlLog, state.into())
}
/// Sets the state of the [`ScmpFilterAttr::CtlSsb`] attribute which will be applied on filter load.
///
/// Settings this to on (`state` == `true`) disables Speculative Store Bypass mitigations for the filter.
/// The [`ScmpFilterAttr::CtlSsb`] attribute is only usable when the libseccomp API level 4 or higher
/// is supported.
///
/// Defaults to off (`state` == `false`).
///
/// This function corresponds to
/// [`seccomp_attr_set`](https://man7.org/linux/man-pages/man3/seccomp_attr_set.3.html).
///
/// # Arguments
///
/// * `state` - A state flag to specify whether the [`ScmpFilterAttr::CtlSsb`] attribute should
/// be enabled
///
/// # Errors
///
/// If this function is called with an invalid filter, an issue is encountered
/// setting the attribute, or the libseccomp API level is less than 4, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// # if check_api(4, ScmpVersion::from((2, 5, 0))).unwrap() {
/// ctx.set_ctl_ssb(false)?;
/// # }
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn set_ctl_ssb(&mut self, state: bool) -> Result<()> {
ensure_supported_api("set_ctl_ssb", 4, ScmpVersion::from((2, 5, 0)))?;
self.set_filter_attr(ScmpFilterAttr::CtlSsb, state.into())
}
/// Sets the [`ScmpFilterAttr::CtlOptimize`] level which will be applied on filter load.
///
/// By default the libseccomp generates a set of sequential "if" statements for each rule in the filter.
/// [`set_syscall_priority()`](ScmpFilterContext::set_syscall_priority) can be used to prioritize the
/// order for the default cause. The binary tree optimization sorts by syscall numbers and generates
/// consistent O(log n) filter traversal for every rule in the filter. The binary tree may be advantageous
/// for large filters. Note that [`set_syscall_priority()`](ScmpFilterContext::set_syscall_priority) is
/// ignored when `level` == `2`.
/// The [`ScmpFilterAttr::CtlOptimize`] attribute is only usable when the libseccomp API level 4 or higher
/// is supported.
///
/// The different optimization levels are described below:
/// * `0` - Reserved value, not currently used.
/// * `1` - Rules sorted by priority and complexity (DEFAULT).
/// * `2` - Binary tree sorted by syscall number.
///
/// This function corresponds to
/// [`seccomp_attr_set`](https://man7.org/linux/man-pages/man3/seccomp_attr_set.3.html).
///
/// # Arguments
///
/// * `level` - The optimization level of the filter
///
/// # Errors
///
/// If this function is called with an invalid filter, an issue is encountered
/// setting the attribute, or the libseccomp API level is less than 4, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// # if check_api(4, ScmpVersion::from((2, 5, 0)))? {
/// ctx.set_ctl_optimize(2)?;
/// # }
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn set_ctl_optimize(&mut self, level: u32) -> Result<()> {
ensure_supported_api("set_ctl_optimize", 4, ScmpVersion::from((2, 5, 0)))?;
self.set_filter_attr(ScmpFilterAttr::CtlOptimize, level)
}
/// Sets the state of the [`ScmpFilterAttr::ApiSysRawRc`] attribute which will be applied on filter load.
///
/// Settings this to on (`state` == `true`) means that the libseccomp should pass system error codes
/// back to the caller instead of the default -ECANCELED.
/// The [`ScmpFilterAttr::ApiSysRawRc`] attribute is only usable when the libseccomp API level 4 or higher
/// is supported.
///
/// Defaults to off (`state` == `false`).
///
/// This function corresponds to
/// [`seccomp_attr_set`](https://man7.org/linux/man-pages/man3/seccomp_attr_set.3.html).
///
/// # Arguments
///
/// * `state` - A state flag to specify whether the [`ScmpFilterAttr::ApiSysRawRc`] attribute should
/// be enabled
///
/// # Errors
///
/// If this function is called with an invalid filter, an issue is encountered
/// setting the attribute, or the libseccomp API level is less than 4, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// # if check_api(4, ScmpVersion::from((2, 5, 0)))? {
/// ctx.set_api_sysrawrc(true)?;
/// # }
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn set_api_sysrawrc(&mut self, state: bool) -> Result<()> {
ensure_supported_api("set_api_sysrawrc", 4, ScmpVersion::from((2, 5, 0)))?;
self.set_filter_attr(ScmpFilterAttr::ApiSysRawRc, state.into())
}
/// Outputs PFC(Pseudo Filter Code)-formatted, human-readable dump of a filter context's rules to a file.
///
/// This function corresponds to
/// [`seccomp_export_pfc`](https://man7.org/linux/man-pages/man3/seccomp_export_pfc.3.html).
///
/// # Arguments
///
/// * `fd` - A file descriptor to write to (must be open for writing)
///
/// # Errors
///
/// If this function is called with an invalid filter or writing to the file fails,
/// an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// # use std::io::{stdout};
/// let ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// ctx.export_pfc(&mut stdout())?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn export_pfc<T: AsRawFd>(&self, fd: &mut T) -> Result<()> {
cvt(unsafe { seccomp_export_pfc(self.ctx.as_ptr(), fd.as_raw_fd()) })
}
/// Outputs BPF(Berkeley Packet Filter)-formatted, kernel-readable dump of a
/// filter context's rules to a file.
///
/// This function corresponds to
/// [`seccomp_export_bpf`](https://man7.org/linux/man-pages/man3/seccomp_export_bpf.3.html).
///
/// # Arguments
///
/// * `fd` - A file descriptor to write to (must be open for writing)
///
/// # Errors
///
/// If this function is called with an invalid filter or writing to the file fails,
/// an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// # use std::io::{stdout};
/// let ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// ctx.export_bpf(&mut stdout())?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn export_bpf<T: AsRawFd>(&self, fd: &mut T) -> Result<()> {
cvt(unsafe { seccomp_export_bpf(self.ctx.as_ptr(), fd.as_raw_fd()) })
}
/// Resets a filter context, removing all its existing state.
///
/// This function corresponds to
/// [`seccomp_reset`](https://man7.org/linux/man-pages/man3/seccomp_reset.3.html).
///
/// # Arguments
///
/// * `action` - A new default action to be taken for syscalls which do not match
///
/// # Errors
///
/// If this function is called with an invalid filter or an issue is encountered
/// resetting the filter, an error will be returned.
///
/// # Examples
///
/// ```
/// # use libseccomp::*;
/// let mut ctx = ScmpFilterContext::new_filter(ScmpAction::Allow)?;
/// ctx.reset(ScmpAction::KillThread)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn reset(&mut self, action: ScmpAction) -> Result<()> {
cvt(unsafe { seccomp_reset(self.ctx.as_ptr(), action.to_sys()) })
}
/// Gets a raw pointer of a seccomp filter.
///
/// This function returns a raw pointer to the [`scmp_filter_ctx`].
/// The caller must ensure that the filter outlives the pointer this function returns,
/// or else it will end up pointing to garbage.
/// You may only modify the filter referenced by the pointer with functions intended
/// for this (the once provided by [`libseccomp_sys`] crate).
#[must_use]
pub fn as_ptr(&self) -> scmp_filter_ctx {
self.ctx.as_ptr()
}
}
impl Drop for ScmpFilterContext {
/// Releases a filter context, freeing its memory.
///
/// After calling this function, the given filter is no longer valid and cannot be used.
///
/// This function corresponds to
/// [`seccomp_release`](https://man7.org/linux/man-pages/man3/seccomp_release.3.html).
fn drop(&mut self) {
unsafe { seccomp_release(self.ctx.as_ptr()) }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_as_ptr() {
let ctx = ScmpFilterContext::new_filter(ScmpAction::Allow).unwrap();
assert_eq!(ctx.as_ptr(), ctx.ctx.as_ptr());
}
}