diff options
Diffstat (limited to 'security/tomoyo/realpath.c')
-rw-r--r-- | security/tomoyo/realpath.c | 226 |
1 files changed, 178 insertions, 48 deletions
diff --git a/security/tomoyo/realpath.c b/security/tomoyo/realpath.c index 8d95e91c9fc4..6c601bd300f3 100644 --- a/security/tomoyo/realpath.c +++ b/security/tomoyo/realpath.c | |||
@@ -1,9 +1,7 @@ | |||
1 | /* | 1 | /* |
2 | * security/tomoyo/realpath.c | 2 | * security/tomoyo/realpath.c |
3 | * | 3 | * |
4 | * Pathname calculation functions for TOMOYO. | 4 | * Copyright (C) 2005-2011 NTT DATA CORPORATION |
5 | * | ||
6 | * Copyright (C) 2005-2010 NTT DATA CORPORATION | ||
7 | */ | 5 | */ |
8 | 6 | ||
9 | #include <linux/types.h> | 7 | #include <linux/types.h> |
@@ -70,6 +68,161 @@ char *tomoyo_encode(const char *str) | |||
70 | } | 68 | } |
71 | 69 | ||
72 | /** | 70 | /** |
71 | * tomoyo_get_absolute_path - Get the path of a dentry but ignores chroot'ed root. | ||
72 | * | ||
73 | * @path: Pointer to "struct path". | ||
74 | * @buffer: Pointer to buffer to return value in. | ||
75 | * @buflen: Sizeof @buffer. | ||
76 | * | ||
77 | * Returns the buffer on success, an error code otherwise. | ||
78 | * | ||
79 | * If dentry is a directory, trailing '/' is appended. | ||
80 | */ | ||
81 | static char *tomoyo_get_absolute_path(struct path *path, char * const buffer, | ||
82 | const int buflen) | ||
83 | { | ||
84 | char *pos = ERR_PTR(-ENOMEM); | ||
85 | if (buflen >= 256) { | ||
86 | struct path ns_root = { }; | ||
87 | /* go to whatever namespace root we are under */ | ||
88 | pos = __d_path(path, &ns_root, buffer, buflen - 1); | ||
89 | if (!IS_ERR(pos) && *pos == '/' && pos[1]) { | ||
90 | struct inode *inode = path->dentry->d_inode; | ||
91 | if (inode && S_ISDIR(inode->i_mode)) { | ||
92 | buffer[buflen - 2] = '/'; | ||
93 | buffer[buflen - 1] = '\0'; | ||
94 | } | ||
95 | } | ||
96 | } | ||
97 | return pos; | ||
98 | } | ||
99 | |||
100 | /** | ||
101 | * tomoyo_get_dentry_path - Get the path of a dentry. | ||
102 | * | ||
103 | * @dentry: Pointer to "struct dentry". | ||
104 | * @buffer: Pointer to buffer to return value in. | ||
105 | * @buflen: Sizeof @buffer. | ||
106 | * | ||
107 | * Returns the buffer on success, an error code otherwise. | ||
108 | * | ||
109 | * If dentry is a directory, trailing '/' is appended. | ||
110 | */ | ||
111 | static char *tomoyo_get_dentry_path(struct dentry *dentry, char * const buffer, | ||
112 | const int buflen) | ||
113 | { | ||
114 | char *pos = ERR_PTR(-ENOMEM); | ||
115 | if (buflen >= 256) { | ||
116 | pos = dentry_path_raw(dentry, buffer, buflen - 1); | ||
117 | if (!IS_ERR(pos) && *pos == '/' && pos[1]) { | ||
118 | struct inode *inode = dentry->d_inode; | ||
119 | if (inode && S_ISDIR(inode->i_mode)) { | ||
120 | buffer[buflen - 2] = '/'; | ||
121 | buffer[buflen - 1] = '\0'; | ||
122 | } | ||
123 | } | ||
124 | } | ||
125 | return pos; | ||
126 | } | ||
127 | |||
128 | /** | ||
129 | * tomoyo_get_local_path - Get the path of a dentry. | ||
130 | * | ||
131 | * @dentry: Pointer to "struct dentry". | ||
132 | * @buffer: Pointer to buffer to return value in. | ||
133 | * @buflen: Sizeof @buffer. | ||
134 | * | ||
135 | * Returns the buffer on success, an error code otherwise. | ||
136 | */ | ||
137 | static char *tomoyo_get_local_path(struct dentry *dentry, char * const buffer, | ||
138 | const int buflen) | ||
139 | { | ||
140 | struct super_block *sb = dentry->d_sb; | ||
141 | char *pos = tomoyo_get_dentry_path(dentry, buffer, buflen); | ||
142 | if (IS_ERR(pos)) | ||
143 | return pos; | ||
144 | /* Convert from $PID to self if $PID is current thread. */ | ||
145 | if (sb->s_magic == PROC_SUPER_MAGIC && *pos == '/') { | ||
146 | char *ep; | ||
147 | const pid_t pid = (pid_t) simple_strtoul(pos + 1, &ep, 10); | ||
148 | if (*ep == '/' && pid && pid == | ||
149 | task_tgid_nr_ns(current, sb->s_fs_info)) { | ||
150 | pos = ep - 5; | ||
151 | if (pos < buffer) | ||
152 | goto out; | ||
153 | memmove(pos, "/self", 5); | ||
154 | } | ||
155 | goto prepend_filesystem_name; | ||
156 | } | ||
157 | /* Use filesystem name for unnamed devices. */ | ||
158 | if (!MAJOR(sb->s_dev)) | ||
159 | goto prepend_filesystem_name; | ||
160 | { | ||
161 | struct inode *inode = sb->s_root->d_inode; | ||
162 | /* | ||
163 | * Use filesystem name if filesystem does not support rename() | ||
164 | * operation. | ||
165 | */ | ||
166 | if (inode->i_op && !inode->i_op->rename) | ||
167 | goto prepend_filesystem_name; | ||
168 | } | ||
169 | /* Prepend device name. */ | ||
170 | { | ||
171 | char name[64]; | ||
172 | int name_len; | ||
173 | const dev_t dev = sb->s_dev; | ||
174 | name[sizeof(name) - 1] = '\0'; | ||
175 | snprintf(name, sizeof(name) - 1, "dev(%u,%u):", MAJOR(dev), | ||
176 | MINOR(dev)); | ||
177 | name_len = strlen(name); | ||
178 | pos -= name_len; | ||
179 | if (pos < buffer) | ||
180 | goto out; | ||
181 | memmove(pos, name, name_len); | ||
182 | return pos; | ||
183 | } | ||
184 | /* Prepend filesystem name. */ | ||
185 | prepend_filesystem_name: | ||
186 | { | ||
187 | const char *name = sb->s_type->name; | ||
188 | const int name_len = strlen(name); | ||
189 | pos -= name_len + 1; | ||
190 | if (pos < buffer) | ||
191 | goto out; | ||
192 | memmove(pos, name, name_len); | ||
193 | pos[name_len] = ':'; | ||
194 | } | ||
195 | return pos; | ||
196 | out: | ||
197 | return ERR_PTR(-ENOMEM); | ||
198 | } | ||
199 | |||
200 | /** | ||
201 | * tomoyo_get_socket_name - Get the name of a socket. | ||
202 | * | ||
203 | * @path: Pointer to "struct path". | ||
204 | * @buffer: Pointer to buffer to return value in. | ||
205 | * @buflen: Sizeof @buffer. | ||
206 | * | ||
207 | * Returns the buffer. | ||
208 | */ | ||
209 | static char *tomoyo_get_socket_name(struct path *path, char * const buffer, | ||
210 | const int buflen) | ||
211 | { | ||
212 | struct inode *inode = path->dentry->d_inode; | ||
213 | struct socket *sock = inode ? SOCKET_I(inode) : NULL; | ||
214 | struct sock *sk = sock ? sock->sk : NULL; | ||
215 | if (sk) { | ||
216 | snprintf(buffer, buflen, "socket:[family=%u:type=%u:" | ||
217 | "protocol=%u]", sk->sk_family, sk->sk_type, | ||
218 | sk->sk_protocol); | ||
219 | } else { | ||
220 | snprintf(buffer, buflen, "socket:[unknown]"); | ||
221 | } | ||
222 | return buffer; | ||
223 | } | ||
224 | |||
225 | /** | ||
73 | * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root. | 226 | * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root. |
74 | * | 227 | * |
75 | * @path: Pointer to "struct path". | 228 | * @path: Pointer to "struct path". |
@@ -90,55 +243,42 @@ char *tomoyo_realpath_from_path(struct path *path) | |||
90 | char *name = NULL; | 243 | char *name = NULL; |
91 | unsigned int buf_len = PAGE_SIZE / 2; | 244 | unsigned int buf_len = PAGE_SIZE / 2; |
92 | struct dentry *dentry = path->dentry; | 245 | struct dentry *dentry = path->dentry; |
93 | bool is_dir; | 246 | struct super_block *sb; |
94 | if (!dentry) | 247 | if (!dentry) |
95 | return NULL; | 248 | return NULL; |
96 | is_dir = dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode); | 249 | sb = dentry->d_sb; |
97 | while (1) { | 250 | while (1) { |
98 | struct path ns_root = { .mnt = NULL, .dentry = NULL }; | ||
99 | char *pos; | 251 | char *pos; |
252 | struct inode *inode; | ||
100 | buf_len <<= 1; | 253 | buf_len <<= 1; |
101 | kfree(buf); | 254 | kfree(buf); |
102 | buf = kmalloc(buf_len, GFP_NOFS); | 255 | buf = kmalloc(buf_len, GFP_NOFS); |
103 | if (!buf) | 256 | if (!buf) |
104 | break; | 257 | break; |
258 | /* To make sure that pos is '\0' terminated. */ | ||
259 | buf[buf_len - 1] = '\0'; | ||
105 | /* Get better name for socket. */ | 260 | /* Get better name for socket. */ |
106 | if (dentry->d_sb->s_magic == SOCKFS_MAGIC) { | 261 | if (sb->s_magic == SOCKFS_MAGIC) { |
107 | struct inode *inode = dentry->d_inode; | 262 | pos = tomoyo_get_socket_name(path, buf, buf_len - 1); |
108 | struct socket *sock = inode ? SOCKET_I(inode) : NULL; | 263 | goto encode; |
109 | struct sock *sk = sock ? sock->sk : NULL; | ||
110 | if (sk) { | ||
111 | snprintf(buf, buf_len - 1, "socket:[family=%u:" | ||
112 | "type=%u:protocol=%u]", sk->sk_family, | ||
113 | sk->sk_type, sk->sk_protocol); | ||
114 | } else { | ||
115 | snprintf(buf, buf_len - 1, "socket:[unknown]"); | ||
116 | } | ||
117 | name = tomoyo_encode(buf); | ||
118 | break; | ||
119 | } | 264 | } |
120 | /* For "socket:[\$]" and "pipe:[\$]". */ | 265 | /* For "pipe:[\$]". */ |
121 | if (dentry->d_op && dentry->d_op->d_dname) { | 266 | if (dentry->d_op && dentry->d_op->d_dname) { |
122 | pos = dentry->d_op->d_dname(dentry, buf, buf_len - 1); | 267 | pos = dentry->d_op->d_dname(dentry, buf, buf_len - 1); |
123 | if (IS_ERR(pos)) | 268 | goto encode; |
124 | continue; | ||
125 | name = tomoyo_encode(pos); | ||
126 | break; | ||
127 | } | ||
128 | /* If we don't have a vfsmount, we can't calculate. */ | ||
129 | if (!path->mnt) | ||
130 | break; | ||
131 | /* go to whatever namespace root we are under */ | ||
132 | pos = __d_path(path, &ns_root, buf, buf_len); | ||
133 | /* Prepend "/proc" prefix if using internal proc vfs mount. */ | ||
134 | if (!IS_ERR(pos) && (path->mnt->mnt_flags & MNT_INTERNAL) && | ||
135 | (path->mnt->mnt_sb->s_magic == PROC_SUPER_MAGIC)) { | ||
136 | pos -= 5; | ||
137 | if (pos >= buf) | ||
138 | memcpy(pos, "/proc", 5); | ||
139 | else | ||
140 | pos = ERR_PTR(-ENOMEM); | ||
141 | } | 269 | } |
270 | inode = sb->s_root->d_inode; | ||
271 | /* | ||
272 | * Get local name for filesystems without rename() operation | ||
273 | * or dentry without vfsmount. | ||
274 | */ | ||
275 | if (!path->mnt || (inode->i_op && !inode->i_op->rename)) | ||
276 | pos = tomoyo_get_local_path(path->dentry, buf, | ||
277 | buf_len - 1); | ||
278 | /* Get absolute name for the rest. */ | ||
279 | else | ||
280 | pos = tomoyo_get_absolute_path(path, buf, buf_len - 1); | ||
281 | encode: | ||
142 | if (IS_ERR(pos)) | 282 | if (IS_ERR(pos)) |
143 | continue; | 283 | continue; |
144 | name = tomoyo_encode(pos); | 284 | name = tomoyo_encode(pos); |
@@ -147,16 +287,6 @@ char *tomoyo_realpath_from_path(struct path *path) | |||
147 | kfree(buf); | 287 | kfree(buf); |
148 | if (!name) | 288 | if (!name) |
149 | tomoyo_warn_oom(__func__); | 289 | tomoyo_warn_oom(__func__); |
150 | else if (is_dir && *name) { | ||
151 | /* Append trailing '/' if dentry is a directory. */ | ||
152 | char *pos = name + strlen(name) - 1; | ||
153 | if (*pos != '/') | ||
154 | /* | ||
155 | * This is OK because tomoyo_encode() reserves space | ||
156 | * for appending "/". | ||
157 | */ | ||
158 | *++pos = '/'; | ||
159 | } | ||
160 | return name; | 290 | return name; |
161 | } | 291 | } |
162 | 292 | ||