2 * AppArmor security module
4 * This file contains AppArmor /proc/<pid>/attr/ interface functions
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation, version 2 of the
15 #include "include/apparmor.h"
16 #include "include/context.h"
17 #include "include/policy.h"
18 #include "include/policy_ns.h"
19 #include "include/domain.h"
20 #include "include/procattr.h"
24 * aa_getprocattr - Return the profile information for @profile
25 * @profile: the profile to print profile info about (NOT NULL)
26 * @string: Returns - string containing the profile info (NOT NULL)
28 * Returns: length of @string on success else error on failure
30 * Requires: profile != NULL
32 * Creates a string containing the namespace_name://profile_name for
35 * Returns: size of string placed in @string else error code on failure
37 int aa_getprocattr(struct aa_profile *profile, char **string)
40 int len = 0, mode_len = 0, ns_len = 0, name_len;
41 const char *mode_str = aa_profile_mode_names[profile->mode];
42 const char *ns_name = NULL;
43 struct aa_ns *ns = profile->ns;
44 struct aa_ns *current_ns = aa_get_current_ns();
47 if (!aa_ns_visible(current_ns, ns, true))
50 ns_name = aa_ns_name(current_ns, ns, true);
51 ns_len = strlen(ns_name);
53 /* if the visible ns_name is > 0 increase size for : :// seperator */
57 /* unconfined profiles don't have a mode string appended */
58 if (!unconfined(profile))
59 mode_len = strlen(mode_str) + 3; /* + 3 for _() */
61 name_len = strlen(profile->base.hname);
62 len = mode_len + ns_len + name_len + 1; /* + 1 for \n */
63 s = str = kmalloc(len + 1, GFP_KERNEL); /* + 1 \0 */
68 /* skip over prefix current_ns->base.hname and separating // */
69 sprintf(s, ":%s://", ns_name);
72 if (unconfined(profile))
73 /* mode string not being appended */
74 sprintf(s, "%s\n", profile->base.hname);
76 sprintf(s, "%s (%s)\n", profile->base.hname, mode_str);
78 aa_put_ns(current_ns);
80 /* NOTE: len does not include \0 of string, not saved as part of file */
85 * split_token_from_name - separate a string of form <token>^<name>
86 * @op: operation being checked
87 * @args: string to parse (NOT NULL)
88 * @token: stores returned parsed token value (NOT NULL)
90 * Returns: start position of name after token else NULL on failure
92 static char *split_token_from_name(const char *op, char *args, u64 *token)
96 *token = simple_strtoull(args, &name, 16);
97 if ((name == args) || *name != '^') {
98 AA_ERROR("%s: Invalid input '%s'", op, args);
99 return ERR_PTR(-EINVAL);
109 * aa_setprocattr_chagnehat - handle procattr interface to change_hat
110 * @args: args received from writing to /proc/<pid>/attr/current (NOT NULL)
111 * @size: size of the args
112 * @test: true if this is a test of change_hat permissions
114 * Returns: %0 or error code if change_hat fails
116 int aa_setprocattr_changehat(char *args, size_t size, int test)
120 const char *hats[16]; /* current hard limit on # of names */
123 hat = split_token_from_name(OP_CHANGE_HAT, args, &token);
127 if (!hat && !token) {
128 AA_ERROR("change_hat: Invalid input, NULL hat and NULL magic");
133 /* set up hat name vector, args guaranteed null terminated
134 * at args[size] by setprocattr.
136 * If there are multiple hat names in the buffer each is
137 * separated by a \0. Ie. userspace writes them pre tokenized
139 char *end = args + size;
140 for (count = 0; (hat < end) && count < 16; ++count) {
141 char *next = hat + strlen(hat) + 1;
143 AA_DEBUG("%s: (pid %d) Magic 0x%llx count %d hat '%s'\n"
144 , __func__, current->pid, token, count, hat);
148 AA_DEBUG("%s: (pid %d) Magic 0x%llx count %d Hat '%s'\n",
149 __func__, current->pid, token, count, "<NULL>");
151 return aa_change_hat(hats, count, token, test);