1 // SPDX-License-Identifier: GPL-2.0
3 * security/tomoyo/realpath.c
5 * Copyright (C) 2005-2011 NTT DATA CORPORATION
9 #include <linux/magic.h>
12 * tomoyo_encode2 - Encode binary string to ascii string.
14 * @str: String in binary format.
15 * @str_len: Size of @str in byte.
17 * Returns pointer to @str in ascii format on success, NULL otherwise.
19 * This function uses kzalloc(), so caller must kfree() if this function
22 char *tomoyo_encode2(const char *str
, int str_len
)
32 for (i
= 0; i
< str_len
; i
++) {
33 const unsigned char c
= p
[i
];
37 else if (c
> ' ' && c
< 127)
43 /* Reserve space for appending "/". */
44 cp
= kzalloc(len
+ 10, GFP_NOFS
);
49 for (i
= 0; i
< str_len
; i
++) {
50 const unsigned char c
= p
[i
];
55 } else if (c
> ' ' && c
< 127) {
59 *cp
++ = (c
>> 6) + '0';
60 *cp
++ = ((c
>> 3) & 7) + '0';
61 *cp
++ = (c
& 7) + '0';
68 * tomoyo_encode - Encode binary string to ascii string.
70 * @str: String in binary format.
72 * Returns pointer to @str in ascii format on success, NULL otherwise.
74 * This function uses kzalloc(), so caller must kfree() if this function
77 char *tomoyo_encode(const char *str
)
79 return str
? tomoyo_encode2(str
, strlen(str
)) : NULL
;
83 * tomoyo_get_absolute_path - Get the path of a dentry but ignores chroot'ed root.
85 * @path: Pointer to "struct path".
86 * @buffer: Pointer to buffer to return value in.
87 * @buflen: Sizeof @buffer.
89 * Returns the buffer on success, an error code otherwise.
91 * If dentry is a directory, trailing '/' is appended.
93 static char *tomoyo_get_absolute_path(const struct path
*path
, char * const buffer
,
96 char *pos
= ERR_PTR(-ENOMEM
);
98 /* go to whatever namespace root we are under */
99 pos
= d_absolute_path(path
, buffer
, buflen
- 1);
100 if (!IS_ERR(pos
) && *pos
== '/' && pos
[1]) {
101 struct inode
*inode
= d_backing_inode(path
->dentry
);
102 if (inode
&& S_ISDIR(inode
->i_mode
)) {
103 buffer
[buflen
- 2] = '/';
104 buffer
[buflen
- 1] = '\0';
112 * tomoyo_get_dentry_path - Get the path of a dentry.
114 * @dentry: Pointer to "struct dentry".
115 * @buffer: Pointer to buffer to return value in.
116 * @buflen: Sizeof @buffer.
118 * Returns the buffer on success, an error code otherwise.
120 * If dentry is a directory, trailing '/' is appended.
122 static char *tomoyo_get_dentry_path(struct dentry
*dentry
, char * const buffer
,
125 char *pos
= ERR_PTR(-ENOMEM
);
127 pos
= dentry_path_raw(dentry
, buffer
, buflen
- 1);
128 if (!IS_ERR(pos
) && *pos
== '/' && pos
[1]) {
129 struct inode
*inode
= d_backing_inode(dentry
);
130 if (inode
&& S_ISDIR(inode
->i_mode
)) {
131 buffer
[buflen
- 2] = '/';
132 buffer
[buflen
- 1] = '\0';
140 * tomoyo_get_local_path - Get the path of a dentry.
142 * @dentry: Pointer to "struct dentry".
143 * @buffer: Pointer to buffer to return value in.
144 * @buflen: Sizeof @buffer.
146 * Returns the buffer on success, an error code otherwise.
148 static char *tomoyo_get_local_path(struct dentry
*dentry
, char * const buffer
,
151 struct super_block
*sb
= dentry
->d_sb
;
152 char *pos
= tomoyo_get_dentry_path(dentry
, buffer
, buflen
);
155 /* Convert from $PID to self if $PID is current thread. */
156 if (sb
->s_magic
== PROC_SUPER_MAGIC
&& *pos
== '/') {
158 const pid_t pid
= (pid_t
) simple_strtoul(pos
+ 1, &ep
, 10);
159 if (*ep
== '/' && pid
&& pid
==
160 task_tgid_nr_ns(current
, sb
->s_fs_info
)) {
164 memmove(pos
, "/self", 5);
166 goto prepend_filesystem_name
;
168 /* Use filesystem name for unnamed devices. */
169 if (!MAJOR(sb
->s_dev
))
170 goto prepend_filesystem_name
;
172 struct inode
*inode
= d_backing_inode(sb
->s_root
);
174 * Use filesystem name if filesystem does not support rename()
177 if (!inode
->i_op
->rename
)
178 goto prepend_filesystem_name
;
180 /* Prepend device name. */
184 const dev_t dev
= sb
->s_dev
;
185 name
[sizeof(name
) - 1] = '\0';
186 snprintf(name
, sizeof(name
) - 1, "dev(%u,%u):", MAJOR(dev
),
188 name_len
= strlen(name
);
192 memmove(pos
, name
, name_len
);
195 /* Prepend filesystem name. */
196 prepend_filesystem_name
:
198 const char *name
= sb
->s_type
->name
;
199 const int name_len
= strlen(name
);
203 memmove(pos
, name
, name_len
);
208 return ERR_PTR(-ENOMEM
);
212 * tomoyo_get_socket_name - Get the name of a socket.
214 * @path: Pointer to "struct path".
215 * @buffer: Pointer to buffer to return value in.
216 * @buflen: Sizeof @buffer.
218 * Returns the buffer.
220 static char *tomoyo_get_socket_name(const struct path
*path
, char * const buffer
,
223 struct inode
*inode
= d_backing_inode(path
->dentry
);
224 struct socket
*sock
= inode
? SOCKET_I(inode
) : NULL
;
225 struct sock
*sk
= sock
? sock
->sk
: NULL
;
227 snprintf(buffer
, buflen
, "socket:[family=%u:type=%u:"
228 "protocol=%u]", sk
->sk_family
, sk
->sk_type
,
231 snprintf(buffer
, buflen
, "socket:[unknown]");
237 * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
239 * @path: Pointer to "struct path".
241 * Returns the realpath of the given @path on success, NULL otherwise.
243 * If dentry is a directory, trailing '/' is appended.
244 * Characters out of 0x20 < c < 0x7F range are converted to
245 * \ooo style octal string.
246 * Character \ is converted to \\ string.
248 * These functions use kzalloc(), so the caller must call kfree()
249 * if these functions didn't return NULL.
251 char *tomoyo_realpath_from_path(const struct path
*path
)
255 unsigned int buf_len
= PAGE_SIZE
/ 2;
256 struct dentry
*dentry
= path
->dentry
;
257 struct super_block
*sb
;
266 buf
= kmalloc(buf_len
, GFP_NOFS
);
269 /* To make sure that pos is '\0' terminated. */
270 buf
[buf_len
- 1] = '\0';
271 /* Get better name for socket. */
272 if (sb
->s_magic
== SOCKFS_MAGIC
) {
273 pos
= tomoyo_get_socket_name(path
, buf
, buf_len
- 1);
276 /* For "pipe:[\$]". */
277 if (dentry
->d_op
&& dentry
->d_op
->d_dname
) {
278 pos
= dentry
->d_op
->d_dname(dentry
, buf
, buf_len
- 1);
281 inode
= d_backing_inode(sb
->s_root
);
283 * Get local name for filesystems without rename() operation
284 * or dentry without vfsmount.
287 (!inode
->i_op
->rename
))
288 pos
= tomoyo_get_local_path(path
->dentry
, buf
,
290 /* Get absolute name for the rest. */
292 pos
= tomoyo_get_absolute_path(path
, buf
, buf_len
- 1);
294 * Fall back to local name if absolute name is not
297 if (pos
== ERR_PTR(-EINVAL
))
298 pos
= tomoyo_get_local_path(path
->dentry
, buf
,
304 name
= tomoyo_encode(pos
);
309 tomoyo_warn_oom(__func__
);
314 * tomoyo_realpath_nofollow - Get realpath of a pathname.
316 * @pathname: The pathname to solve.
318 * Returns the realpath of @pathname on success, NULL otherwise.
320 char *tomoyo_realpath_nofollow(const char *pathname
)
324 if (pathname
&& kern_path(pathname
, 0, &path
) == 0) {
325 char *buf
= tomoyo_realpath_from_path(&path
);