blob: 3466a27bca098fc4d75324fc5df4f744d43e8077 [file] [log] [blame]
John Johansen63e2b422010-07-29 14:48:03 -07001/*
2 * AppArmor security module
3 *
4 * This file contains AppArmor /proc/<pid>/attr/ interface functions
5 *
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
8 *
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
12 * License.
13 */
14
15#include "include/apparmor.h"
16#include "include/context.h"
17#include "include/policy.h"
John Johansencff281f2017-01-16 00:42:15 -080018#include "include/policy_ns.h"
John Johansen63e2b422010-07-29 14:48:03 -070019#include "include/domain.h"
James Morriscc7db092011-08-29 11:45:44 +100020#include "include/procattr.h"
John Johansen63e2b422010-07-29 14:48:03 -070021
22
23/**
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)
27 *
28 * Returns: length of @string on success else error on failure
29 *
30 * Requires: profile != NULL
31 *
32 * Creates a string containing the namespace_name://profile_name for
33 * @profile.
34 *
35 * Returns: size of string placed in @string else error code on failure
36 */
37int aa_getprocattr(struct aa_profile *profile, char **string)
38{
39 char *str;
40 int len = 0, mode_len = 0, ns_len = 0, name_len;
John Johansen0d259f02013-07-10 21:13:43 -070041 const char *mode_str = aa_profile_mode_names[profile->mode];
John Johansen63e2b422010-07-29 14:48:03 -070042 const char *ns_name = NULL;
John Johansen98849df2017-01-16 00:42:16 -080043 struct aa_ns *ns = profile->ns;
44 struct aa_ns *current_ns = __aa_current_profile()->ns;
John Johansen63e2b422010-07-29 14:48:03 -070045 char *s;
46
John Johansen92b6d8e2017-01-16 00:42:25 -080047 if (!aa_ns_visible(current_ns, ns, true))
John Johansen63e2b422010-07-29 14:48:03 -070048 return -EACCES;
49
John Johansen92b6d8e2017-01-16 00:42:25 -080050 ns_name = aa_ns_name(current_ns, ns, true);
John Johansen63e2b422010-07-29 14:48:03 -070051 ns_len = strlen(ns_name);
52
53 /* if the visible ns_name is > 0 increase size for : :// seperator */
54 if (ns_len)
55 ns_len += 4;
56
57 /* unconfined profiles don't have a mode string appended */
58 if (!unconfined(profile))
59 mode_len = strlen(mode_str) + 3; /* + 3 for _() */
60
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 */
64 if (!str)
65 return -ENOMEM;
66
67 if (ns_len) {
68 /* skip over prefix current_ns->base.hname and separating // */
69 sprintf(s, ":%s://", ns_name);
70 s += ns_len;
71 }
72 if (unconfined(profile))
73 /* mode string not being appended */
74 sprintf(s, "%s\n", profile->base.hname);
75 else
76 sprintf(s, "%s (%s)\n", profile->base.hname, mode_str);
77 *string = str;
78
79 /* NOTE: len does not include \0 of string, not saved as part of file */
80 return len;
81}
82
83/**
84 * split_token_from_name - separate a string of form <token>^<name>
85 * @op: operation being checked
86 * @args: string to parse (NOT NULL)
87 * @token: stores returned parsed token value (NOT NULL)
88 *
89 * Returns: start position of name after token else NULL on failure
90 */
John Johansen47f6e5c2017-01-16 00:43:01 -080091static char *split_token_from_name(const char *op, char *args, u64 *token)
John Johansen63e2b422010-07-29 14:48:03 -070092{
93 char *name;
94
95 *token = simple_strtoull(args, &name, 16);
96 if ((name == args) || *name != '^') {
John Johansen47f6e5c2017-01-16 00:43:01 -080097 AA_ERROR("%s: Invalid input '%s'", op, args);
John Johansen63e2b422010-07-29 14:48:03 -070098 return ERR_PTR(-EINVAL);
99 }
100
101 name++; /* skip ^ */
102 if (!*name)
103 name = NULL;
104 return name;
105}
106
107/**
108 * aa_setprocattr_chagnehat - handle procattr interface to change_hat
109 * @args: args received from writing to /proc/<pid>/attr/current (NOT NULL)
110 * @size: size of the args
111 * @test: true if this is a test of change_hat permissions
112 *
113 * Returns: %0 or error code if change_hat fails
114 */
115int aa_setprocattr_changehat(char *args, size_t size, int test)
116{
117 char *hat;
118 u64 token;
119 const char *hats[16]; /* current hard limit on # of names */
120 int count = 0;
121
122 hat = split_token_from_name(OP_CHANGE_HAT, args, &token);
123 if (IS_ERR(hat))
124 return PTR_ERR(hat);
125
126 if (!hat && !token) {
127 AA_ERROR("change_hat: Invalid input, NULL hat and NULL magic");
128 return -EINVAL;
129 }
130
131 if (hat) {
132 /* set up hat name vector, args guaranteed null terminated
133 * at args[size] by setprocattr.
134 *
135 * If there are multiple hat names in the buffer each is
136 * separated by a \0. Ie. userspace writes them pre tokenized
137 */
138 char *end = args + size;
139 for (count = 0; (hat < end) && count < 16; ++count) {
140 char *next = hat + strlen(hat) + 1;
141 hats[count] = hat;
John Johansenc3e1e582017-01-16 00:43:05 -0800142 AA_DEBUG("%s: (pid %d) Magic 0x%llx count %d hat '%s'\n"
143 , __func__, current->pid, token, count, hat);
John Johansen63e2b422010-07-29 14:48:03 -0700144 hat = next;
145 }
John Johansenc3e1e582017-01-16 00:43:05 -0800146 } else
147 AA_DEBUG("%s: (pid %d) Magic 0x%llx count %d Hat '%s'\n",
148 __func__, current->pid, token, count, "<NULL>");
John Johansen63e2b422010-07-29 14:48:03 -0700149
150 return aa_change_hat(hats, count, token, test);
151}