hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/security/tomoyo/realpath.c
....@@ -7,6 +7,7 @@
77
88 #include "common.h"
99 #include <linux/magic.h>
10
+#include <linux/proc_fs.h>
1011
1112 /**
1213 * tomoyo_encode2 - Encode binary string to ascii string.
....@@ -94,11 +95,13 @@
9495 const int buflen)
9596 {
9697 char *pos = ERR_PTR(-ENOMEM);
98
+
9799 if (buflen >= 256) {
98100 /* go to whatever namespace root we are under */
99101 pos = d_absolute_path(path, buffer, buflen - 1);
100102 if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
101103 struct inode *inode = d_backing_inode(path->dentry);
104
+
102105 if (inode && S_ISDIR(inode->i_mode)) {
103106 buffer[buflen - 2] = '/';
104107 buffer[buflen - 1] = '\0';
....@@ -123,10 +126,12 @@
123126 const int buflen)
124127 {
125128 char *pos = ERR_PTR(-ENOMEM);
129
+
126130 if (buflen >= 256) {
127131 pos = dentry_path_raw(dentry, buffer, buflen - 1);
128132 if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
129133 struct inode *inode = d_backing_inode(dentry);
134
+
130135 if (inode && S_ISDIR(inode->i_mode)) {
131136 buffer[buflen - 2] = '/';
132137 buffer[buflen - 1] = '\0';
....@@ -150,14 +155,17 @@
150155 {
151156 struct super_block *sb = dentry->d_sb;
152157 char *pos = tomoyo_get_dentry_path(dentry, buffer, buflen);
158
+
153159 if (IS_ERR(pos))
154160 return pos;
155161 /* Convert from $PID to self if $PID is current thread. */
156162 if (sb->s_magic == PROC_SUPER_MAGIC && *pos == '/') {
157163 char *ep;
158164 const pid_t pid = (pid_t) simple_strtoul(pos + 1, &ep, 10);
165
+ struct pid_namespace *proc_pidns = proc_pid_ns(sb);
166
+
159167 if (*ep == '/' && pid && pid ==
160
- task_tgid_nr_ns(current, sb->s_fs_info)) {
168
+ task_tgid_nr_ns(current, proc_pidns)) {
161169 pos = ep - 5;
162170 if (pos < buffer)
163171 goto out;
....@@ -170,6 +178,7 @@
170178 goto prepend_filesystem_name;
171179 {
172180 struct inode *inode = d_backing_inode(sb->s_root);
181
+
173182 /*
174183 * Use filesystem name if filesystem does not support rename()
175184 * operation.
....@@ -182,6 +191,7 @@
182191 char name[64];
183192 int name_len;
184193 const dev_t dev = sb->s_dev;
194
+
185195 name[sizeof(name) - 1] = '\0';
186196 snprintf(name, sizeof(name) - 1, "dev(%u,%u):", MAJOR(dev),
187197 MINOR(dev));
....@@ -197,6 +207,7 @@
197207 {
198208 const char *name = sb->s_type->name;
199209 const int name_len = strlen(name);
210
+
200211 pos -= name_len + 1;
201212 if (pos < buffer)
202213 goto out;
....@@ -206,31 +217,6 @@
206217 return pos;
207218 out:
208219 return ERR_PTR(-ENOMEM);
209
-}
210
-
211
-/**
212
- * tomoyo_get_socket_name - Get the name of a socket.
213
- *
214
- * @path: Pointer to "struct path".
215
- * @buffer: Pointer to buffer to return value in.
216
- * @buflen: Sizeof @buffer.
217
- *
218
- * Returns the buffer.
219
- */
220
-static char *tomoyo_get_socket_name(const struct path *path, char * const buffer,
221
- const int buflen)
222
-{
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;
226
- if (sk) {
227
- snprintf(buffer, buflen, "socket:[family=%u:type=%u:"
228
- "protocol=%u]", sk->sk_family, sk->sk_type,
229
- sk->sk_protocol);
230
- } else {
231
- snprintf(buffer, buflen, "socket:[unknown]");
232
- }
233
- return buffer;
234220 }
235221
236222 /**
....@@ -255,12 +241,14 @@
255241 unsigned int buf_len = PAGE_SIZE / 2;
256242 struct dentry *dentry = path->dentry;
257243 struct super_block *sb;
244
+
258245 if (!dentry)
259246 return NULL;
260247 sb = dentry->d_sb;
261248 while (1) {
262249 char *pos;
263250 struct inode *inode;
251
+
264252 buf_len <<= 1;
265253 kfree(buf);
266254 buf = kmalloc(buf_len, GFP_NOFS);
....@@ -268,12 +256,7 @@
268256 break;
269257 /* To make sure that pos is '\0' terminated. */
270258 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);
274
- goto encode;
275
- }
276
- /* For "pipe:[\$]". */
259
+ /* For "pipe:[\$]" and "socket:[\$]". */
277260 if (dentry->d_op && dentry->d_op->d_dname) {
278261 pos = dentry->d_op->d_dname(dentry, buf, buf_len - 1);
279262 goto encode;
....@@ -284,7 +267,8 @@
284267 * or dentry without vfsmount.
285268 */
286269 if (!path->mnt ||
287
- (!inode->i_op->rename))
270
+ (!inode->i_op->rename &&
271
+ !(sb->s_type->fs_flags & FS_REQUIRES_DEV)))
288272 pos = tomoyo_get_local_path(path->dentry, buf,
289273 buf_len - 1);
290274 /* Get absolute name for the rest. */
....@@ -323,6 +307,7 @@
323307
324308 if (pathname && kern_path(pathname, 0, &path) == 0) {
325309 char *buf = tomoyo_realpath_from_path(&path);
310
+
326311 path_put(&path);
327312 return buf;
328313 }