xref: /linux-6.15/security/apparmor/path.c (revision 57fa1e18)
1cdff2642SJohn Johansen /*
2cdff2642SJohn Johansen  * AppArmor security module
3cdff2642SJohn Johansen  *
4cdff2642SJohn Johansen  * This file contains AppArmor function for pathnames
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 
15cdff2642SJohn Johansen #include <linux/magic.h>
16cdff2642SJohn Johansen #include <linux/mount.h>
17cdff2642SJohn Johansen #include <linux/namei.h>
18cdff2642SJohn Johansen #include <linux/nsproxy.h>
19cdff2642SJohn Johansen #include <linux/path.h>
20cdff2642SJohn Johansen #include <linux/sched.h>
21cdff2642SJohn Johansen #include <linux/slab.h>
22cdff2642SJohn Johansen #include <linux/fs_struct.h>
23cdff2642SJohn Johansen 
24cdff2642SJohn Johansen #include "include/apparmor.h"
25cdff2642SJohn Johansen #include "include/path.h"
26cdff2642SJohn Johansen #include "include/policy.h"
27cdff2642SJohn Johansen 
28cdff2642SJohn Johansen 
29cdff2642SJohn Johansen /* modified from dcache.c */
30cdff2642SJohn Johansen static int prepend(char **buffer, int buflen, const char *str, int namelen)
31cdff2642SJohn Johansen {
32cdff2642SJohn Johansen 	buflen -= namelen;
33cdff2642SJohn Johansen 	if (buflen < 0)
34cdff2642SJohn Johansen 		return -ENAMETOOLONG;
35cdff2642SJohn Johansen 	*buffer -= namelen;
36cdff2642SJohn Johansen 	memcpy(*buffer, str, namelen);
37cdff2642SJohn Johansen 	return 0;
38cdff2642SJohn Johansen }
39cdff2642SJohn Johansen 
40cdff2642SJohn Johansen #define CHROOT_NSCONNECT (PATH_CHROOT_REL | PATH_CHROOT_NSCONNECT)
41cdff2642SJohn Johansen 
42cdff2642SJohn Johansen /**
43cdff2642SJohn Johansen  * d_namespace_path - lookup a name associated with a given path
44cdff2642SJohn Johansen  * @path: path to lookup  (NOT NULL)
45cdff2642SJohn Johansen  * @buf:  buffer to store path to  (NOT NULL)
46cdff2642SJohn Johansen  * @buflen: length of @buf
47cdff2642SJohn Johansen  * @name: Returns - pointer for start of path name with in @buf (NOT NULL)
48cdff2642SJohn Johansen  * @flags: flags controlling path lookup
49cdff2642SJohn Johansen  *
50cdff2642SJohn Johansen  * Handle path name lookup.
51cdff2642SJohn Johansen  *
52cdff2642SJohn Johansen  * Returns: %0 else error code if path lookup fails
53cdff2642SJohn Johansen  *          When no error the path name is returned in @name which points to
54cdff2642SJohn Johansen  *          to a position in @buf
55cdff2642SJohn Johansen  */
56cdff2642SJohn Johansen static int d_namespace_path(struct path *path, char *buf, int buflen,
57cdff2642SJohn Johansen 			    char **name, int flags)
58cdff2642SJohn Johansen {
59cdff2642SJohn Johansen 	char *res;
6002125a82SAl Viro 	int error = 0;
6102125a82SAl Viro 	int connected = 1;
62cdff2642SJohn Johansen 
6302125a82SAl Viro 	if (path->mnt->mnt_flags & MNT_INTERNAL) {
6402125a82SAl Viro 		/* it's not mounted anywhere */
6502125a82SAl Viro 		res = dentry_path(path->dentry, buf, buflen);
6602125a82SAl Viro 		*name = res;
6702125a82SAl Viro 		if (IS_ERR(res)) {
6802125a82SAl Viro 			*name = buf;
6902125a82SAl Viro 			return PTR_ERR(res);
7002125a82SAl Viro 		}
7102125a82SAl Viro 		if (path->dentry->d_sb->s_magic == PROC_SUPER_MAGIC &&
7202125a82SAl Viro 		    strncmp(*name, "/sys/", 5) == 0) {
7302125a82SAl Viro 			/* TODO: convert over to using a per namespace
7402125a82SAl Viro 			 * control instead of hard coded /proc
7502125a82SAl Viro 			 */
7602125a82SAl Viro 			return prepend(name, *name - buf, "/proc", 5);
7702125a82SAl Viro 		}
7802125a82SAl Viro 		return 0;
79cdff2642SJohn Johansen 	}
80cdff2642SJohn Johansen 
8102125a82SAl Viro 	/* resolve paths relative to chroot?*/
8202125a82SAl Viro 	if (flags & PATH_CHROOT_REL) {
8302125a82SAl Viro 		struct path root;
8402125a82SAl Viro 		get_fs_root(current->fs, &root);
8502125a82SAl Viro 		res = __d_path(path, &root, buf, buflen);
8602125a82SAl Viro 		path_put(&root);
873372b68aSJohn Johansen 	} else {
8802125a82SAl Viro 		res = d_absolute_path(path, buf, buflen);
893372b68aSJohn Johansen 		if (!our_mnt(path->mnt))
903372b68aSJohn Johansen 			connected = 0;
913372b68aSJohn Johansen 	}
92cdff2642SJohn Johansen 
93cdff2642SJohn Johansen 	/* handle error conditions - and still allow a partial path to
94cdff2642SJohn Johansen 	 * be returned.
95cdff2642SJohn Johansen 	 */
963372b68aSJohn Johansen 	if (!res || IS_ERR(res)) {
973372b68aSJohn Johansen 		connected = 0;
98fbba8d89SJohn Johansen 		res = dentry_path_raw(path->dentry, buf, buflen);
99fbba8d89SJohn Johansen 		if (IS_ERR(res)) {
100cdff2642SJohn Johansen 			error = PTR_ERR(res);
101cdff2642SJohn Johansen 			*name = buf;
102cdff2642SJohn Johansen 			goto out;
103fbba8d89SJohn Johansen 		};
104fbba8d89SJohn Johansen 	} else if (!our_mnt(path->mnt))
10502125a82SAl Viro 		connected = 0;
106cdff2642SJohn Johansen 
107fbba8d89SJohn Johansen 	*name = res;
108fbba8d89SJohn Johansen 
109e819ff51SJohn Johansen 	/* Handle two cases:
110e819ff51SJohn Johansen 	 * 1. A deleted dentry && profile is not allowing mediation of deleted
111e819ff51SJohn Johansen 	 * 2. On some filesystems, newly allocated dentries appear to the
112e819ff51SJohn Johansen 	 *    security_path hooks as a deleted dentry except without an inode
113e819ff51SJohn Johansen 	 *    allocated.
114e819ff51SJohn Johansen 	 */
115e819ff51SJohn Johansen 	if (d_unlinked(path->dentry) && path->dentry->d_inode &&
116e819ff51SJohn Johansen 	    !(flags & PATH_MEDIATE_DELETED)) {
117cdff2642SJohn Johansen 			error = -ENOENT;
118cdff2642SJohn Johansen 			goto out;
119cdff2642SJohn Johansen 	}
120cdff2642SJohn Johansen 
12102125a82SAl Viro 	/* If the path is not connected to the expected root,
122cdff2642SJohn Johansen 	 * check if it is a sysctl and handle specially else remove any
123cdff2642SJohn Johansen 	 * leading / that __d_path may have returned.
124cdff2642SJohn Johansen 	 * Unless
125cdff2642SJohn Johansen 	 *     specifically directed to connect the path,
126cdff2642SJohn Johansen 	 * OR
127cdff2642SJohn Johansen 	 *     if in a chroot and doing chroot relative paths and the path
128cdff2642SJohn Johansen 	 *     resolves to the namespace root (would be connected outside
129cdff2642SJohn Johansen 	 *     of chroot) and specifically directed to connect paths to
130cdff2642SJohn Johansen 	 *     namespace root.
131cdff2642SJohn Johansen 	 */
132cdff2642SJohn Johansen 	if (!connected) {
13302125a82SAl Viro 		if (!(flags & PATH_CONNECT_PATH) &&
134cdff2642SJohn Johansen 			   !(((flags & CHROOT_NSCONNECT) == CHROOT_NSCONNECT) &&
13502125a82SAl Viro 			     our_mnt(path->mnt))) {
136cdff2642SJohn Johansen 			/* disconnected path, don't return pathname starting
137cdff2642SJohn Johansen 			 * with '/'
138cdff2642SJohn Johansen 			 */
139ef9a7622SJohn Johansen 			error = -EACCES;
140cdff2642SJohn Johansen 			if (*res == '/')
141cdff2642SJohn Johansen 				*name = res + 1;
142cdff2642SJohn Johansen 		}
143cdff2642SJohn Johansen 	}
144cdff2642SJohn Johansen 
145cdff2642SJohn Johansen out:
146cdff2642SJohn Johansen 	return error;
147cdff2642SJohn Johansen }
148cdff2642SJohn Johansen 
149cdff2642SJohn Johansen /**
150cdff2642SJohn Johansen  * get_name_to_buffer - get the pathname to a buffer ensure dir / is appended
151cdff2642SJohn Johansen  * @path: path to get name for  (NOT NULL)
152cdff2642SJohn Johansen  * @flags: flags controlling path lookup
153cdff2642SJohn Johansen  * @buffer: buffer to put name in  (NOT NULL)
154cdff2642SJohn Johansen  * @size: size of buffer
155cdff2642SJohn Johansen  * @name: Returns - contains position of path name in @buffer (NOT NULL)
156cdff2642SJohn Johansen  *
157cdff2642SJohn Johansen  * Returns: %0 else error on failure
158cdff2642SJohn Johansen  */
159cdff2642SJohn Johansen static int get_name_to_buffer(struct path *path, int flags, char *buffer,
160*57fa1e18SJohn Johansen 			      int size, char **name, const char **info)
161cdff2642SJohn Johansen {
162cdff2642SJohn Johansen 	int adjust = (flags & PATH_IS_DIR) ? 1 : 0;
163cdff2642SJohn Johansen 	int error = d_namespace_path(path, buffer, size - adjust, name, flags);
164cdff2642SJohn Johansen 
165cdff2642SJohn Johansen 	if (!error && (flags & PATH_IS_DIR) && (*name)[1] != '\0')
166cdff2642SJohn Johansen 		/*
167cdff2642SJohn Johansen 		 * Append "/" to the pathname.  The root directory is a special
168cdff2642SJohn Johansen 		 * case; it already ends in slash.
169cdff2642SJohn Johansen 		 */
170cdff2642SJohn Johansen 		strcpy(&buffer[size - 2], "/");
171cdff2642SJohn Johansen 
172*57fa1e18SJohn Johansen 	if (info && error) {
173*57fa1e18SJohn Johansen 		if (error == -ENOENT)
174*57fa1e18SJohn Johansen 			*info = "Failed name lookup - deleted entry";
175*57fa1e18SJohn Johansen 		else if (error == -ESTALE)
176*57fa1e18SJohn Johansen 			*info = "Failed name lookup - disconnected path";
177*57fa1e18SJohn Johansen 		else if (error == -ENAMETOOLONG)
178*57fa1e18SJohn Johansen 			*info = "Failed name lookup - name too long";
179*57fa1e18SJohn Johansen 		else
180*57fa1e18SJohn Johansen 			*info = "Failed name lookup";
181*57fa1e18SJohn Johansen 	}
182*57fa1e18SJohn Johansen 
183cdff2642SJohn Johansen 	return error;
184cdff2642SJohn Johansen }
185cdff2642SJohn Johansen 
186cdff2642SJohn Johansen /**
187*57fa1e18SJohn Johansen  * aa_path_name - compute the pathname of a file
188cdff2642SJohn Johansen  * @path: path the file  (NOT NULL)
189cdff2642SJohn Johansen  * @flags: flags controlling path name generation
190cdff2642SJohn Johansen  * @buffer: buffer that aa_get_name() allocated  (NOT NULL)
191cdff2642SJohn Johansen  * @name: Returns - the generated path name if !error (NOT NULL)
192*57fa1e18SJohn Johansen  * @info: Returns - information on why the path lookup failed (MAYBE NULL)
193cdff2642SJohn Johansen  *
194cdff2642SJohn Johansen  * @name is a pointer to the beginning of the pathname (which usually differs
195cdff2642SJohn Johansen  * from the beginning of the buffer), or NULL.  If there is an error @name
196cdff2642SJohn Johansen  * may contain a partial or invalid name that can be used for audit purposes,
197cdff2642SJohn Johansen  * but it can not be used for mediation.
198cdff2642SJohn Johansen  *
199cdff2642SJohn Johansen  * We need PATH_IS_DIR to indicate whether the file is a directory or not
200cdff2642SJohn Johansen  * because the file may not yet exist, and so we cannot check the inode's
201cdff2642SJohn Johansen  * file type.
202cdff2642SJohn Johansen  *
203cdff2642SJohn Johansen  * Returns: %0 else error code if could retrieve name
204cdff2642SJohn Johansen  */
205*57fa1e18SJohn Johansen int aa_path_name(struct path *path, int flags, char **buffer, const char **name,
206*57fa1e18SJohn Johansen 		 const char **info)
207cdff2642SJohn Johansen {
208cdff2642SJohn Johansen 	char *buf, *str = NULL;
209cdff2642SJohn Johansen 	int size = 256;
210cdff2642SJohn Johansen 	int error;
211cdff2642SJohn Johansen 
212cdff2642SJohn Johansen 	*name = NULL;
213cdff2642SJohn Johansen 	*buffer = NULL;
214cdff2642SJohn Johansen 	for (;;) {
215cdff2642SJohn Johansen 		/* freed by caller */
216cdff2642SJohn Johansen 		buf = kmalloc(size, GFP_KERNEL);
217cdff2642SJohn Johansen 		if (!buf)
218cdff2642SJohn Johansen 			return -ENOMEM;
219cdff2642SJohn Johansen 
220*57fa1e18SJohn Johansen 		error = get_name_to_buffer(path, flags, buf, size, &str, info);
221cdff2642SJohn Johansen 		if (error != -ENAMETOOLONG)
222cdff2642SJohn Johansen 			break;
223cdff2642SJohn Johansen 
224cdff2642SJohn Johansen 		kfree(buf);
225cdff2642SJohn Johansen 		size <<= 1;
226cdff2642SJohn Johansen 		if (size > aa_g_path_max)
227cdff2642SJohn Johansen 			return -ENAMETOOLONG;
228*57fa1e18SJohn Johansen 		*info = NULL;
229cdff2642SJohn Johansen 	}
230cdff2642SJohn Johansen 	*buffer = buf;
231cdff2642SJohn Johansen 	*name = str;
232cdff2642SJohn Johansen 
233cdff2642SJohn Johansen 	return error;
234cdff2642SJohn Johansen }
235