]>
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
10 * tomoyo_encode2 - Encode binary string to ascii string.
12 * @str: String in binary format.
13 * @str_len: Size of @str in byte.
15 * Returns pointer to @str in ascii format on success, NULL otherwise.
17 * This function uses kzalloc(), so caller must kfree() if this function
20 char *tomoyo_encode2(const char *str
, int str_len
)
30 for (i
= 0; i
< str_len
; i
++) {
31 const unsigned char c
= p
[i
];
35 else if (c
> ' ' && c
< 127)
41 /* Reserve space for appending "/". */
42 cp
= kzalloc(len
+ 10, GFP_NOFS
);
47 for (i
= 0; i
< str_len
; i
++) {
48 const unsigned char c
= p
[i
];
53 } else if (c
> ' ' && c
< 127) {
57 *cp
++ = (c
>> 6) + '0';
58 *cp
++ = ((c
>> 3) & 7) + '0';
59 *cp
++ = (c
& 7) + '0';
66 * tomoyo_encode - Encode binary string to ascii string.
68 * @str: String in binary format.
70 * Returns pointer to @str in ascii format on success, NULL otherwise.
72 * This function uses kzalloc(), so caller must kfree() if this function
75 char *tomoyo_encode(const char *str
)
77 return str
? tomoyo_encode2(str
, strlen(str
)) : NULL
;
81 * tomoyo_get_absolute_path - Get the path of a dentry but ignores chroot'ed root.
83 * @path: Pointer to "struct path".
84 * @buffer: Pointer to buffer to return value in.
85 * @buflen: Sizeof @buffer.
87 * Returns the buffer on success, an error code otherwise.
89 * If dentry is a directory, trailing '/' is appended.
91 static char *tomoyo_get_absolute_path(struct path
*path
, char * const buffer
,
94 char *pos
= ERR_PTR(-ENOMEM
);
96 /* go to whatever namespace root we are under */
97 pos
= d_absolute_path(path
, buffer
, buflen
- 1);
98 if (!IS_ERR(pos
) && *pos
== '/' && pos
[1]) {
99 struct inode
*inode
= path
->dentry
->d_inode
;
100 if (inode
&& S_ISDIR(inode
->i_mode
)) {
101 buffer
[buflen
- 2] = '/';
102 buffer
[buflen
- 1] = '\0';
110 * tomoyo_get_dentry_path - Get the path of a dentry.
112 * @dentry: Pointer to "struct dentry".
113 * @buffer: Pointer to buffer to return value in.
114 * @buflen: Sizeof @buffer.
116 * Returns the buffer on success, an error code otherwise.
118 * If dentry is a directory, trailing '/' is appended.
120 static char *tomoyo_get_dentry_path(struct dentry
*dentry
, char * const buffer
,
123 char *pos
= ERR_PTR(-ENOMEM
);
125 pos
= dentry_path_raw(dentry
, buffer
, buflen
- 1);
126 if (!IS_ERR(pos
) && *pos
== '/' && pos
[1]) {
127 struct inode
*inode
= dentry
->d_inode
;
128 if (inode
&& S_ISDIR(inode
->i_mode
)) {
129 buffer
[buflen
- 2] = '/';
130 buffer
[buflen
- 1] = '\0';
138 * tomoyo_get_local_path - Get the path of a dentry.
140 * @dentry: Pointer to "struct dentry".
141 * @buffer: Pointer to buffer to return value in.
142 * @buflen: Sizeof @buffer.
144 * Returns the buffer on success, an error code otherwise.
146 static char *tomoyo_get_local_path(struct dentry
*dentry
, char * const buffer
,
149 struct super_block
*sb
= dentry
->d_sb
;
150 char *pos
= tomoyo_get_dentry_path(dentry
, buffer
, buflen
);
153 /* Convert from $PID to self if $PID is current thread. */
154 if (sb
->s_magic
== PROC_SUPER_MAGIC
&& *pos
== '/') {
156 const pid_t pid
= (pid_t
) simple_strtoul(pos
+ 1, &ep
, 10);
157 if (*ep
== '/' && pid
&& pid
==
158 task_tgid_nr_ns(current
, sb
->s_fs_info
)) {
162 memmove(pos
, "/self", 5);
164 goto prepend_filesystem_name
;
166 /* Use filesystem name for unnamed devices. */
167 if (!MAJOR(sb
->s_dev
))
168 goto prepend_filesystem_name
;
170 struct inode
*inode
= sb
->s_root
->d_inode
;
172 * Use filesystem name if filesystem does not support rename()
175 if (inode
->i_op
&& !inode
->i_op
->rename
)
176 goto prepend_filesystem_name
;
178 /* Prepend device name. */
182 const dev_t dev
= sb
->s_dev
;
183 name
[sizeof(name
) - 1] = '\0';
184 snprintf(name
, sizeof(name
) - 1, "dev(%u,%u):", MAJOR(dev
),
186 name_len
= strlen(name
);
190 memmove(pos
, name
, name_len
);
193 /* Prepend filesystem name. */
194 prepend_filesystem_name
:
196 const char *name
= sb
->s_type
->name
;
197 const int name_len
= strlen(name
);
201 memmove(pos
, name
, name_len
);
206 return ERR_PTR(-ENOMEM
);
210 * tomoyo_get_socket_name - Get the name of a socket.
212 * @path: Pointer to "struct path".
213 * @buffer: Pointer to buffer to return value in.
214 * @buflen: Sizeof @buffer.
216 * Returns the buffer.
218 static char *tomoyo_get_socket_name(struct path
*path
, char * const buffer
,
221 struct inode
*inode
= path
->dentry
->d_inode
;
222 struct socket
*sock
= inode
? SOCKET_I(inode
) : NULL
;
223 struct sock
*sk
= sock
? sock
->sk
: NULL
;
225 snprintf(buffer
, buflen
, "socket:[family=%u:type=%u:"
226 "protocol=%u]", sk
->sk_family
, sk
->sk_type
,
229 snprintf(buffer
, buflen
, "socket:[unknown]");
235 * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
237 * @path: Pointer to "struct path".
239 * Returns the realpath of the given @path on success, NULL otherwise.
241 * If dentry is a directory, trailing '/' is appended.
242 * Characters out of 0x20 < c < 0x7F range are converted to
243 * \ooo style octal string.
244 * Character \ is converted to \\ string.
246 * These functions use kzalloc(), so the caller must call kfree()
247 * if these functions didn't return NULL.
249 char *tomoyo_realpath_from_path(struct path
*path
)
253 unsigned int buf_len
= PAGE_SIZE
/ 2;
254 struct dentry
*dentry
= path
->dentry
;
255 struct super_block
*sb
;
264 buf
= kmalloc(buf_len
, GFP_NOFS
);
267 /* To make sure that pos is '\0' terminated. */
268 buf
[buf_len
- 1] = '\0';
269 /* Get better name for socket. */
270 if (sb
->s_magic
== SOCKFS_MAGIC
) {
271 pos
= tomoyo_get_socket_name(path
, buf
, buf_len
- 1);
274 /* For "pipe:[\$]". */
275 if (dentry
->d_op
&& dentry
->d_op
->d_dname
) {
276 pos
= dentry
->d_op
->d_dname(dentry
, buf
, buf_len
- 1);
279 inode
= sb
->s_root
->d_inode
;
281 * Get local name for filesystems without rename() operation
282 * or dentry without vfsmount.
284 if (!path
->mnt
|| (inode
->i_op
&& !inode
->i_op
->rename
))
285 pos
= tomoyo_get_local_path(path
->dentry
, buf
,
287 /* Get absolute name for the rest. */
289 pos
= tomoyo_get_absolute_path(path
, buf
, buf_len
- 1);
291 * Fall back to local name if absolute name is not
294 if (pos
== ERR_PTR(-EINVAL
))
295 pos
= tomoyo_get_local_path(path
->dentry
, buf
,
301 name
= tomoyo_encode(pos
);
306 tomoyo_warn_oom(__func__
);
311 * tomoyo_realpath_nofollow - Get realpath of a pathname.
313 * @pathname: The pathname to solve.
315 * Returns the realpath of @pathname on success, NULL otherwise.
317 char *tomoyo_realpath_nofollow(const char *pathname
)
321 if (pathname
&& kern_path(pathname
, 0, &path
) == 0) {
322 char *buf
= tomoyo_realpath_from_path(&path
);