// SPDX-License-Identifier: GPL-2.01/*2* security/tomoyo/environ.c3*4* Copyright (C) 2005-2011 NTT DATA CORPORATION5*/67#include "common.h"89/**10* tomoyo_check_env_acl - Check permission for environment variable's name.11*12* @r: Pointer to "struct tomoyo_request_info".13* @ptr: Pointer to "struct tomoyo_acl_info".14*15* Returns true if granted, false otherwise.16*/17static bool tomoyo_check_env_acl(struct tomoyo_request_info *r,18const struct tomoyo_acl_info *ptr)19{20const struct tomoyo_env_acl *acl =21container_of(ptr, typeof(*acl), head);2223return tomoyo_path_matches_pattern(r->param.environ.name, acl->env);24}2526/**27* tomoyo_audit_env_log - Audit environment variable name log.28*29* @r: Pointer to "struct tomoyo_request_info".30*31* Returns 0 on success, negative value otherwise.32*/33static int tomoyo_audit_env_log(struct tomoyo_request_info *r)34{35return tomoyo_supervisor(r, "misc env %s\n",36r->param.environ.name->name);37}3839/**40* tomoyo_env_perm - Check permission for environment variable's name.41*42* @r: Pointer to "struct tomoyo_request_info".43* @env: The name of environment variable.44*45* Returns 0 on success, negative value otherwise.46*47* Caller holds tomoyo_read_lock().48*/49int tomoyo_env_perm(struct tomoyo_request_info *r, const char *env)50{51struct tomoyo_path_info environ;52int error;5354if (!env || !*env)55return 0;56environ.name = env;57tomoyo_fill_path_info(&environ);58r->param_type = TOMOYO_TYPE_ENV_ACL;59r->param.environ.name = &environ;60do {61tomoyo_check_acl(r, tomoyo_check_env_acl);62error = tomoyo_audit_env_log(r);63} while (error == TOMOYO_RETRY_REQUEST);64return error;65}6667/**68* tomoyo_same_env_acl - Check for duplicated "struct tomoyo_env_acl" entry.69*70* @a: Pointer to "struct tomoyo_acl_info".71* @b: Pointer to "struct tomoyo_acl_info".72*73* Returns true if @a == @b, false otherwise.74*/75static bool tomoyo_same_env_acl(const struct tomoyo_acl_info *a,76const struct tomoyo_acl_info *b)77{78const struct tomoyo_env_acl *p1 = container_of(a, typeof(*p1), head);79const struct tomoyo_env_acl *p2 = container_of(b, typeof(*p2), head);8081return p1->env == p2->env;82}8384/**85* tomoyo_write_env - Write "struct tomoyo_env_acl" list.86*87* @param: Pointer to "struct tomoyo_acl_param".88*89* Returns 0 on success, negative value otherwise.90*91* Caller holds tomoyo_read_lock().92*/93static int tomoyo_write_env(struct tomoyo_acl_param *param)94{95struct tomoyo_env_acl e = { .head.type = TOMOYO_TYPE_ENV_ACL };96int error = -ENOMEM;97const char *data = tomoyo_read_token(param);9899if (!tomoyo_correct_word(data) || strchr(data, '='))100return -EINVAL;101e.env = tomoyo_get_name(data);102if (!e.env)103return error;104error = tomoyo_update_domain(&e.head, sizeof(e), param,105tomoyo_same_env_acl, NULL);106tomoyo_put_name(e.env);107return error;108}109110/**111* tomoyo_write_misc - Update environment variable list.112*113* @param: Pointer to "struct tomoyo_acl_param".114*115* Returns 0 on success, negative value otherwise.116*/117int tomoyo_write_misc(struct tomoyo_acl_param *param)118{119if (tomoyo_str_starts(¶m->data, "env "))120return tomoyo_write_env(param);121return -EINVAL;122}123124125