// SPDX-License-Identifier: GPL-2.0-only1/*2* mmap based event notifications for SELinux3*4* Author: KaiGai Kohei <[email protected]>5*6* Copyright (C) 2010 NEC corporation7*/8#include <linux/kernel.h>9#include <linux/gfp.h>10#include <linux/mm.h>11#include <linux/mutex.h>12#include "avc.h"13#include "security.h"1415/*16* The selinux_status_page shall be exposed to userspace applications17* using mmap interface on /selinux/status.18* It enables to notify applications a few events that will cause reset19* of userspace access vector without context switching.20*21* The selinux_kernel_status structure on the head of status page is22* protected from concurrent accesses using seqlock logic, so userspace23* application should reference the status page according to the seqlock24* logic.25*26* Typically, application checks status->sequence at the head of access27* control routine. If it is odd-number, kernel is updating the status,28* so please wait for a moment. If it is changed from the last sequence29* number, it means something happen, so application will reset userspace30* avc, if needed.31* In most cases, application shall confirm the kernel status is not32* changed without any system call invocations.33*/3435/*36* selinux_kernel_status_page37*38* It returns a reference to selinux_status_page. If the status page is39* not allocated yet, it also tries to allocate it at the first time.40*/41struct page *selinux_kernel_status_page(void)42{43struct selinux_kernel_status *status;44struct page *result = NULL;4546mutex_lock(&selinux_state.status_lock);47if (!selinux_state.status_page) {48selinux_state.status_page = alloc_page(GFP_KERNEL|__GFP_ZERO);4950if (selinux_state.status_page) {51status = page_address(selinux_state.status_page);5253status->version = SELINUX_KERNEL_STATUS_VERSION;54status->sequence = 0;55status->enforcing = enforcing_enabled();56/*57* NOTE: the next policyload event shall set58* a positive value on the status->policyload,59* although it may not be 1, but never zero.60* So, application can know it was updated.61*/62status->policyload = 0;63status->deny_unknown =64!security_get_allow_unknown();65}66}67result = selinux_state.status_page;68mutex_unlock(&selinux_state.status_lock);6970return result;71}7273/*74* selinux_status_update_setenforce75*76* It updates status of the current enforcing/permissive mode.77*/78void selinux_status_update_setenforce(bool enforcing)79{80struct selinux_kernel_status *status;8182mutex_lock(&selinux_state.status_lock);83if (selinux_state.status_page) {84status = page_address(selinux_state.status_page);8586status->sequence++;87smp_wmb();8889status->enforcing = enforcing ? 1 : 0;9091smp_wmb();92status->sequence++;93}94mutex_unlock(&selinux_state.status_lock);95}9697/*98* selinux_status_update_policyload99*100* It updates status of the times of policy reloaded, and current101* setting of deny_unknown.102*/103void selinux_status_update_policyload(u32 seqno)104{105struct selinux_kernel_status *status;106107mutex_lock(&selinux_state.status_lock);108if (selinux_state.status_page) {109status = page_address(selinux_state.status_page);110111status->sequence++;112smp_wmb();113114status->policyload = seqno;115status->deny_unknown = !security_get_allow_unknown();116117smp_wmb();118status->sequence++;119}120mutex_unlock(&selinux_state.status_lock);121}122123124