]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - security/tomoyo/realpath.c
2 * security/tomoyo/realpath.c
4 * Copyright (C) 2005-2011 NTT DATA CORPORATION
7 #include <linux/types.h>
8 #include <linux/mount.h>
9 #include <linux/mnt_namespace.h>
10 #include <linux/fs_struct.h>
11 #include <linux/magic.h>
12 #include <linux/slab.h>
15 #include "../../fs/internal.h"
18 * tomoyo_encode2 - Encode binary string to ascii string.
20 * @str: String in binary format.
21 * @str_len: Size of @str in byte.
23 * Returns pointer to @str in ascii format on success, NULL otherwise.
25 * This function uses kzalloc(), so caller must kfree() if this function
28 char *tomoyo_encode2(const char *str
, int str_len
)
38 for (i
= 0; i
< str_len
; i
++) {
39 const unsigned char c
= p
[i
];
43 else if (c
> ' ' && c
< 127)
49 /* Reserve space for appending "/". */
50 cp
= kzalloc(len
+ 10, GFP_NOFS
);
55 for (i
= 0; i
< str_len
; i
++) {
56 const unsigned char c
= p
[i
];
61 } else if (c
> ' ' && c
< 127) {
65 *cp
++ = (c
>> 6) + '0';
66 *cp
++ = ((c
>> 3) & 7) + '0';
67 *cp
++ = (c
& 7) + '0';
74 * tomoyo_encode - Encode binary string to ascii string.
76 * @str: String in binary format.
78 * Returns pointer to @str in ascii format on success, NULL otherwise.
80 * This function uses kzalloc(), so caller must kfree() if this function
83 char *tomoyo_encode(const char *str
)
85 return str
? tomoyo_encode2(str
, strlen(str
)) : NULL
;
89 * tomoyo_get_absolute_path - Get the path of a dentry but ignores chroot'ed root.
91 * @path: Pointer to "struct path".
92 * @buffer: Pointer to buffer to return value in.
93 * @buflen: Sizeof @buffer.
95 * Returns the buffer on success, an error code otherwise.
97 * If dentry is a directory, trailing '/' is appended.
99 static char *tomoyo_get_absolute_path(struct path
*path
, char * const buffer
,
102 char *pos
= ERR_PTR(-ENOMEM
);
104 /* go to whatever namespace root we are under */
105 pos
= d_absolute_path(path
, buffer
, buflen
- 1);
106 if (!IS_ERR(pos
) && *pos
== '/' && pos
[1]) {
107 struct inode
*inode
= path
->dentry
->d_inode
;
108 if (inode
&& S_ISDIR(inode
->i_mode
)) {
109 buffer
[buflen
- 2] = '/';
110 buffer
[buflen
- 1] = '\0';
118 * tomoyo_get_dentry_path - Get the path of a dentry.
120 * @dentry: Pointer to "struct dentry".
121 * @buffer: Pointer to buffer to return value in.
122 * @buflen: Sizeof @buffer.
124 * Returns the buffer on success, an error code otherwise.
126 * If dentry is a directory, trailing '/' is appended.
128 static char *tomoyo_get_dentry_path(struct dentry
*dentry
, char * const buffer
,
131 char *pos
= ERR_PTR(-ENOMEM
);
133 pos
= dentry_path_raw(dentry
, buffer
, buflen
- 1);
134 if (!IS_ERR(pos
) && *pos
== '/' && pos
[1]) {
135 struct inode
*inode
= dentry
->d_inode
;
136 if (inode
&& S_ISDIR(inode
->i_mode
)) {
137 buffer
[buflen
- 2] = '/';
138 buffer
[buflen
- 1] = '\0';
146 * tomoyo_get_local_path - Get the path of a dentry.
148 * @dentry: Pointer to "struct dentry".
149 * @buffer: Pointer to buffer to return value in.
150 * @buflen: Sizeof @buffer.
152 * Returns the buffer on success, an error code otherwise.
154 static char *tomoyo_get_local_path(struct dentry
*dentry
, char * const buffer
,
157 struct super_block
*sb
= dentry
->d_sb
;
158 char *pos
= tomoyo_get_dentry_path(dentry
, buffer
, buflen
);
161 /* Convert from $PID to self if $PID is current thread. */
162 if (sb
->s_magic
== PROC_SUPER_MAGIC
&& *pos
== '/') {
164 const pid_t pid
= (pid_t
) simple_strtoul(pos
+ 1, &ep
, 10);
165 if (*ep
== '/' && pid
&& pid
==
166 task_tgid_nr_ns(current
, sb
->s_fs_info
)) {
170 memmove(pos
, "/self", 5);
172 goto prepend_filesystem_name
;
174 /* Use filesystem name for unnamed devices. */
175 if (!MAJOR(sb
->s_dev
))
176 goto prepend_filesystem_name
;
178 struct inode
*inode
= sb
->s_root
->d_inode
;
180 * Use filesystem name if filesystem does not support rename()
183 if (inode
->i_op
&& !inode
->i_op
->rename
)
184 goto prepend_filesystem_name
;
186 /* Prepend device name. */
190 const dev_t dev
= sb
->s_dev
;
191 name
[sizeof(name
) - 1] = '\0';
192 snprintf(name
, sizeof(name
) - 1, "dev(%u,%u):", MAJOR(dev
),
194 name_len
= strlen(name
);
198 memmove(pos
, name
, name_len
);
201 /* Prepend filesystem name. */
202 prepend_filesystem_name
:
204 const char *name
= sb
->s_type
->name
;
205 const int name_len
= strlen(name
);
209 memmove(pos
, name
, name_len
);
214 return ERR_PTR(-ENOMEM
);
218 * tomoyo_get_socket_name - Get the name of a socket.
220 * @path: Pointer to "struct path".
221 * @buffer: Pointer to buffer to return value in.
222 * @buflen: Sizeof @buffer.
224 * Returns the buffer.
226 static char *tomoyo_get_socket_name(struct path
*path
, char * const buffer
,
229 struct inode
*inode
= path
->dentry
->d_inode
;
230 struct socket
*sock
= inode
? SOCKET_I(inode
) : NULL
;
231 struct sock
*sk
= sock
? sock
->sk
: NULL
;
233 snprintf(buffer
, buflen
, "socket:[family=%u:type=%u:"
234 "protocol=%u]", sk
->sk_family
, sk
->sk_type
,
237 snprintf(buffer
, buflen
, "socket:[unknown]");
243 * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
245 * @path: Pointer to "struct path".
247 * Returns the realpath of the given @path on success, NULL otherwise.
249 * If dentry is a directory, trailing '/' is appended.
250 * Characters out of 0x20 < c < 0x7F range are converted to
251 * \ooo style octal string.
252 * Character \ is converted to \\ string.
254 * These functions use kzalloc(), so the caller must call kfree()
255 * if these functions didn't return NULL.
257 char *tomoyo_realpath_from_path(struct path
*path
)
261 unsigned int buf_len
= PAGE_SIZE
/ 2;
262 struct dentry
*dentry
= path
->dentry
;
263 struct super_block
*sb
;
272 buf
= kmalloc(buf_len
, GFP_NOFS
);
275 /* To make sure that pos is '\0' terminated. */
276 buf
[buf_len
- 1] = '\0';
277 /* Get better name for socket. */
278 if (sb
->s_magic
== SOCKFS_MAGIC
) {
279 pos
= tomoyo_get_socket_name(path
, buf
, buf_len
- 1);
282 /* For "pipe:[\$]". */
283 if (dentry
->d_op
&& dentry
->d_op
->d_dname
) {
284 pos
= dentry
->d_op
->d_dname(dentry
, buf
, buf_len
- 1);
287 inode
= sb
->s_root
->d_inode
;
289 * Get local name for filesystems without rename() operation
290 * or dentry without vfsmount.
292 if (!path
->mnt
|| (inode
->i_op
&& !inode
->i_op
->rename
))
293 pos
= tomoyo_get_local_path(path
->dentry
, buf
,
295 /* Get absolute name for the rest. */
297 pos
= tomoyo_get_absolute_path(path
, buf
, buf_len
- 1);
299 * Fall back to local name if absolute name is not
302 if (pos
== ERR_PTR(-EINVAL
))
303 pos
= tomoyo_get_local_path(path
->dentry
, buf
,
309 name
= tomoyo_encode(pos
);
314 tomoyo_warn_oom(__func__
);
319 * tomoyo_realpath_nofollow - Get realpath of a pathname.
321 * @pathname: The pathname to solve.
323 * Returns the realpath of @pathname on success, NULL otherwise.
325 char *tomoyo_realpath_nofollow(const char *pathname
)
329 if (pathname
&& kern_path(pathname
, 0, &path
) == 0) {
330 char *buf
= tomoyo_realpath_from_path(&path
);