1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * AppArmor security module 4 * 5 * This file contains AppArmor /proc/<pid>/attr/ interface functions 6 * 7 * Copyright (C) 1998-2008 Novell/SUSE 8 * Copyright 2009-2010 Canonical Ltd. 9 */ 10 11 #include "include/apparmor.h" 12 #include "include/cred.h" 13 #include "include/policy.h" 14 #include "include/policy_ns.h" 15 #include "include/domain.h" 16 #include "include/procattr.h" 17 18 19 /** 20 * aa_getprocattr - Return the profile information for @profile 21 * @profile: the profile to print profile info about (NOT NULL) 22 * @string: Returns - string containing the profile info (NOT NULL) 23 * 24 * Requires: profile != NULL 25 * 26 * Creates a string containing the namespace_name://profile_name for 27 * @profile. 28 * 29 * Returns: size of string placed in @string else error code on failure 30 */ 31 int aa_getprocattr(struct aa_label *label, char **string) 32 { 33 struct aa_ns *ns = labels_ns(label); 34 struct aa_ns *current_ns = aa_get_current_ns(); 35 int len; 36 37 if (!aa_ns_visible(current_ns, ns, true)) { 38 aa_put_ns(current_ns); 39 return -EACCES; 40 } 41 42 len = aa_label_snxprint(NULL, 0, current_ns, label, 43 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS | 44 FLAG_HIDDEN_UNCONFINED); 45 AA_BUG(len < 0); 46 47 *string = kmalloc(len + 2, GFP_KERNEL); 48 if (!*string) { 49 aa_put_ns(current_ns); 50 return -ENOMEM; 51 } 52 53 len = aa_label_snxprint(*string, len + 2, current_ns, label, 54 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS | 55 FLAG_HIDDEN_UNCONFINED); 56 if (len < 0) { 57 aa_put_ns(current_ns); 58 return len; 59 } 60 61 (*string)[len] = '\n'; 62 (*string)[len + 1] = 0; 63 64 aa_put_ns(current_ns); 65 return len + 1; 66 } 67 68 /** 69 * split_token_from_name - separate a string of form <token>^<name> 70 * @op: operation being checked 71 * @args: string to parse (NOT NULL) 72 * @token: stores returned parsed token value (NOT NULL) 73 * 74 * Returns: start position of name after token else NULL on failure 75 */ 76 static char *split_token_from_name(const char *op, char *args, u64 *token) 77 { 78 char *name; 79 80 *token = simple_strtoull(args, &name, 16); 81 if ((name == args) || *name != '^') { 82 AA_ERROR("%s: Invalid input '%s'", op, args); 83 return ERR_PTR(-EINVAL); 84 } 85 86 name++; /* skip ^ */ 87 if (!*name) 88 name = NULL; 89 return name; 90 } 91 92 /** 93 * aa_setprocattr_changehat - handle procattr interface to change_hat 94 * @args: args received from writing to /proc/<pid>/attr/current (NOT NULL) 95 * @size: size of the args 96 * @flags: set of flags governing behavior 97 * 98 * Returns: %0 or error code if change_hat fails 99 */ 100 int aa_setprocattr_changehat(char *args, size_t size, int flags) 101 { 102 char *hat; 103 u64 token; 104 const char *hats[16]; /* current hard limit on # of names */ 105 int count = 0; 106 107 hat = split_token_from_name(OP_CHANGE_HAT, args, &token); 108 if (IS_ERR(hat)) 109 return PTR_ERR(hat); 110 111 if (!hat && !token) { 112 AA_ERROR("change_hat: Invalid input, NULL hat and NULL magic"); 113 return -EINVAL; 114 } 115 116 if (hat) { 117 /* set up hat name vector, args guaranteed null terminated 118 * at args[size] by setprocattr. 119 * 120 * If there are multiple hat names in the buffer each is 121 * separated by a \0. Ie. userspace writes them pre tokenized 122 */ 123 char *end = args + size; 124 for (count = 0; (hat < end) && count < 16; ++count) { 125 char *next = hat + strlen(hat) + 1; 126 hats[count] = hat; 127 AA_DEBUG("%s: (pid %d) Magic 0x%llx count %d hat '%s'\n" 128 , __func__, current->pid, token, count, hat); 129 hat = next; 130 } 131 } else 132 AA_DEBUG("%s: (pid %d) Magic 0x%llx count %d Hat '%s'\n", 133 __func__, current->pid, token, count, "<NULL>"); 134 135 return aa_change_hat(hats, count, token, flags); 136 } 137