xref: /linux-6.15/security/apparmor/path.c (revision fbba8d89)
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 		if (res && !IS_ERR(res)) {
8702125a82SAl Viro 			/* everything's fine */
8802125a82SAl Viro 			*name = res;
8902125a82SAl Viro 			path_put(&root);
9002125a82SAl Viro 			goto ok;
9102125a82SAl Viro 		}
9202125a82SAl Viro 		path_put(&root);
9302125a82SAl Viro 		connected = 0;
9428042fabSJohn Johansen 	} else
9502125a82SAl Viro 		res = d_absolute_path(path, buf, buflen);
96cdff2642SJohn Johansen 
97cdff2642SJohn Johansen 	/* handle error conditions - and still allow a partial path to
98cdff2642SJohn Johansen 	 * be returned.
99cdff2642SJohn Johansen 	 */
100cdff2642SJohn Johansen 	if (IS_ERR(res)) {
101*fbba8d89SJohn Johansen 		res = dentry_path_raw(path->dentry, buf, buflen);
102*fbba8d89SJohn Johansen 		if (IS_ERR(res)) {
103cdff2642SJohn Johansen 			error = PTR_ERR(res);
104cdff2642SJohn Johansen 			*name = buf;
105cdff2642SJohn Johansen 			goto out;
106*fbba8d89SJohn Johansen 		};
107*fbba8d89SJohn Johansen 	} else if (!our_mnt(path->mnt))
10802125a82SAl Viro 		connected = 0;
109cdff2642SJohn Johansen 
110*fbba8d89SJohn Johansen 	*name = res;
111*fbba8d89SJohn Johansen 
11202125a82SAl Viro ok:
113e819ff51SJohn Johansen 	/* Handle two cases:
114e819ff51SJohn Johansen 	 * 1. A deleted dentry && profile is not allowing mediation of deleted
115e819ff51SJohn Johansen 	 * 2. On some filesystems, newly allocated dentries appear to the
116e819ff51SJohn Johansen 	 *    security_path hooks as a deleted dentry except without an inode
117e819ff51SJohn Johansen 	 *    allocated.
118e819ff51SJohn Johansen 	 */
119e819ff51SJohn Johansen 	if (d_unlinked(path->dentry) && path->dentry->d_inode &&
120e819ff51SJohn Johansen 	    !(flags & PATH_MEDIATE_DELETED)) {
121cdff2642SJohn Johansen 			error = -ENOENT;
122cdff2642SJohn Johansen 			goto out;
123cdff2642SJohn Johansen 	}
124cdff2642SJohn Johansen 
12502125a82SAl Viro 	/* If the path is not connected to the expected root,
126cdff2642SJohn Johansen 	 * check if it is a sysctl and handle specially else remove any
127cdff2642SJohn Johansen 	 * leading / that __d_path may have returned.
128cdff2642SJohn Johansen 	 * Unless
129cdff2642SJohn Johansen 	 *     specifically directed to connect the path,
130cdff2642SJohn Johansen 	 * OR
131cdff2642SJohn Johansen 	 *     if in a chroot and doing chroot relative paths and the path
132cdff2642SJohn Johansen 	 *     resolves to the namespace root (would be connected outside
133cdff2642SJohn Johansen 	 *     of chroot) and specifically directed to connect paths to
134cdff2642SJohn Johansen 	 *     namespace root.
135cdff2642SJohn Johansen 	 */
136cdff2642SJohn Johansen 	if (!connected) {
13702125a82SAl Viro 		if (!(flags & PATH_CONNECT_PATH) &&
138cdff2642SJohn Johansen 			   !(((flags & CHROOT_NSCONNECT) == CHROOT_NSCONNECT) &&
13902125a82SAl Viro 			     our_mnt(path->mnt))) {
140cdff2642SJohn Johansen 			/* disconnected path, don't return pathname starting
141cdff2642SJohn Johansen 			 * with '/'
142cdff2642SJohn Johansen 			 */
143ef9a7622SJohn Johansen 			error = -EACCES;
144cdff2642SJohn Johansen 			if (*res == '/')
145cdff2642SJohn Johansen 				*name = res + 1;
146cdff2642SJohn Johansen 		}
147cdff2642SJohn Johansen 	}
148cdff2642SJohn Johansen 
149cdff2642SJohn Johansen out:
150cdff2642SJohn Johansen 	return error;
151cdff2642SJohn Johansen }
152cdff2642SJohn Johansen 
153cdff2642SJohn Johansen /**
154cdff2642SJohn Johansen  * get_name_to_buffer - get the pathname to a buffer ensure dir / is appended
155cdff2642SJohn Johansen  * @path: path to get name for  (NOT NULL)
156cdff2642SJohn Johansen  * @flags: flags controlling path lookup
157cdff2642SJohn Johansen  * @buffer: buffer to put name in  (NOT NULL)
158cdff2642SJohn Johansen  * @size: size of buffer
159cdff2642SJohn Johansen  * @name: Returns - contains position of path name in @buffer (NOT NULL)
160cdff2642SJohn Johansen  *
161cdff2642SJohn Johansen  * Returns: %0 else error on failure
162cdff2642SJohn Johansen  */
163cdff2642SJohn Johansen static int get_name_to_buffer(struct path *path, int flags, char *buffer,
164cdff2642SJohn Johansen 			      int size, char **name)
165cdff2642SJohn Johansen {
166cdff2642SJohn Johansen 	int adjust = (flags & PATH_IS_DIR) ? 1 : 0;
167cdff2642SJohn Johansen 	int error = d_namespace_path(path, buffer, size - adjust, name, flags);
168cdff2642SJohn Johansen 
169cdff2642SJohn Johansen 	if (!error && (flags & PATH_IS_DIR) && (*name)[1] != '\0')
170cdff2642SJohn Johansen 		/*
171cdff2642SJohn Johansen 		 * Append "/" to the pathname.  The root directory is a special
172cdff2642SJohn Johansen 		 * case; it already ends in slash.
173cdff2642SJohn Johansen 		 */
174cdff2642SJohn Johansen 		strcpy(&buffer[size - 2], "/");
175cdff2642SJohn Johansen 
176cdff2642SJohn Johansen 	return error;
177cdff2642SJohn Johansen }
178cdff2642SJohn Johansen 
179cdff2642SJohn Johansen /**
180cdff2642SJohn Johansen  * aa_get_name - compute the pathname of a file
181cdff2642SJohn Johansen  * @path: path the file  (NOT NULL)
182cdff2642SJohn Johansen  * @flags: flags controlling path name generation
183cdff2642SJohn Johansen  * @buffer: buffer that aa_get_name() allocated  (NOT NULL)
184cdff2642SJohn Johansen  * @name: Returns - the generated path name if !error (NOT NULL)
185cdff2642SJohn Johansen  *
186cdff2642SJohn Johansen  * @name is a pointer to the beginning of the pathname (which usually differs
187cdff2642SJohn Johansen  * from the beginning of the buffer), or NULL.  If there is an error @name
188cdff2642SJohn Johansen  * may contain a partial or invalid name that can be used for audit purposes,
189cdff2642SJohn Johansen  * but it can not be used for mediation.
190cdff2642SJohn Johansen  *
191cdff2642SJohn Johansen  * We need PATH_IS_DIR to indicate whether the file is a directory or not
192cdff2642SJohn Johansen  * because the file may not yet exist, and so we cannot check the inode's
193cdff2642SJohn Johansen  * file type.
194cdff2642SJohn Johansen  *
195cdff2642SJohn Johansen  * Returns: %0 else error code if could retrieve name
196cdff2642SJohn Johansen  */
197cdff2642SJohn Johansen int aa_get_name(struct path *path, int flags, char **buffer, const char **name)
198cdff2642SJohn Johansen {
199cdff2642SJohn Johansen 	char *buf, *str = NULL;
200cdff2642SJohn Johansen 	int size = 256;
201cdff2642SJohn Johansen 	int error;
202cdff2642SJohn Johansen 
203cdff2642SJohn Johansen 	*name = NULL;
204cdff2642SJohn Johansen 	*buffer = NULL;
205cdff2642SJohn Johansen 	for (;;) {
206cdff2642SJohn Johansen 		/* freed by caller */
207cdff2642SJohn Johansen 		buf = kmalloc(size, GFP_KERNEL);
208cdff2642SJohn Johansen 		if (!buf)
209cdff2642SJohn Johansen 			return -ENOMEM;
210cdff2642SJohn Johansen 
211cdff2642SJohn Johansen 		error = get_name_to_buffer(path, flags, buf, size, &str);
212cdff2642SJohn Johansen 		if (error != -ENAMETOOLONG)
213cdff2642SJohn Johansen 			break;
214cdff2642SJohn Johansen 
215cdff2642SJohn Johansen 		kfree(buf);
216cdff2642SJohn Johansen 		size <<= 1;
217cdff2642SJohn Johansen 		if (size > aa_g_path_max)
218cdff2642SJohn Johansen 			return -ENAMETOOLONG;
219cdff2642SJohn Johansen 	}
220cdff2642SJohn Johansen 	*buffer = buf;
221cdff2642SJohn Johansen 	*name = str;
222cdff2642SJohn Johansen 
223cdff2642SJohn Johansen 	return error;
224cdff2642SJohn Johansen }
225