2 * security/tomoyo/realpath.c
4 * Pathname calculation functions for TOMOYO.
6 * Copyright (C) 2005-2010 NTT DATA CORPORATION
9 #include <linux/types.h>
10 #include <linux/mount.h>
11 #include <linux/mnt_namespace.h>
12 #include <linux/fs_struct.h>
13 #include <linux/magic.h>
14 #include <linux/slab.h>
17 #include "../../fs/internal.h"
20 * tomoyo_encode: Convert binary string to ascii string.
22 * @str: String in binary format.
24 * Returns pointer to @str in ascii format on success, NULL otherwise.
26 * This function uses kzalloc(), so caller must kfree() if this function
29 char *tomoyo_encode(const char *str
)
39 const unsigned char c
= *p
++;
42 else if (c
> ' ' && c
< 127)
48 /* Reserve space for appending "/". */
49 cp
= kzalloc(len
+ 10, GFP_NOFS
);
55 const unsigned char c
= *p
++;
60 } else if (c
> ' ' && c
< 127) {
64 *cp
++ = (c
>> 6) + '0';
65 *cp
++ = ((c
>> 3) & 7) + '0';
66 *cp
++ = (c
& 7) + '0';
73 * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
75 * @path: Pointer to "struct path".
77 * Returns the realpath of the given @path on success, NULL otherwise.
79 * If dentry is a directory, trailing '/' is appended.
80 * Characters out of 0x20 < c < 0x7F range are converted to
81 * \ooo style octal string.
82 * Character \ is converted to \\ string.
84 * These functions use kzalloc(), so the caller must call kfree()
85 * if these functions didn't return NULL.
87 char *tomoyo_realpath_from_path(struct path
*path
)
91 unsigned int buf_len
= PAGE_SIZE
/ 2;
92 struct dentry
*dentry
= path
->dentry
;
96 is_dir
= dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
);
101 buf
= kmalloc(buf_len
, GFP_NOFS
);
104 /* Get better name for socket. */
105 if (dentry
->d_sb
&& dentry
->d_sb
->s_magic
== SOCKFS_MAGIC
) {
106 struct inode
*inode
= dentry
->d_inode
;
107 struct socket
*sock
= inode
? SOCKET_I(inode
) : NULL
;
108 struct sock
*sk
= sock
? sock
->sk
: NULL
;
110 snprintf(buf
, buf_len
- 1, "socket:[family=%u:"
111 "type=%u:protocol=%u]", sk
->sk_family
,
112 sk
->sk_type
, sk
->sk_protocol
);
114 snprintf(buf
, buf_len
- 1, "socket:[unknown]");
116 name
= tomoyo_encode(buf
);
119 /* For "socket:[\$]" and "pipe:[\$]". */
120 if (dentry
->d_op
&& dentry
->d_op
->d_dname
) {
121 pos
= dentry
->d_op
->d_dname(dentry
, buf
, buf_len
- 1);
124 name
= tomoyo_encode(pos
);
127 /* If we don't have a vfsmount, we can't calculate. */
130 pos
= d_absolute_path(path
, buf
, buf_len
- 1);
131 /* If path is disconnected, use "[unknown]" instead. */
132 if (pos
== ERR_PTR(-EINVAL
)) {
133 name
= tomoyo_encode("[unknown]");
136 /* Prepend "/proc" prefix if using internal proc vfs mount. */
137 if (!IS_ERR(pos
) && (path
->mnt
->mnt_flags
& MNT_INTERNAL
) &&
138 (path
->mnt
->mnt_sb
->s_magic
== PROC_SUPER_MAGIC
)) {
141 memcpy(pos
, "/proc", 5);
143 pos
= ERR_PTR(-ENOMEM
);
147 name
= tomoyo_encode(pos
);
152 tomoyo_warn_oom(__func__
);
153 else if (is_dir
&& *name
) {
154 /* Append trailing '/' if dentry is a directory. */
155 char *pos
= name
+ strlen(name
) - 1;
158 * This is OK because tomoyo_encode() reserves space
167 * tomoyo_realpath_nofollow - Get realpath of a pathname.
169 * @pathname: The pathname to solve.
171 * Returns the realpath of @pathname on success, NULL otherwise.
173 char *tomoyo_realpath_nofollow(const char *pathname
)
177 if (pathname
&& kern_path(pathname
, 0, &path
) == 0) {
178 char *buf
= tomoyo_realpath_from_path(&path
);