Path: blob/main/sys/security/mac_priority/mac_priority.c
39476 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2021 Florian Walpen <[email protected]>4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <sys/param.h>28#include <sys/conf.h>29#include <sys/kernel.h>30#include <sys/module.h>31#include <sys/priv.h>32#include <sys/sysctl.h>33#include <sys/ucred.h>3435#include <security/mac/mac_policy.h>3637static SYSCTL_NODE(_security_mac, OID_AUTO, priority,38CTLFLAG_RW | CTLFLAG_MPSAFE, 0,39"mac_priority policy controls");4041static int realtime_enabled = 1;42SYSCTL_INT(_security_mac_priority, OID_AUTO, realtime, CTLFLAG_RWTUN,43&realtime_enabled, 0,44"Enable realtime priority scheduling for group realtime_gid");4546static int realtime_gid = GID_RT_PRIO;47SYSCTL_INT(_security_mac_priority, OID_AUTO, realtime_gid, CTLFLAG_RWTUN,48&realtime_gid, 0,49"Group id of the realtime privilege group");5051static int idletime_enabled = 1;52SYSCTL_INT(_security_mac_priority, OID_AUTO, idletime, CTLFLAG_RWTUN,53&idletime_enabled, 0,54"Enable idle priority scheduling for group idletime_gid");5556static int idletime_gid = GID_ID_PRIO;57SYSCTL_INT(_security_mac_priority, OID_AUTO, idletime_gid, CTLFLAG_RWTUN,58&idletime_gid, 0,59"Group id of the idletime privilege group");6061static int62priority_priv_grant(struct ucred *cred, int priv)63{64if ((priv == PRIV_SCHED_RTPRIO || priv == PRIV_SCHED_SETPOLICY) &&65realtime_enabled && groupmember(realtime_gid, cred))66return (0);6768if (priv == PRIV_SCHED_IDPRIO && idletime_enabled &&69groupmember(idletime_gid, cred))70return (0);7172return (EPERM);73}7475static struct mac_policy_ops priority_ops = {76.mpo_priv_grant = priority_priv_grant,77};7879MAC_POLICY_SET(&priority_ops, mac_priority, "MAC/priority",80MPC_LOADTIME_FLAG_UNLOADOK, NULL);818283