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