Skip to content

Commit 5625f2e

Browse files
Tetsuo Handaxjamesmorris
authored andcommitted
TOMOYO: Change pathname for non-rename()able filesystems.
TOMOYO wants to use /proc/self/ rather than /proc/$PID/ if $PID matches current thread's process ID in order to prevent current thread from accessing other process's information unless needed. But since procfs can be mounted on various locations (e.g. /proc/ /proc2/ /p/ /tmp/foo/100/p/ ), TOMOYO cannot tell that whether the numeric part in the string returned by __d_path() represents process ID or not. Therefore, to be able to convert from $PID to self no matter where procfs is mounted, this patch changes pathname representations for filesystems which do not support rename() operation (e.g. proc, sysfs, securityfs). Examples: /proc/self/mounts => proc:/self/mounts /sys/kernel/security/ => sys:/kernel/security/ /dev/pts/0 => devpts:/0 Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Signed-off-by: James Morris <jmorris@namei.org>
1 parent bd03a3e commit 5625f2e

2 files changed

Lines changed: 180 additions & 54 deletions

File tree

‎security/tomoyo/file.c‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ int tomoyo_path_number_perm(const u8 type, struct path *path,
712712
int idx;
713713

714714
if (tomoyo_init_request_info(&r, NULL, tomoyo_pn2mac[type])
715-
== TOMOYO_CONFIG_DISABLED || !path->mnt || !path->dentry)
715+
== TOMOYO_CONFIG_DISABLED || !path->dentry)
716716
return 0;
717717
idx = tomoyo_read_lock();
718718
if (!tomoyo_get_realpath(&buf, path))
@@ -753,8 +753,6 @@ int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
753753
struct tomoyo_request_info r;
754754
int idx;
755755

756-
if (!path->mnt)
757-
return 0;
758756
buf.name = NULL;
759757
r.mode = TOMOYO_CONFIG_DISABLED;
760758
idx = tomoyo_read_lock();
@@ -798,8 +796,6 @@ int tomoyo_path_perm(const u8 operation, struct path *path)
798796
bool is_enforce;
799797
int idx;
800798

801-
if (!path->mnt)
802-
return 0;
803799
if (tomoyo_init_request_info(&r, NULL, tomoyo_p2mac[operation])
804800
== TOMOYO_CONFIG_DISABLED)
805801
return 0;
@@ -842,8 +838,7 @@ int tomoyo_mkdev_perm(const u8 operation, struct path *path,
842838
struct tomoyo_path_info buf;
843839
int idx;
844840

845-
if (!path->mnt ||
846-
tomoyo_init_request_info(&r, NULL, tomoyo_pnnn2mac[operation])
841+
if (tomoyo_init_request_info(&r, NULL, tomoyo_pnnn2mac[operation])
847842
== TOMOYO_CONFIG_DISABLED)
848843
return 0;
849844
idx = tomoyo_read_lock();
@@ -884,8 +879,7 @@ int tomoyo_path2_perm(const u8 operation, struct path *path1,
884879
struct tomoyo_request_info r;
885880
int idx;
886881

887-
if (!path1->mnt || !path2->mnt ||
888-
tomoyo_init_request_info(&r, NULL, tomoyo_pp2mac[operation])
882+
if (tomoyo_init_request_info(&r, NULL, tomoyo_pp2mac[operation])
889883
== TOMOYO_CONFIG_DISABLED)
890884
return 0;
891885
buf1.name = NULL;

‎security/tomoyo/realpath.c‎

Lines changed: 177 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,161 @@ char *tomoyo_encode(const char *str)
6969
return cp0;
7070
}
7171

72+
/**
73+
* tomoyo_get_absolute_path - Get the path of a dentry but ignores chroot'ed root.
74+
*
75+
* @path: Pointer to "struct path".
76+
* @buffer: Pointer to buffer to return value in.
77+
* @buflen: Sizeof @buffer.
78+
*
79+
* Returns the buffer on success, an error code otherwise.
80+
*
81+
* If dentry is a directory, trailing '/' is appended.
82+
*/
83+
static char *tomoyo_get_absolute_path(struct path *path, char * const buffer,
84+
const int buflen)
85+
{
86+
char *pos = ERR_PTR(-ENOMEM);
87+
if (buflen >= 256) {
88+
struct path ns_root = { };
89+
/* go to whatever namespace root we are under */
90+
pos = __d_path(path, &ns_root, buffer, buflen - 1);
91+
if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
92+
struct inode *inode = path->dentry->d_inode;
93+
if (inode && S_ISDIR(inode->i_mode)) {
94+
buffer[buflen - 2] = '/';
95+
buffer[buflen - 1] = '\0';
96+
}
97+
}
98+
}
99+
return pos;
100+
}
101+
102+
/**
103+
* tomoyo_get_dentry_path - Get the path of a dentry.
104+
*
105+
* @dentry: Pointer to "struct dentry".
106+
* @buffer: Pointer to buffer to return value in.
107+
* @buflen: Sizeof @buffer.
108+
*
109+
* Returns the buffer on success, an error code otherwise.
110+
*
111+
* If dentry is a directory, trailing '/' is appended.
112+
*/
113+
static char *tomoyo_get_dentry_path(struct dentry *dentry, char * const buffer,
114+
const int buflen)
115+
{
116+
char *pos = ERR_PTR(-ENOMEM);
117+
if (buflen >= 256) {
118+
pos = dentry_path_raw(dentry, buffer, buflen - 1);
119+
if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
120+
struct inode *inode = dentry->d_inode;
121+
if (inode && S_ISDIR(inode->i_mode)) {
122+
buffer[buflen - 2] = '/';
123+
buffer[buflen - 1] = '\0';
124+
}
125+
}
126+
}
127+
return pos;
128+
}
129+
130+
/**
131+
* tomoyo_get_local_path - Get the path of a dentry.
132+
*
133+
* @dentry: Pointer to "struct dentry".
134+
* @buffer: Pointer to buffer to return value in.
135+
* @buflen: Sizeof @buffer.
136+
*
137+
* Returns the buffer on success, an error code otherwise.
138+
*/
139+
static char *tomoyo_get_local_path(struct dentry *dentry, char * const buffer,
140+
const int buflen)
141+
{
142+
struct super_block *sb = dentry->d_sb;
143+
char *pos = tomoyo_get_dentry_path(dentry, buffer, buflen);
144+
if (IS_ERR(pos))
145+
return pos;
146+
/* Convert from $PID to self if $PID is current thread. */
147+
if (sb->s_magic == PROC_SUPER_MAGIC && *pos == '/') {
148+
char *ep;
149+
const pid_t pid = (pid_t) simple_strtoul(pos + 1, &ep, 10);
150+
if (*ep == '/' && pid && pid ==
151+
task_tgid_nr_ns(current, sb->s_fs_info)) {
152+
pos = ep - 5;
153+
if (pos < buffer)
154+
goto out;
155+
memmove(pos, "/self", 5);
156+
}
157+
goto prepend_filesystem_name;
158+
}
159+
/* Use filesystem name for unnamed devices. */
160+
if (!MAJOR(sb->s_dev))
161+
goto prepend_filesystem_name;
162+
{
163+
struct inode *inode = sb->s_root->d_inode;
164+
/*
165+
* Use filesystem name if filesystem does not support rename()
166+
* operation.
167+
*/
168+
if (inode->i_op && !inode->i_op->rename)
169+
goto prepend_filesystem_name;
170+
}
171+
/* Prepend device name. */
172+
{
173+
char name[64];
174+
int name_len;
175+
const dev_t dev = sb->s_dev;
176+
name[sizeof(name) - 1] = '\0';
177+
snprintf(name, sizeof(name) - 1, "dev(%u,%u):", MAJOR(dev),
178+
MINOR(dev));
179+
name_len = strlen(name);
180+
pos -= name_len;
181+
if (pos < buffer)
182+
goto out;
183+
memmove(pos, name, name_len);
184+
return pos;
185+
}
186+
/* Prepend filesystem name. */
187+
prepend_filesystem_name:
188+
{
189+
const char *name = sb->s_type->name;
190+
const int name_len = strlen(name);
191+
pos -= name_len + 1;
192+
if (pos < buffer)
193+
goto out;
194+
memmove(pos, name, name_len);
195+
pos[name_len] = ':';
196+
}
197+
return pos;
198+
out:
199+
return ERR_PTR(-ENOMEM);
200+
}
201+
202+
/**
203+
* tomoyo_get_socket_name - Get the name of a socket.
204+
*
205+
* @path: Pointer to "struct path".
206+
* @buffer: Pointer to buffer to return value in.
207+
* @buflen: Sizeof @buffer.
208+
*
209+
* Returns the buffer.
210+
*/
211+
static char *tomoyo_get_socket_name(struct path *path, char * const buffer,
212+
const int buflen)
213+
{
214+
struct inode *inode = path->dentry->d_inode;
215+
struct socket *sock = inode ? SOCKET_I(inode) : NULL;
216+
struct sock *sk = sock ? sock->sk : NULL;
217+
if (sk) {
218+
snprintf(buffer, buflen, "socket:[family=%u:type=%u:"
219+
"protocol=%u]", sk->sk_family, sk->sk_type,
220+
sk->sk_protocol);
221+
} else {
222+
snprintf(buffer, buflen, "socket:[unknown]");
223+
}
224+
return buffer;
225+
}
226+
72227
/**
73228
* tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root.
74229
*
@@ -90,55 +245,42 @@ char *tomoyo_realpath_from_path(struct path *path)
90245
char *name = NULL;
91246
unsigned int buf_len = PAGE_SIZE / 2;
92247
struct dentry *dentry = path->dentry;
93-
bool is_dir;
248+
struct super_block *sb;
94249
if (!dentry)
95250
return NULL;
96-
is_dir = dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode);
251+
sb = dentry->d_sb;
97252
while (1) {
98-
struct path ns_root = { .mnt = NULL, .dentry = NULL };
99253
char *pos;
254+
struct inode *inode;
100255
buf_len <<= 1;
101256
kfree(buf);
102257
buf = kmalloc(buf_len, GFP_NOFS);
103258
if (!buf)
104259
break;
260+
/* To make sure that pos is '\0' terminated. */
261+
buf[buf_len - 1] = '\0';
105262
/* Get better name for socket. */
106-
if (dentry->d_sb && dentry->d_sb->s_magic == SOCKFS_MAGIC) {
107-
struct inode *inode = dentry->d_inode;
108-
struct socket *sock = inode ? SOCKET_I(inode) : NULL;
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;
263+
if (sb->s_magic == SOCKFS_MAGIC) {
264+
pos = tomoyo_get_socket_name(path, buf, buf_len - 1);
265+
goto encode;
119266
}
120-
/* For "socket:[\$]" and "pipe:[\$]". */
267+
/* For "pipe:[\$]". */
121268
if (dentry->d_op && dentry->d_op->d_dname) {
122269
pos = dentry->d_op->d_dname(dentry, buf, buf_len - 1);
123-
if (IS_ERR(pos))
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);
270+
goto encode;
141271
}
272+
inode = sb->s_root->d_inode;
273+
/*
274+
* Get local name for filesystems without rename() operation
275+
* or dentry without vfsmount.
276+
*/
277+
if (!path->mnt || (inode->i_op && !inode->i_op->rename))
278+
pos = tomoyo_get_local_path(path->dentry, buf,
279+
buf_len - 1);
280+
/* Get absolute name for the rest. */
281+
else
282+
pos = tomoyo_get_absolute_path(path, buf, buf_len - 1);
283+
encode:
142284
if (IS_ERR(pos))
143285
continue;
144286
name = tomoyo_encode(pos);
@@ -147,16 +289,6 @@ char *tomoyo_realpath_from_path(struct path *path)
147289
kfree(buf);
148290
if (!name)
149291
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-
}
160292
return name;
161293
}
162294

0 commit comments

Comments
 (0)