Path: blob/master/security/integrity/ima/ima_policy.c
10818 views
/*1* Copyright (C) 2008 IBM Corporation2* Author: Mimi Zohar <[email protected]>3*4* This program is free software; you can redistribute it and/or modify5* it under the terms of the GNU General Public License as published by6* the Free Software Foundation, version 2 of the License.7*8* ima_policy.c9* - initialize default measure policy rules10*11*/12#include <linux/module.h>13#include <linux/list.h>14#include <linux/security.h>15#include <linux/magic.h>16#include <linux/parser.h>17#include <linux/slab.h>1819#include "ima.h"2021/* flags definitions */22#define IMA_FUNC 0x000123#define IMA_MASK 0x000224#define IMA_FSMAGIC 0x000425#define IMA_UID 0x00082627enum ima_action { UNKNOWN = -1, DONT_MEASURE = 0, MEASURE };2829#define MAX_LSM_RULES 630enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,31LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE32};3334struct ima_measure_rule_entry {35struct list_head list;36enum ima_action action;37unsigned int flags;38enum ima_hooks func;39int mask;40unsigned long fsmagic;41uid_t uid;42struct {43void *rule; /* LSM file metadata specific */44int type; /* audit type */45} lsm[MAX_LSM_RULES];46};4748/*49* Without LSM specific knowledge, the default policy can only be50* written in terms of .action, .func, .mask, .fsmagic, and .uid51*/5253/*54* The minimum rule set to allow for full TCB coverage. Measures all files55* opened or mmap for exec and everything read by root. Dangerous because56* normal users can easily run the machine out of memory simply building57* and running executables.58*/59static struct ima_measure_rule_entry default_rules[] = {60{.action = DONT_MEASURE,.fsmagic = PROC_SUPER_MAGIC,.flags = IMA_FSMAGIC},61{.action = DONT_MEASURE,.fsmagic = SYSFS_MAGIC,.flags = IMA_FSMAGIC},62{.action = DONT_MEASURE,.fsmagic = DEBUGFS_MAGIC,.flags = IMA_FSMAGIC},63{.action = DONT_MEASURE,.fsmagic = TMPFS_MAGIC,.flags = IMA_FSMAGIC},64{.action = DONT_MEASURE,.fsmagic = SECURITYFS_MAGIC,.flags = IMA_FSMAGIC},65{.action = DONT_MEASURE,.fsmagic = SELINUX_MAGIC,.flags = IMA_FSMAGIC},66{.action = MEASURE,.func = FILE_MMAP,.mask = MAY_EXEC,67.flags = IMA_FUNC | IMA_MASK},68{.action = MEASURE,.func = BPRM_CHECK,.mask = MAY_EXEC,69.flags = IMA_FUNC | IMA_MASK},70{.action = MEASURE,.func = FILE_CHECK,.mask = MAY_READ,.uid = 0,71.flags = IMA_FUNC | IMA_MASK | IMA_UID},72};7374static LIST_HEAD(measure_default_rules);75static LIST_HEAD(measure_policy_rules);76static struct list_head *ima_measure;7778static DEFINE_MUTEX(ima_measure_mutex);7980static bool ima_use_tcb __initdata;81static int __init default_policy_setup(char *str)82{83ima_use_tcb = 1;84return 1;85}86__setup("ima_tcb", default_policy_setup);8788/**89* ima_match_rules - determine whether an inode matches the measure rule.90* @rule: a pointer to a rule91* @inode: a pointer to an inode92* @func: LIM hook identifier93* @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)94*95* Returns true on rule match, false on failure.96*/97static bool ima_match_rules(struct ima_measure_rule_entry *rule,98struct inode *inode, enum ima_hooks func, int mask)99{100struct task_struct *tsk = current;101int i;102103if ((rule->flags & IMA_FUNC) && rule->func != func)104return false;105if ((rule->flags & IMA_MASK) && rule->mask != mask)106return false;107if ((rule->flags & IMA_FSMAGIC)108&& rule->fsmagic != inode->i_sb->s_magic)109return false;110if ((rule->flags & IMA_UID) && rule->uid != tsk->cred->uid)111return false;112for (i = 0; i < MAX_LSM_RULES; i++) {113int rc = 0;114u32 osid, sid;115116if (!rule->lsm[i].rule)117continue;118119switch (i) {120case LSM_OBJ_USER:121case LSM_OBJ_ROLE:122case LSM_OBJ_TYPE:123security_inode_getsecid(inode, &osid);124rc = security_filter_rule_match(osid,125rule->lsm[i].type,126Audit_equal,127rule->lsm[i].rule,128NULL);129break;130case LSM_SUBJ_USER:131case LSM_SUBJ_ROLE:132case LSM_SUBJ_TYPE:133security_task_getsecid(tsk, &sid);134rc = security_filter_rule_match(sid,135rule->lsm[i].type,136Audit_equal,137rule->lsm[i].rule,138NULL);139default:140break;141}142if (!rc)143return false;144}145return true;146}147148/**149* ima_match_policy - decision based on LSM and other conditions150* @inode: pointer to an inode for which the policy decision is being made151* @func: IMA hook identifier152* @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)153*154* Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)155* conditions.156*157* (There is no need for locking when walking the policy list,158* as elements in the list are never deleted, nor does the list159* change.)160*/161int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask)162{163struct ima_measure_rule_entry *entry;164165list_for_each_entry(entry, ima_measure, list) {166bool rc;167168rc = ima_match_rules(entry, inode, func, mask);169if (rc)170return entry->action;171}172return 0;173}174175/**176* ima_init_policy - initialize the default measure rules.177*178* ima_measure points to either the measure_default_rules or the179* the new measure_policy_rules.180*/181void __init ima_init_policy(void)182{183int i, entries;184185/* if !ima_use_tcb set entries = 0 so we load NO default rules */186if (ima_use_tcb)187entries = ARRAY_SIZE(default_rules);188else189entries = 0;190191for (i = 0; i < entries; i++)192list_add_tail(&default_rules[i].list, &measure_default_rules);193ima_measure = &measure_default_rules;194}195196/**197* ima_update_policy - update default_rules with new measure rules198*199* Called on file .release to update the default rules with a complete new200* policy. Once updated, the policy is locked, no additional rules can be201* added to the policy.202*/203void ima_update_policy(void)204{205const char *op = "policy_update";206const char *cause = "already exists";207int result = 1;208int audit_info = 0;209210if (ima_measure == &measure_default_rules) {211ima_measure = &measure_policy_rules;212cause = "complete";213result = 0;214}215integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,216NULL, op, cause, result, audit_info);217}218219enum {220Opt_err = -1,221Opt_measure = 1, Opt_dont_measure,222Opt_obj_user, Opt_obj_role, Opt_obj_type,223Opt_subj_user, Opt_subj_role, Opt_subj_type,224Opt_func, Opt_mask, Opt_fsmagic, Opt_uid225};226227static match_table_t policy_tokens = {228{Opt_measure, "measure"},229{Opt_dont_measure, "dont_measure"},230{Opt_obj_user, "obj_user=%s"},231{Opt_obj_role, "obj_role=%s"},232{Opt_obj_type, "obj_type=%s"},233{Opt_subj_user, "subj_user=%s"},234{Opt_subj_role, "subj_role=%s"},235{Opt_subj_type, "subj_type=%s"},236{Opt_func, "func=%s"},237{Opt_mask, "mask=%s"},238{Opt_fsmagic, "fsmagic=%s"},239{Opt_uid, "uid=%s"},240{Opt_err, NULL}241};242243static int ima_lsm_rule_init(struct ima_measure_rule_entry *entry,244char *args, int lsm_rule, int audit_type)245{246int result;247248if (entry->lsm[lsm_rule].rule)249return -EINVAL;250251entry->lsm[lsm_rule].type = audit_type;252result = security_filter_rule_init(entry->lsm[lsm_rule].type,253Audit_equal, args,254&entry->lsm[lsm_rule].rule);255if (!entry->lsm[lsm_rule].rule)256return -EINVAL;257return result;258}259260static void ima_log_string(struct audit_buffer *ab, char *key, char *value)261{262audit_log_format(ab, "%s=", key);263audit_log_untrustedstring(ab, value);264audit_log_format(ab, " ");265}266267static int ima_parse_rule(char *rule, struct ima_measure_rule_entry *entry)268{269struct audit_buffer *ab;270char *p;271int result = 0;272273ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);274275entry->uid = -1;276entry->action = UNKNOWN;277while ((p = strsep(&rule, " \t")) != NULL) {278substring_t args[MAX_OPT_ARGS];279int token;280unsigned long lnum;281282if (result < 0)283break;284if ((*p == '\0') || (*p == ' ') || (*p == '\t'))285continue;286token = match_token(p, policy_tokens, args);287switch (token) {288case Opt_measure:289ima_log_string(ab, "action", "measure");290291if (entry->action != UNKNOWN)292result = -EINVAL;293294entry->action = MEASURE;295break;296case Opt_dont_measure:297ima_log_string(ab, "action", "dont_measure");298299if (entry->action != UNKNOWN)300result = -EINVAL;301302entry->action = DONT_MEASURE;303break;304case Opt_func:305ima_log_string(ab, "func", args[0].from);306307if (entry->func)308result = -EINVAL;309310if (strcmp(args[0].from, "FILE_CHECK") == 0)311entry->func = FILE_CHECK;312/* PATH_CHECK is for backwards compat */313else if (strcmp(args[0].from, "PATH_CHECK") == 0)314entry->func = FILE_CHECK;315else if (strcmp(args[0].from, "FILE_MMAP") == 0)316entry->func = FILE_MMAP;317else if (strcmp(args[0].from, "BPRM_CHECK") == 0)318entry->func = BPRM_CHECK;319else320result = -EINVAL;321if (!result)322entry->flags |= IMA_FUNC;323break;324case Opt_mask:325ima_log_string(ab, "mask", args[0].from);326327if (entry->mask)328result = -EINVAL;329330if ((strcmp(args[0].from, "MAY_EXEC")) == 0)331entry->mask = MAY_EXEC;332else if (strcmp(args[0].from, "MAY_WRITE") == 0)333entry->mask = MAY_WRITE;334else if (strcmp(args[0].from, "MAY_READ") == 0)335entry->mask = MAY_READ;336else if (strcmp(args[0].from, "MAY_APPEND") == 0)337entry->mask = MAY_APPEND;338else339result = -EINVAL;340if (!result)341entry->flags |= IMA_MASK;342break;343case Opt_fsmagic:344ima_log_string(ab, "fsmagic", args[0].from);345346if (entry->fsmagic) {347result = -EINVAL;348break;349}350351result = strict_strtoul(args[0].from, 16,352&entry->fsmagic);353if (!result)354entry->flags |= IMA_FSMAGIC;355break;356case Opt_uid:357ima_log_string(ab, "uid", args[0].from);358359if (entry->uid != -1) {360result = -EINVAL;361break;362}363364result = strict_strtoul(args[0].from, 10, &lnum);365if (!result) {366entry->uid = (uid_t) lnum;367if (entry->uid != lnum)368result = -EINVAL;369else370entry->flags |= IMA_UID;371}372break;373case Opt_obj_user:374ima_log_string(ab, "obj_user", args[0].from);375result = ima_lsm_rule_init(entry, args[0].from,376LSM_OBJ_USER,377AUDIT_OBJ_USER);378break;379case Opt_obj_role:380ima_log_string(ab, "obj_role", args[0].from);381result = ima_lsm_rule_init(entry, args[0].from,382LSM_OBJ_ROLE,383AUDIT_OBJ_ROLE);384break;385case Opt_obj_type:386ima_log_string(ab, "obj_type", args[0].from);387result = ima_lsm_rule_init(entry, args[0].from,388LSM_OBJ_TYPE,389AUDIT_OBJ_TYPE);390break;391case Opt_subj_user:392ima_log_string(ab, "subj_user", args[0].from);393result = ima_lsm_rule_init(entry, args[0].from,394LSM_SUBJ_USER,395AUDIT_SUBJ_USER);396break;397case Opt_subj_role:398ima_log_string(ab, "subj_role", args[0].from);399result = ima_lsm_rule_init(entry, args[0].from,400LSM_SUBJ_ROLE,401AUDIT_SUBJ_ROLE);402break;403case Opt_subj_type:404ima_log_string(ab, "subj_type", args[0].from);405result = ima_lsm_rule_init(entry, args[0].from,406LSM_SUBJ_TYPE,407AUDIT_SUBJ_TYPE);408break;409case Opt_err:410ima_log_string(ab, "UNKNOWN", p);411result = -EINVAL;412break;413}414}415if (!result && (entry->action == UNKNOWN))416result = -EINVAL;417418audit_log_format(ab, "res=%d", !!result);419audit_log_end(ab);420return result;421}422423/**424* ima_parse_add_rule - add a rule to measure_policy_rules425* @rule - ima measurement policy rule426*427* Uses a mutex to protect the policy list from multiple concurrent writers.428* Returns the length of the rule parsed, an error code on failure429*/430ssize_t ima_parse_add_rule(char *rule)431{432const char *op = "update_policy";433char *p;434struct ima_measure_rule_entry *entry;435ssize_t result, len;436int audit_info = 0;437438/* Prevent installed policy from changing */439if (ima_measure != &measure_default_rules) {440integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,441NULL, op, "already exists",442-EACCES, audit_info);443return -EACCES;444}445446entry = kzalloc(sizeof(*entry), GFP_KERNEL);447if (!entry) {448integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,449NULL, op, "-ENOMEM", -ENOMEM, audit_info);450return -ENOMEM;451}452453INIT_LIST_HEAD(&entry->list);454455p = strsep(&rule, "\n");456len = strlen(p) + 1;457458if (*p == '#') {459kfree(entry);460return len;461}462463result = ima_parse_rule(p, entry);464if (result) {465kfree(entry);466integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,467NULL, op, "invalid policy", result,468audit_info);469return result;470}471472mutex_lock(&ima_measure_mutex);473list_add_tail(&entry->list, &measure_policy_rules);474mutex_unlock(&ima_measure_mutex);475476return len;477}478479/* ima_delete_rules called to cleanup invalid policy */480void ima_delete_rules(void)481{482struct ima_measure_rule_entry *entry, *tmp;483484mutex_lock(&ima_measure_mutex);485list_for_each_entry_safe(entry, tmp, &measure_policy_rules, list) {486list_del(&entry->list);487kfree(entry);488}489mutex_unlock(&ima_measure_mutex);490}491492493