Skip to content

Commit

Permalink
Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/…
Browse files Browse the repository at this point in the history
…pcmoore/lsm.git
  • Loading branch information
sfrothwell committed Mar 7, 2025
2 parents 25fa16e + ab938b5 commit 18897e4
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 39 deletions.
2 changes: 1 addition & 1 deletion arch/x86/events/intel/bts.c
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ static int bts_event_init(struct perf_event *event)
* to the user in a zero-copy fashion.
*/
if (event->attr.exclude_kernel) {
ret = perf_allow_kernel(&event->attr);
ret = perf_allow_kernel();
if (ret)
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/events/intel/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -4247,7 +4247,7 @@ static int intel_pmu_hw_config(struct perf_event *event)
if (x86_pmu.version < 3)
return -EINVAL;

ret = perf_allow_cpu(&event->attr);
ret = perf_allow_cpu();
if (ret)
return ret;

Expand Down
2 changes: 1 addition & 1 deletion arch/x86/events/intel/p4.c
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ static int p4_validate_raw_event(struct perf_event *event)
* the user needs special permissions to be able to use it
*/
if (p4_ht_active() && p4_event_bind_map[v].shared) {
v = perf_allow_cpu(&event->attr);
v = perf_allow_cpu();
if (v)
return v;
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/perf/arm_spe_pmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static_assert((PERF_EVENT_FLAG_ARCH & SPE_PMU_HW_FLAGS_CX) == SPE_PMU_HW_FLAGS_C

static void set_spe_event_has_cx(struct perf_event *event)
{
if (IS_ENABLED(CONFIG_PID_IN_CONTEXTIDR) && !perf_allow_kernel(&event->attr))
if (IS_ENABLED(CONFIG_PID_IN_CONTEXTIDR) && !perf_allow_kernel())
event->hw.flags |= SPE_PMU_HW_FLAGS_CX;
}

Expand Down Expand Up @@ -765,7 +765,7 @@ static int arm_spe_pmu_event_init(struct perf_event *event)
set_spe_event_has_cx(event);
reg = arm_spe_event_to_pmscr(event);
if (reg & (PMSCR_EL1_PA | PMSCR_EL1_PCT))
return perf_allow_kernel(&event->attr);
return perf_allow_kernel();

return 0;
}
Expand Down
3 changes: 2 additions & 1 deletion include/linux/lsm_hook_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ LSM_HOOK(int, 0, bpf_token_capable, const struct bpf_token *token, int cap)
LSM_HOOK(int, 0, locked_down, enum lockdown_reason what)

#ifdef CONFIG_PERF_EVENTS
LSM_HOOK(int, 0, perf_event_open, struct perf_event_attr *attr, int type)
LSM_HOOK(int, 0, perf_event_open, int type)
LSM_HOOK(int, 0, perf_event_alloc, struct perf_event *event)
LSM_HOOK(int, 0, perf_event_read, struct perf_event *event)
LSM_HOOK(int, 0, perf_event_write, struct perf_event *event)
Expand All @@ -455,6 +455,7 @@ LSM_HOOK(int, 0, perf_event_write, struct perf_event *event)
LSM_HOOK(int, 0, uring_override_creds, const struct cred *new)
LSM_HOOK(int, 0, uring_sqpoll, void)
LSM_HOOK(int, 0, uring_cmd, struct io_uring_cmd *ioucmd)
LSM_HOOK(int, 0, uring_allowed, void)
#endif /* CONFIG_IO_URING */

LSM_HOOK(void, LSM_RET_VOID, initramfs_populated, void)
Expand Down
10 changes: 5 additions & 5 deletions include/linux/perf_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -1672,22 +1672,22 @@ static inline int perf_is_paranoid(void)
return sysctl_perf_event_paranoid > -1;
}

int perf_allow_kernel(struct perf_event_attr *attr);
int perf_allow_kernel(void);

static inline int perf_allow_cpu(struct perf_event_attr *attr)
static inline int perf_allow_cpu(void)
{
if (sysctl_perf_event_paranoid > 0 && !perfmon_capable())
return -EACCES;

return security_perf_event_open(attr, PERF_SECURITY_CPU);
return security_perf_event_open(PERF_SECURITY_CPU);
}

static inline int perf_allow_tracepoint(struct perf_event_attr *attr)
static inline int perf_allow_tracepoint(void)
{
if (sysctl_perf_event_paranoid > -1 && !perfmon_capable())
return -EPERM;

return security_perf_event_open(attr, PERF_SECURITY_TRACEPOINT);
return security_perf_event_open(PERF_SECURITY_TRACEPOINT);
}

extern int perf_exclude_event(struct perf_event *event, struct pt_regs *regs);
Expand Down
10 changes: 7 additions & 3 deletions include/linux/security.h
Original file line number Diff line number Diff line change
Expand Up @@ -2324,14 +2324,13 @@ struct perf_event_attr;
struct perf_event;

#ifdef CONFIG_SECURITY
extern int security_perf_event_open(struct perf_event_attr *attr, int type);
extern int security_perf_event_open(int type);
extern int security_perf_event_alloc(struct perf_event *event);
extern void security_perf_event_free(struct perf_event *event);
extern int security_perf_event_read(struct perf_event *event);
extern int security_perf_event_write(struct perf_event *event);
#else
static inline int security_perf_event_open(struct perf_event_attr *attr,
int type)
static inline int security_perf_event_open(int type)
{
return 0;
}
Expand Down Expand Up @@ -2362,6 +2361,7 @@ static inline int security_perf_event_write(struct perf_event *event)
extern int security_uring_override_creds(const struct cred *new);
extern int security_uring_sqpoll(void);
extern int security_uring_cmd(struct io_uring_cmd *ioucmd);
extern int security_uring_allowed(void);
#else
static inline int security_uring_override_creds(const struct cred *new)
{
Expand All @@ -2375,6 +2375,10 @@ static inline int security_uring_cmd(struct io_uring_cmd *ioucmd)
{
return 0;
}
static inline int security_uring_allowed(void)
{
return 0;
}
#endif /* CONFIG_SECURITY */
#endif /* CONFIG_IO_URING */

Expand Down
21 changes: 14 additions & 7 deletions io_uring/io_uring.c
Original file line number Diff line number Diff line change
Expand Up @@ -3833,29 +3833,36 @@ static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
return io_uring_create(entries, &p, params);
}

static inline bool io_uring_allowed(void)
static inline int io_uring_allowed(void)
{
int disabled = READ_ONCE(sysctl_io_uring_disabled);
kgid_t io_uring_group;

if (disabled == 2)
return false;
return -EPERM;

if (disabled == 0 || capable(CAP_SYS_ADMIN))
return true;
goto allowed_lsm;

io_uring_group = make_kgid(&init_user_ns, sysctl_io_uring_group);
if (!gid_valid(io_uring_group))
return false;
return -EPERM;

if (!in_group_p(io_uring_group))
return -EPERM;

return in_group_p(io_uring_group);
allowed_lsm:
return security_uring_allowed();
}

SYSCALL_DEFINE2(io_uring_setup, u32, entries,
struct io_uring_params __user *, params)
{
if (!io_uring_allowed())
return -EPERM;
int ret;

ret = io_uring_allowed();
if (ret)
return ret;

return io_uring_setup(entries, params);
}
Expand Down
14 changes: 7 additions & 7 deletions kernel/events/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -4883,7 +4883,7 @@ find_get_context(struct task_struct *task, struct perf_event *event)

if (!task) {
/* Must be root to operate on a CPU event: */
err = perf_allow_cpu(&event->attr);
err = perf_allow_cpu();
if (err)
return ERR_PTR(err);

Expand Down Expand Up @@ -12565,7 +12565,7 @@ static int perf_copy_attr(struct perf_event_attr __user *uattr,
}
/* privileged levels capture (kernel, hv): check permissions */
if (mask & PERF_SAMPLE_BRANCH_PERM_PLM) {
ret = perf_allow_kernel(attr);
ret = perf_allow_kernel();
if (ret)
return ret;
}
Expand Down Expand Up @@ -12822,12 +12822,12 @@ SYSCALL_DEFINE5(perf_event_open,
return err;

/* Do we allow access to perf_event_open(2) ? */
err = security_perf_event_open(&attr, PERF_SECURITY_OPEN);
err = security_perf_event_open(PERF_SECURITY_OPEN);
if (err)
return err;

if (!attr.exclude_kernel) {
err = perf_allow_kernel(&attr);
err = perf_allow_kernel();
if (err)
return err;
}
Expand All @@ -12847,7 +12847,7 @@ SYSCALL_DEFINE5(perf_event_open,

/* Only privileged users can get physical addresses */
if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR)) {
err = perf_allow_kernel(&attr);
err = perf_allow_kernel();
if (err)
return err;
}
Expand Down Expand Up @@ -13680,12 +13680,12 @@ const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
return &event->attr;
}

int perf_allow_kernel(struct perf_event_attr *attr)
int perf_allow_kernel(void)
{
if (sysctl_perf_event_paranoid > 1 && !perfmon_capable())
return -EACCES;

return security_perf_event_open(attr, PERF_SECURITY_KERNEL);
return security_perf_event_open(PERF_SECURITY_KERNEL);
}
EXPORT_SYMBOL_GPL(perf_allow_kernel);

Expand Down
4 changes: 2 additions & 2 deletions kernel/trace/trace_event_perf.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static int perf_trace_event_perm(struct trace_event_call *tp_event,

/* The ftrace function trace is allowed only for root. */
if (ftrace_event_is_function(tp_event)) {
ret = perf_allow_tracepoint(&p_event->attr);
ret = perf_allow_tracepoint();
if (ret)
return ret;

Expand Down Expand Up @@ -86,7 +86,7 @@ static int perf_trace_event_perm(struct trace_event_call *tp_event,
* ...otherwise raw tracepoint data can be a severe data leak,
* only allow root to have these.
*/
ret = perf_allow_tracepoint(&p_event->attr);
ret = perf_allow_tracepoint();
if (ret)
return ret;

Expand Down
5 changes: 5 additions & 0 deletions rust/kernel/cred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ impl Credential {
///
/// The caller must ensure that `ptr` is valid and remains valid for the lifetime of the
/// returned [`Credential`] reference.
#[inline]
pub unsafe fn from_ptr<'a>(ptr: *const bindings::cred) -> &'a Credential {
// SAFETY: The safety requirements guarantee the validity of the dereference, while the
// `Credential` type being transparent makes the cast ok.
unsafe { &*ptr.cast() }
}

/// Get the id for this security context.
#[inline]
pub fn get_secid(&self) -> u32 {
let mut secid = 0;
// SAFETY: The invariants of this type ensures that the pointer is valid.
Expand All @@ -62,6 +64,7 @@ impl Credential {
}

/// Returns the effective UID of the given credential.
#[inline]
pub fn euid(&self) -> Kuid {
// SAFETY: By the type invariant, we know that `self.0` is valid. Furthermore, the `euid`
// field of a credential is never changed after initialization, so there is no potential
Expand All @@ -72,11 +75,13 @@ impl Credential {

// SAFETY: The type invariants guarantee that `Credential` is always ref-counted.
unsafe impl AlwaysRefCounted for Credential {
#[inline]
fn inc_ref(&self) {
// SAFETY: The existence of a shared reference means that the refcount is nonzero.
unsafe { bindings::get_cred(self.0.get()) };
}

#[inline]
unsafe fn dec_ref(obj: core::ptr::NonNull<Credential>) {
// SAFETY: The safety requirements guarantee that the refcount is nonzero. The cast is okay
// because `Credential` has the same representation as `struct cred`.
Expand Down
12 changes: 8 additions & 4 deletions rust/kernel/security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ use crate::{
/// # Invariants
///
/// The `ctx` field corresponds to a valid security context as returned by a successful call to
/// `security_secid_to_secctx`, that has not yet been destroyed by `security_release_secctx`.
/// `security_secid_to_secctx`, that has not yet been released by `security_release_secctx`.
pub struct SecurityCtx {
ctx: bindings::lsm_context,
}

impl SecurityCtx {
/// Get the security context given its id.
#[inline]
pub fn from_secid(secid: u32) -> Result<Self> {
// SAFETY: `struct lsm_context` can be initialized to all zeros.
let mut ctx: bindings::lsm_context = unsafe { core::mem::zeroed() };
Expand All @@ -35,16 +36,19 @@ impl SecurityCtx {
}

/// Returns whether the security context is empty.
#[inline]
pub fn is_empty(&self) -> bool {
self.ctx.len == 0
}

/// Returns the length of this security context.
#[inline]
pub fn len(&self) -> usize {
self.ctx.len as usize
}

/// Returns the bytes for this security context.
#[inline]
pub fn as_bytes(&self) -> &[u8] {
let ptr = self.ctx.context;
if ptr.is_null() {
Expand All @@ -61,10 +65,10 @@ impl SecurityCtx {
}

impl Drop for SecurityCtx {
#[inline]
fn drop(&mut self) {
// SAFETY: By the invariant of `Self`, this frees a context that came from a successful
// call to `security_secid_to_secctx` and has not yet been destroyed by
// `security_release_secctx`.
// SAFETY: By the invariant of `Self`, this releases an lsm context that came from a
// successful call to `security_secid_to_secctx` and has not yet been released.
unsafe { bindings::security_release_secctx(&mut self.ctx) };
}
}
17 changes: 14 additions & 3 deletions security/security.c
Original file line number Diff line number Diff line change
Expand Up @@ -5883,16 +5883,15 @@ EXPORT_SYMBOL(security_bdev_setintegrity);
#ifdef CONFIG_PERF_EVENTS
/**
* security_perf_event_open() - Check if a perf event open is allowed
* @attr: perf event attribute
* @type: type of event
*
* Check whether the @type of perf_event_open syscall is allowed.
*
* Return: Returns 0 if permission is granted.
*/
int security_perf_event_open(struct perf_event_attr *attr, int type)
int security_perf_event_open(int type)
{
return call_int_hook(perf_event_open, attr, type);
return call_int_hook(perf_event_open, type);
}

/**
Expand Down Expand Up @@ -5999,6 +5998,18 @@ int security_uring_cmd(struct io_uring_cmd *ioucmd)
{
return call_int_hook(uring_cmd, ioucmd);
}

/**
* security_uring_allowed() - Check if io_uring_setup() is allowed
*
* Check whether the current task is allowed to call io_uring_setup().
*
* Return: Returns 0 if permission is granted.
*/
int security_uring_allowed(void)
{
return call_int_hook(uring_allowed);
}
#endif /* CONFIG_IO_URING */

/**
Expand Down
Loading

0 comments on commit 18897e4

Please sign in to comment.