2 * security/tomoyo/realpath.c
4 * Copyright (C) 2005-2011 NTT DATA CORPORATION
8 #include <linux/magic.h>
11 * tomoyo_encode2 - Encode binary string to ascii string.
13 * @str: String in binary format.
14 * @str_len: Size of @str in byte.
16 * Returns pointer to @str in ascii format on success, NULL otherwise.
18 * This function uses kzalloc(), so caller must kfree() if this function
21 char *tomoyo_encode2(const char *str
, int str_len
)
31 for (i
= 0; i
< str_len
; i
++) {
32 const unsigned char c
= p
[i
];
36 else if (c
> ' ' && c
< 127)
42 /* Reserve space for appending "/". */
43 cp
= kzalloc(len
+ 10, GFP_NOFS
);
48 for (i
= 0; i
< str_len
; i
++) {
49 const unsigned char c
= p
[i
];
54 } else if (c
> ' ' && c
< 127) {
58 *cp
++ = (c
>> 6) + '0';
59 *cp
++ = ((c
>> 3) & 7) + '0';
60 *cp
++ = (c
& 7) + '0';
67 * tomoyo_encode - Encode binary string to ascii string.
69 * @str: String in binary format.
71 * Returns pointer to @str in ascii format on success, NULL otherwise.
73 * This function uses kzalloc(), so caller must kfree() if this function
76 char *tomoyo_encode(const char *str
)
78 return str
? tomoyo_encode2(str
, strlen(str
)) : NULL
;
82 * tomoyo_get_absolute_path - Get the path of a dentry but ignores chroot'ed root.
84 * @path: Pointer to "struct path".
85 * @buffer: Pointer to buffer to return value in.
86 * @buflen: Sizeof @buffer.
88 * Returns the buffer on success, an error code otherwise.
90 * If dentry is a directory, trailing '/' is appended.
92 static char *tomoyo_get_absolute_path(const struct path
*path
, char * const buffer
,
95 char *pos
= ERR_PTR(-ENOMEM
);
97 /* go to whatever namespace root we are under */
98 pos
= d_absolute_path(path
, buffer
, buflen
- 1);
99 if (!IS_ERR(pos
) && *pos
== '/' && pos
[1]) {
100 struct inode
*inode
= d_backing_inode(path
->dentry
);
101 if (inode
&& S_ISDIR(inode
->i_mode
)) {
102 buffer
[buflen
- 2] = '/';
103 buffer
[buflen
- 1] = '\0';
111 * tomoyo_get_dentry_path - Get the path of a dentry.
113 * @dentry: Pointer to "struct dentry".
114 * @buffer: Pointer to buffer to return value in.
115 * @buflen: Sizeof @buffer.
117 * Returns the buffer on success, an error code otherwise.
119 * If dentry is a directory, trailing '/' is appended.
121 static char *tomoyo_get_dentry_path(struct dentry
*dentry
, char * const buffer
,
124 char *pos
= ERR_PTR(-ENOMEM
);
126 pos
= dentry_path_raw(dentry
, buffer
, buflen
- 1);
127 if (!IS_ERR(pos
) && *pos
== '/' && pos
[1]) {
128 struct inode
*inode
= d_backing_inode(dentry
);
129 if (inode
&& S_ISDIR(inode
->i_mode
)) {
130 buffer
[buflen
- 2] = '/';
131 buffer
[buflen
- 1] = '\0';
139 * tomoyo_get_local_path - Get the path of a dentry.
141 * @dentry: Pointer to "struct dentry".
142 * @buffer: Pointer to buffer to return value in.
143 * @buflen: Sizeof @buffer.
145 * Returns the buffer on success, an error code otherwise.
147 static char *tomoyo_get_local_path(struct dentry
*dentry
, char * const buffer
,
150 struct super_block
*sb
= dentry
->d_sb
;
151 char *pos
= tomoyo_get_dentry_path(dentry
, buffer
, buflen
);
154 /* Convert from $PID to self if $PID is current thread. */
155 if (sb
->s_magic
== PROC_SUPER_MAGIC
&& *pos
== '/') {
157 const pid_t pid
= (pid_t
) simple_strtoul(pos
+ 1, &ep
, 10);
158 if (*ep
== '/' && pid
&& pid
==
159 task_tgid_nr_ns(current
, sb
->s_fs_info
)) {
163 memmove(pos
, "/self", 5);
165 goto prepend_filesystem_name
;
167 /* Use filesystem name for unnamed devices. */
168 if (!MAJOR(sb
->s_dev
))
169 goto prepend_filesystem_name
;
171 struct inode
*inode
= d_backing_inode(sb
->s_root
);
173 * Use filesystem name if filesystem does not support rename()
176 if (!inode
->i_op
->rename
)
177 goto prepend_filesystem_name
;
179 /* Prepend device name. */
183 const dev_t dev
= sb
->s_dev
;
184 name
[sizeof(name
) - 1] = '\0';
185 snprintf(name
, sizeof(name
) - 1, "dev(%u,%u):", MAJOR(dev
),
187 name_len
= strlen(name
);
191 memmove(pos
, name
, name_len
);
194 /* Prepend filesystem name. */
195 prepend_filesystem_name
:
197 const char *name
= sb
->s_type
->name
;
198 const int name_len
= strlen(name
);
202 memmove(pos
, name
, name_len
);
207 return ERR_PTR(-ENOMEM
);
211 * tomoyo_get_socket_name - Get the name of a socket.
213 * @path: Pointer to "struct path".
214 * @buffer: Pointer to buffer to return value in.
215 * @buflen: Sizeof @buffer.
217 * Returns the buffer.
219 static char *tomoyo_get_socket_name(const struct path
*path
, char * const buffer
,
222 struct inode
*inode
= d_backing_inode(path
->dentry
);
223 struct socket
*sock
= inode
? SOCKET_I(inode
) : NULL
;
224 struct sock
*sk
= sock
? sock
->sk
: NULL
;
226 snprintf(buffer
, buflen
, "socket:[family=%u:type=%u:"
227 "protocol=%u]", sk
->sk_family
, sk
->sk_type
,
230 snprintf(buffer
, buflen
, "socket:[unknown]");
236 * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
238 * @path: Pointer to "struct path".
240 * Returns the realpath of the given @path on success, NULL otherwise.
242 * If dentry is a directory, trailing '/' is appended.
243 * Characters out of 0x20 < c < 0x7F range are converted to
244 * \ooo style octal string.
245 * Character \ is converted to \\ string.
247 * These functions use kzalloc(), so the caller must call kfree()
248 * if these functions didn't return NULL.
250 char *tomoyo_realpath_from_path(const struct path
*path
)
254 unsigned int buf_len
= PAGE_SIZE
/ 2;
255 struct dentry
*dentry
= path
->dentry
;
256 struct super_block
*sb
;
265 buf
= kmalloc(buf_len
, GFP_NOFS
);
268 /* To make sure that pos is '\0' terminated. */
269 buf
[buf_len
- 1] = '\0';
270 /* Get better name for socket. */
271 if (sb
->s_magic
== SOCKFS_MAGIC
) {
272 pos
= tomoyo_get_socket_name(path
, buf
, buf_len
- 1);
275 /* For "pipe:[\$]". */
276 if (dentry
->d_op
&& dentry
->d_op
->d_dname
) {
277 pos
= dentry
->d_op
->d_dname(dentry
, buf
, buf_len
- 1);
280 inode
= d_backing_inode(sb
->s_root
);
282 * Get local name for filesystems without rename() operation
283 * or dentry without vfsmount.
286 (!inode
->i_op
->rename
))
287 pos
= tomoyo_get_local_path(path
->dentry
, buf
,
289 /* Get absolute name for the rest. */
291 pos
= tomoyo_get_absolute_path(path
, buf
, buf_len
- 1);
293 * Fall back to local name if absolute name is not
296 if (pos
== ERR_PTR(-EINVAL
))
297 pos
= tomoyo_get_local_path(path
->dentry
, buf
,
303 name
= tomoyo_encode(pos
);
308 tomoyo_warn_oom(__func__
);
313 * tomoyo_realpath_nofollow - Get realpath of a pathname.
315 * @pathname: The pathname to solve.
317 * Returns the realpath of @pathname on success, NULL otherwise.
319 char *tomoyo_realpath_nofollow(const char *pathname
)
323 if (pathname
&& kern_path(pathname
, 0, &path
) == 0) {
324 char *buf
= tomoyo_realpath_from_path(&path
);