// Copyright 2019 The ChromiumOS Authors1// Use of this source code is governed by a BSD-style license that can be2// found in the LICENSE file.34use libc::c_int;5use libc::c_void;67use super::errno_result;8use super::Result;910#[allow(non_camel_case_types)]11type cap_t = *mut c_void;1213#[link(name = "cap")]14extern "C" {15fn cap_init() -> cap_t;16fn cap_free(ptr: *mut c_void) -> c_int;17fn cap_set_proc(cap: cap_t) -> c_int;18}1920/// Drops all capabilities (permitted, inheritable, and effective) from the current process.21pub fn drop_capabilities() -> Result<()> {22// SAFETY:23// Safe because we do not actually manipulate any memory handled by libcap24// and we check errors.25unsafe {26let caps = cap_init();27if caps.is_null() {28return errno_result();29}3031// Freshly initialized capabilities do not have any bits set, so applying them32// will drop all capabilities from the process.33// Safe because we will check the result and otherwise do not touch the memory.34let ret = cap_set_proc(caps);35// We need to free capabilities regardless of success of the operation above.36cap_free(caps);37// Now check if we managed to apply (drop) capabilities.38if ret < 0 {39return errno_result();40}41}42Ok(())43}444546