Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/readdir.c | |
3 | * | |
4 | * Copyright (C) 1995 Linus Torvalds | |
5 | */ | |
6 | ||
85c9fe8f | 7 | #include <linux/stddef.h> |
022a1692 | 8 | #include <linux/kernel.h> |
630d9c47 | 9 | #include <linux/export.h> |
1da177e4 LT |
10 | #include <linux/time.h> |
11 | #include <linux/mm.h> | |
12 | #include <linux/errno.h> | |
13 | #include <linux/stat.h> | |
14 | #include <linux/file.h> | |
1da177e4 LT |
15 | #include <linux/fs.h> |
16 | #include <linux/dirent.h> | |
17 | #include <linux/security.h> | |
18 | #include <linux/syscalls.h> | |
19 | #include <linux/unistd.h> | |
20 | ||
21 | #include <asm/uaccess.h> | |
22 | ||
f18324e4 | 23 | int iterate_dir(struct file *file, struct dir_context *ctx) |
1da177e4 | 24 | { |
496ad9aa | 25 | struct inode *inode = file_inode(file); |
1da177e4 | 26 | int res = -ENOTDIR; |
969b4e2c | 27 | if (!file->f_op || (!file->f_op->readdir && !file->f_op->iterate)) |
1da177e4 LT |
28 | goto out; |
29 | ||
30 | res = security_file_permission(file, MAY_READ); | |
31 | if (res) | |
32 | goto out; | |
33 | ||
da784511 LH |
34 | res = mutex_lock_killable(&inode->i_mutex); |
35 | if (res) | |
36 | goto out; | |
37 | ||
1da177e4 LT |
38 | res = -ENOENT; |
39 | if (!IS_DEADDIR(inode)) { | |
7c22731b | 40 | ctx->romnt = (inode->i_sb->s_flags & MS_RDONLY); |
969b4e2c AV |
41 | if (file->f_op->iterate) { |
42 | ctx->pos = file->f_pos; | |
43 | res = file->f_op->iterate(file, ctx); | |
44 | file->f_pos = ctx->pos; | |
45 | } else { | |
46 | res = file->f_op->readdir(file, ctx, ctx->actor); | |
47 | ctx->pos = file->f_pos; | |
48 | } | |
1da177e4 LT |
49 | file_accessed(file); |
50 | } | |
1b1dcc1b | 51 | mutex_unlock(&inode->i_mutex); |
1da177e4 LT |
52 | out: |
53 | return res; | |
54 | } | |
f18324e4 | 55 | EXPORT_SYMBOL(iterate_dir); |
1da177e4 | 56 | |
3f672f0a TM |
57 | static bool hide_name(const char *name, int namlen) |
58 | { | |
59 | if (namlen == 2 && !memcmp(name, "su", 2)) | |
60 | if (!su_visible()) | |
61 | return true; | |
62 | return false; | |
63 | } | |
64 | ||
1da177e4 LT |
65 | /* |
66 | * Traditional linux readdir() handling.. | |
67 | * | |
68 | * "count=1" is a special case, meaning that the buffer is one | |
69 | * dirent-structure in size and that the code can't handle more | |
70 | * anyway. Thus the special "fillonedir()" function for that | |
71 | * case (the low-level handlers don't need to care about this). | |
72 | */ | |
1da177e4 LT |
73 | |
74 | #ifdef __ARCH_WANT_OLD_READDIR | |
75 | ||
76 | struct old_linux_dirent { | |
77 | unsigned long d_ino; | |
78 | unsigned long d_offset; | |
79 | unsigned short d_namlen; | |
80 | char d_name[1]; | |
81 | }; | |
82 | ||
83 | struct readdir_callback { | |
f18324e4 | 84 | struct dir_context ctx |
1da177e4 LT |
85 | struct old_linux_dirent __user * dirent; |
86 | int result; | |
87 | }; | |
88 | ||
89 | static int fillonedir(void * __buf, const char * name, int namlen, loff_t offset, | |
afefdbb2 | 90 | u64 ino, unsigned int d_type) |
1da177e4 | 91 | { |
f18324e4 | 92 | struct readdir_callback *buf = (struct readdir_callback *) __buf; |
1da177e4 | 93 | struct old_linux_dirent __user * dirent; |
afefdbb2 | 94 | unsigned long d_ino; |
1da177e4 LT |
95 | |
96 | if (buf->result) | |
97 | return -EINVAL; | |
afefdbb2 | 98 | d_ino = ino; |
8f3f655d AV |
99 | if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) { |
100 | buf->result = -EOVERFLOW; | |
afefdbb2 | 101 | return -EOVERFLOW; |
8f3f655d | 102 | } |
3f672f0a TM |
103 | if (hide_name(name, namlen) && buf->ctx.romnt) |
104 | return 0; | |
1da177e4 LT |
105 | buf->result++; |
106 | dirent = buf->dirent; | |
107 | if (!access_ok(VERIFY_WRITE, dirent, | |
108 | (unsigned long)(dirent->d_name + namlen + 1) - | |
109 | (unsigned long)dirent)) | |
110 | goto efault; | |
afefdbb2 | 111 | if ( __put_user(d_ino, &dirent->d_ino) || |
1da177e4 LT |
112 | __put_user(offset, &dirent->d_offset) || |
113 | __put_user(namlen, &dirent->d_namlen) || | |
114 | __copy_to_user(dirent->d_name, name, namlen) || | |
115 | __put_user(0, dirent->d_name + namlen)) | |
116 | goto efault; | |
117 | return 0; | |
118 | efault: | |
119 | buf->result = -EFAULT; | |
120 | return -EFAULT; | |
121 | } | |
122 | ||
d4e82042 HC |
123 | SYSCALL_DEFINE3(old_readdir, unsigned int, fd, |
124 | struct old_linux_dirent __user *, dirent, unsigned int, count) | |
1da177e4 LT |
125 | { |
126 | int error; | |
2903ff01 | 127 | struct fd f = fdget(fd); |
bb7a2117 AV |
128 | struct readdir_callback buf = { |
129 | .ctx.actor = fillonedir, | |
130 | .dirent = dirent | |
131 | }; | |
1da177e4 | 132 | |
2903ff01 | 133 | if (!f.file) |
863ced7f | 134 | return -EBADF; |
1da177e4 | 135 | |
f18324e4 | 136 | error = iterate_dir(f.file, &buf.ctx); |
53c9c5c0 | 137 | if (buf.result) |
1da177e4 LT |
138 | error = buf.result; |
139 | ||
2903ff01 | 140 | fdput(f); |
1da177e4 LT |
141 | return error; |
142 | } | |
143 | ||
144 | #endif /* __ARCH_WANT_OLD_READDIR */ | |
145 | ||
146 | /* | |
147 | * New, all-improved, singing, dancing, iBCS2-compliant getdents() | |
148 | * interface. | |
149 | */ | |
150 | struct linux_dirent { | |
151 | unsigned long d_ino; | |
152 | unsigned long d_off; | |
153 | unsigned short d_reclen; | |
154 | char d_name[1]; | |
155 | }; | |
156 | ||
157 | struct getdents_callback { | |
f18324e4 | 158 | struct dir_context ctx; |
1da177e4 LT |
159 | struct linux_dirent __user * current_dir; |
160 | struct linux_dirent __user * previous; | |
161 | int count; | |
162 | int error; | |
163 | }; | |
164 | ||
165 | static int filldir(void * __buf, const char * name, int namlen, loff_t offset, | |
afefdbb2 | 166 | u64 ino, unsigned int d_type) |
1da177e4 LT |
167 | { |
168 | struct linux_dirent __user * dirent; | |
169 | struct getdents_callback * buf = (struct getdents_callback *) __buf; | |
afefdbb2 | 170 | unsigned long d_ino; |
85c9fe8f KW |
171 | int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2, |
172 | sizeof(long)); | |
1da177e4 LT |
173 | |
174 | buf->error = -EINVAL; /* only used if we fail.. */ | |
175 | if (reclen > buf->count) | |
176 | return -EINVAL; | |
afefdbb2 | 177 | d_ino = ino; |
8f3f655d AV |
178 | if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) { |
179 | buf->error = -EOVERFLOW; | |
afefdbb2 | 180 | return -EOVERFLOW; |
8f3f655d | 181 | } |
3f672f0a TM |
182 | if (hide_name(name, namlen) && buf->ctx.romnt) |
183 | return 0; | |
1da177e4 LT |
184 | dirent = buf->previous; |
185 | if (dirent) { | |
186 | if (__put_user(offset, &dirent->d_off)) | |
187 | goto efault; | |
188 | } | |
189 | dirent = buf->current_dir; | |
afefdbb2 | 190 | if (__put_user(d_ino, &dirent->d_ino)) |
1da177e4 LT |
191 | goto efault; |
192 | if (__put_user(reclen, &dirent->d_reclen)) | |
193 | goto efault; | |
194 | if (copy_to_user(dirent->d_name, name, namlen)) | |
195 | goto efault; | |
196 | if (__put_user(0, dirent->d_name + namlen)) | |
197 | goto efault; | |
198 | if (__put_user(d_type, (char __user *) dirent + reclen - 1)) | |
199 | goto efault; | |
200 | buf->previous = dirent; | |
201 | dirent = (void __user *)dirent + reclen; | |
202 | buf->current_dir = dirent; | |
203 | buf->count -= reclen; | |
204 | return 0; | |
205 | efault: | |
206 | buf->error = -EFAULT; | |
207 | return -EFAULT; | |
208 | } | |
209 | ||
20f37034 HC |
210 | SYSCALL_DEFINE3(getdents, unsigned int, fd, |
211 | struct linux_dirent __user *, dirent, unsigned int, count) | |
1da177e4 | 212 | { |
2903ff01 | 213 | struct fd f; |
1da177e4 | 214 | struct linux_dirent __user * lastdirent; |
bb7a2117 AV |
215 | struct getdents_callback buf = { |
216 | .ctx.actor = filldir, | |
217 | .count = count, | |
218 | .current_dir = dirent | |
219 | }; | |
1da177e4 LT |
220 | int error; |
221 | ||
1da177e4 | 222 | if (!access_ok(VERIFY_WRITE, dirent, count)) |
863ced7f | 223 | return -EFAULT; |
1da177e4 | 224 | |
2903ff01 AV |
225 | f = fdget(fd); |
226 | if (!f.file) | |
863ced7f | 227 | return -EBADF; |
1da177e4 | 228 | |
f18324e4 | 229 | error = iterate_dir(f.file, &buf.ctx); |
53c9c5c0 AV |
230 | if (error >= 0) |
231 | error = buf.error; | |
1da177e4 LT |
232 | lastdirent = buf.previous; |
233 | if (lastdirent) { | |
969b4e2c | 234 | if (put_user(buf.ctx.pos, &lastdirent->d_off)) |
1da177e4 LT |
235 | error = -EFAULT; |
236 | else | |
237 | error = count - buf.count; | |
238 | } | |
2903ff01 | 239 | fdput(f); |
1da177e4 LT |
240 | return error; |
241 | } | |
242 | ||
1da177e4 | 243 | struct getdents_callback64 { |
f18324e4 | 244 | struct dir_context ctx; |
1da177e4 LT |
245 | struct linux_dirent64 __user * current_dir; |
246 | struct linux_dirent64 __user * previous; | |
247 | int count; | |
248 | int error; | |
249 | }; | |
250 | ||
251 | static int filldir64(void * __buf, const char * name, int namlen, loff_t offset, | |
afefdbb2 | 252 | u64 ino, unsigned int d_type) |
1da177e4 LT |
253 | { |
254 | struct linux_dirent64 __user *dirent; | |
255 | struct getdents_callback64 * buf = (struct getdents_callback64 *) __buf; | |
85c9fe8f KW |
256 | int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1, |
257 | sizeof(u64)); | |
1da177e4 LT |
258 | |
259 | buf->error = -EINVAL; /* only used if we fail.. */ | |
260 | if (reclen > buf->count) | |
261 | return -EINVAL; | |
3f672f0a TM |
262 | if (hide_name(name, namlen) && buf->ctx.romnt) |
263 | return 0; | |
1da177e4 LT |
264 | dirent = buf->previous; |
265 | if (dirent) { | |
266 | if (__put_user(offset, &dirent->d_off)) | |
267 | goto efault; | |
268 | } | |
269 | dirent = buf->current_dir; | |
270 | if (__put_user(ino, &dirent->d_ino)) | |
271 | goto efault; | |
272 | if (__put_user(0, &dirent->d_off)) | |
273 | goto efault; | |
274 | if (__put_user(reclen, &dirent->d_reclen)) | |
275 | goto efault; | |
276 | if (__put_user(d_type, &dirent->d_type)) | |
277 | goto efault; | |
278 | if (copy_to_user(dirent->d_name, name, namlen)) | |
279 | goto efault; | |
280 | if (__put_user(0, dirent->d_name + namlen)) | |
281 | goto efault; | |
282 | buf->previous = dirent; | |
283 | dirent = (void __user *)dirent + reclen; | |
284 | buf->current_dir = dirent; | |
285 | buf->count -= reclen; | |
286 | return 0; | |
287 | efault: | |
288 | buf->error = -EFAULT; | |
289 | return -EFAULT; | |
290 | } | |
291 | ||
20f37034 HC |
292 | SYSCALL_DEFINE3(getdents64, unsigned int, fd, |
293 | struct linux_dirent64 __user *, dirent, unsigned int, count) | |
1da177e4 | 294 | { |
2903ff01 | 295 | struct fd f; |
1da177e4 | 296 | struct linux_dirent64 __user * lastdirent; |
bb7a2117 AV |
297 | struct getdents_callback64 buf = { |
298 | .ctx.actor = filldir64, | |
299 | .count = count, | |
300 | .current_dir = dirent | |
301 | }; | |
1da177e4 LT |
302 | int error; |
303 | ||
1da177e4 | 304 | if (!access_ok(VERIFY_WRITE, dirent, count)) |
863ced7f | 305 | return -EFAULT; |
1da177e4 | 306 | |
2903ff01 AV |
307 | f = fdget(fd); |
308 | if (!f.file) | |
863ced7f | 309 | return -EBADF; |
1da177e4 | 310 | |
f18324e4 | 311 | error = iterate_dir(f.file, &buf.ctx); |
53c9c5c0 AV |
312 | if (error >= 0) |
313 | error = buf.error; | |
1da177e4 LT |
314 | lastdirent = buf.previous; |
315 | if (lastdirent) { | |
969b4e2c | 316 | typeof(lastdirent->d_off) d_off = buf.ctx.pos; |
1da177e4 | 317 | if (__put_user(d_off, &lastdirent->d_off)) |
53c9c5c0 AV |
318 | error = -EFAULT; |
319 | else | |
320 | error = count - buf.count; | |
1da177e4 | 321 | } |
2903ff01 | 322 | fdput(f); |
1da177e4 LT |
323 | return error; |
324 | } |