1cdff2642SJohn Johansen /* 2cdff2642SJohn Johansen * AppArmor security module 3cdff2642SJohn Johansen * 4cdff2642SJohn Johansen * This file contains basic common functions used in AppArmor 5cdff2642SJohn Johansen * 6cdff2642SJohn Johansen * Copyright (C) 1998-2008 Novell/SUSE 7cdff2642SJohn Johansen * Copyright 2009-2010 Canonical Ltd. 8cdff2642SJohn Johansen * 9cdff2642SJohn Johansen * This program is free software; you can redistribute it and/or 10cdff2642SJohn Johansen * modify it under the terms of the GNU General Public License as 11cdff2642SJohn Johansen * published by the Free Software Foundation, version 2 of the 12cdff2642SJohn Johansen * License. 13cdff2642SJohn Johansen */ 14cdff2642SJohn Johansen 153b0aaf58SJohn Johansen #include <linux/ctype.h> 16b7f080cfSAlexey Dobriyan #include <linux/mm.h> 17cdff2642SJohn Johansen #include <linux/slab.h> 18cdff2642SJohn Johansen #include <linux/string.h> 19cdff2642SJohn Johansen #include <linux/vmalloc.h> 20cdff2642SJohn Johansen 21cdff2642SJohn Johansen #include "include/audit.h" 2232c3df63SJames Morris #include "include/apparmor.h" 2312557dcbSJohn Johansen #include "include/lib.h" 24fe6bb31fSJohn Johansen #include "include/policy.h" 25cdff2642SJohn Johansen 26cdff2642SJohn Johansen /** 27cdff2642SJohn Johansen * aa_split_fqname - split a fqname into a profile and namespace name 28cdff2642SJohn Johansen * @fqname: a full qualified name in namespace profile format (NOT NULL) 29cdff2642SJohn Johansen * @ns_name: pointer to portion of the string containing the ns name (NOT NULL) 30cdff2642SJohn Johansen * 31cdff2642SJohn Johansen * Returns: profile name or NULL if one is not specified 32cdff2642SJohn Johansen * 33cdff2642SJohn Johansen * Split a namespace name from a profile name (see policy.c for naming 34cdff2642SJohn Johansen * description). If a portion of the name is missing it returns NULL for 35cdff2642SJohn Johansen * that portion. 36cdff2642SJohn Johansen * 37cdff2642SJohn Johansen * NOTE: may modify the @fqname string. The pointers returned point 38cdff2642SJohn Johansen * into the @fqname string. 39cdff2642SJohn Johansen */ 40cdff2642SJohn Johansen char *aa_split_fqname(char *fqname, char **ns_name) 41cdff2642SJohn Johansen { 42cdff2642SJohn Johansen char *name = strim(fqname); 43cdff2642SJohn Johansen 44cdff2642SJohn Johansen *ns_name = NULL; 45cdff2642SJohn Johansen if (name[0] == ':') { 46cdff2642SJohn Johansen char *split = strchr(&name[1], ':'); 4704ccd53fSJohn Johansen *ns_name = skip_spaces(&name[1]); 48cdff2642SJohn Johansen if (split) { 49cdff2642SJohn Johansen /* overwrite ':' with \0 */ 502654bfbcSJohn Johansen *split++ = 0; 512654bfbcSJohn Johansen if (strncmp(split, "//", 2) == 0) 522654bfbcSJohn Johansen split += 2; 532654bfbcSJohn Johansen name = skip_spaces(split); 54cdff2642SJohn Johansen } else 55cdff2642SJohn Johansen /* a ns name without a following profile is allowed */ 56cdff2642SJohn Johansen name = NULL; 57cdff2642SJohn Johansen } 58cdff2642SJohn Johansen if (name && *name == 0) 59cdff2642SJohn Johansen name = NULL; 60cdff2642SJohn Johansen 61cdff2642SJohn Johansen return name; 62cdff2642SJohn Johansen } 63cdff2642SJohn Johansen 64cdff2642SJohn Johansen /** 653b0aaf58SJohn Johansen * skipn_spaces - Removes leading whitespace from @str. 663b0aaf58SJohn Johansen * @str: The string to be stripped. 673b0aaf58SJohn Johansen * 683b0aaf58SJohn Johansen * Returns a pointer to the first non-whitespace character in @str. 693b0aaf58SJohn Johansen * if all whitespace will return NULL 703b0aaf58SJohn Johansen */ 713b0aaf58SJohn Johansen 72*b91deb9dSJohn Johansen const char *skipn_spaces(const char *str, size_t n) 733b0aaf58SJohn Johansen { 743b0aaf58SJohn Johansen for (; n && isspace(*str); --n) 753b0aaf58SJohn Johansen ++str; 763b0aaf58SJohn Johansen if (n) 773b0aaf58SJohn Johansen return (char *)str; 783b0aaf58SJohn Johansen return NULL; 793b0aaf58SJohn Johansen } 803b0aaf58SJohn Johansen 813b0aaf58SJohn Johansen const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name, 823b0aaf58SJohn Johansen size_t *ns_len) 833b0aaf58SJohn Johansen { 843b0aaf58SJohn Johansen const char *end = fqname + n; 853b0aaf58SJohn Johansen const char *name = skipn_spaces(fqname, n); 863b0aaf58SJohn Johansen 873b0aaf58SJohn Johansen if (!name) 883b0aaf58SJohn Johansen return NULL; 893b0aaf58SJohn Johansen *ns_name = NULL; 903b0aaf58SJohn Johansen *ns_len = 0; 913b0aaf58SJohn Johansen if (name[0] == ':') { 923b0aaf58SJohn Johansen char *split = strnchr(&name[1], end - &name[1], ':'); 933b0aaf58SJohn Johansen *ns_name = skipn_spaces(&name[1], end - &name[1]); 943b0aaf58SJohn Johansen if (!*ns_name) 953b0aaf58SJohn Johansen return NULL; 963b0aaf58SJohn Johansen if (split) { 973b0aaf58SJohn Johansen *ns_len = split - *ns_name; 983b0aaf58SJohn Johansen if (*ns_len == 0) 993b0aaf58SJohn Johansen *ns_name = NULL; 1003b0aaf58SJohn Johansen split++; 1013b0aaf58SJohn Johansen if (end - split > 1 && strncmp(split, "//", 2) == 0) 1023b0aaf58SJohn Johansen split += 2; 1033b0aaf58SJohn Johansen name = skipn_spaces(split, end - split); 1043b0aaf58SJohn Johansen } else { 1053b0aaf58SJohn Johansen /* a ns name without a following profile is allowed */ 1063b0aaf58SJohn Johansen name = NULL; 1073b0aaf58SJohn Johansen *ns_len = end - *ns_name; 1083b0aaf58SJohn Johansen } 1093b0aaf58SJohn Johansen } 1103b0aaf58SJohn Johansen if (name && *name == 0) 1113b0aaf58SJohn Johansen name = NULL; 1123b0aaf58SJohn Johansen 1133b0aaf58SJohn Johansen return name; 1143b0aaf58SJohn Johansen } 1153b0aaf58SJohn Johansen 1163b0aaf58SJohn Johansen /** 117cdff2642SJohn Johansen * aa_info_message - log a none profile related status message 118cdff2642SJohn Johansen * @str: message to log 119cdff2642SJohn Johansen */ 120cdff2642SJohn Johansen void aa_info_message(const char *str) 121cdff2642SJohn Johansen { 122cdff2642SJohn Johansen if (audit_enabled) { 123ef88a7acSJohn Johansen DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, NULL); 124ef88a7acSJohn Johansen 125ef88a7acSJohn Johansen aad(&sa)->info = str; 126cdff2642SJohn Johansen aa_audit_msg(AUDIT_APPARMOR_STATUS, &sa, NULL); 127cdff2642SJohn Johansen } 128cdff2642SJohn Johansen printk(KERN_INFO "AppArmor: %s\n", str); 129cdff2642SJohn Johansen } 130cdff2642SJohn Johansen 131cdff2642SJohn Johansen /** 132fe6bb31fSJohn Johansen * aa_policy_init - initialize a policy structure 133fe6bb31fSJohn Johansen * @policy: policy to initialize (NOT NULL) 134fe6bb31fSJohn Johansen * @prefix: prefix name if any is required. (MAYBE NULL) 135fe6bb31fSJohn Johansen * @name: name of the policy, init will make a copy of it (NOT NULL) 136fe6bb31fSJohn Johansen * 137fe6bb31fSJohn Johansen * Note: this fn creates a copy of strings passed in 138fe6bb31fSJohn Johansen * 139fe6bb31fSJohn Johansen * Returns: true if policy init successful 140fe6bb31fSJohn Johansen */ 141fe6bb31fSJohn Johansen bool aa_policy_init(struct aa_policy *policy, const char *prefix, 142d102d895SJohn Johansen const char *name, gfp_t gfp) 143fe6bb31fSJohn Johansen { 144fe6bb31fSJohn Johansen /* freed by policy_free */ 145fe6bb31fSJohn Johansen if (prefix) { 146fe6bb31fSJohn Johansen policy->hname = kmalloc(strlen(prefix) + strlen(name) + 3, 147d102d895SJohn Johansen gfp); 148fe6bb31fSJohn Johansen if (policy->hname) 149bbe4a7c8SJohn Johansen sprintf((char *)policy->hname, "%s//%s", prefix, name); 150fe6bb31fSJohn Johansen } else 151d102d895SJohn Johansen policy->hname = kstrdup(name, gfp); 152fe6bb31fSJohn Johansen if (!policy->hname) 153b9c42ac7Skbuild test robot return false; 154fe6bb31fSJohn Johansen /* base.name is a substring of fqname */ 155d102d895SJohn Johansen policy->name = basename(policy->hname); 156fe6bb31fSJohn Johansen INIT_LIST_HEAD(&policy->list); 157fe6bb31fSJohn Johansen INIT_LIST_HEAD(&policy->profiles); 158fe6bb31fSJohn Johansen 159b9c42ac7Skbuild test robot return true; 160fe6bb31fSJohn Johansen } 161fe6bb31fSJohn Johansen 162fe6bb31fSJohn Johansen /** 163fe6bb31fSJohn Johansen * aa_policy_destroy - free the elements referenced by @policy 164fe6bb31fSJohn Johansen * @policy: policy that is to have its elements freed (NOT NULL) 165fe6bb31fSJohn Johansen */ 166fe6bb31fSJohn Johansen void aa_policy_destroy(struct aa_policy *policy) 167fe6bb31fSJohn Johansen { 1685fd1b95fSJohn Johansen AA_BUG(on_list_rcu(&policy->profiles)); 1695fd1b95fSJohn Johansen AA_BUG(on_list_rcu(&policy->list)); 170fe6bb31fSJohn Johansen 171fe6bb31fSJohn Johansen /* don't free name as its a subset of hname */ 172fe6bb31fSJohn Johansen kzfree(policy->hname); 173fe6bb31fSJohn Johansen } 174