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 | ||
0f837692 | 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; |
0f63c0e8 | 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)) { | |
d0b3db95 | 40 | ctx->romnt = (inode->i_sb->s_flags & MS_RDONLY); |
0f63c0e8 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 | } | |
0f837692 | 55 | EXPORT_SYMBOL(iterate_dir); |
1da177e4 | 56 | |
672c40a1 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 { | |
0f837692 | 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 | { |
0f837692 | 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 | } |
672c40a1 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); |
1da177e4 LT |
128 | struct readdir_callback buf; |
129 | ||
2903ff01 | 130 | if (!f.file) |
863ced7f | 131 | return -EBADF; |
1da177e4 | 132 | |
0f837692 | 133 | buf.ctx.actor = fillonedir; |
1da177e4 LT |
134 | buf.result = 0; |
135 | buf.dirent = dirent; | |
136 | ||
0f837692 | 137 | error = iterate_dir(f.file, &buf.ctx); |
53c9c5c0 | 138 | if (buf.result) |
1da177e4 LT |
139 | error = buf.result; |
140 | ||
2903ff01 | 141 | fdput(f); |
1da177e4 LT |
142 | return error; |
143 | } | |
144 | ||
145 | #endif /* __ARCH_WANT_OLD_READDIR */ | |
146 | ||
147 | /* | |
148 | * New, all-improved, singing, dancing, iBCS2-compliant getdents() | |
149 | * interface. | |
150 | */ | |
151 | struct linux_dirent { | |
152 | unsigned long d_ino; | |
153 | unsigned long d_off; | |
154 | unsigned short d_reclen; | |
155 | char d_name[1]; | |
156 | }; | |
157 | ||
158 | struct getdents_callback { | |
0f837692 | 159 | struct dir_context ctx; |
1da177e4 LT |
160 | struct linux_dirent __user * current_dir; |
161 | struct linux_dirent __user * previous; | |
162 | int count; | |
163 | int error; | |
164 | }; | |
165 | ||
166 | static int filldir(void * __buf, const char * name, int namlen, loff_t offset, | |
afefdbb2 | 167 | u64 ino, unsigned int d_type) |
1da177e4 LT |
168 | { |
169 | struct linux_dirent __user * dirent; | |
170 | struct getdents_callback * buf = (struct getdents_callback *) __buf; | |
afefdbb2 | 171 | unsigned long d_ino; |
85c9fe8f KW |
172 | int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2, |
173 | sizeof(long)); | |
1da177e4 LT |
174 | |
175 | buf->error = -EINVAL; /* only used if we fail.. */ | |
176 | if (reclen > buf->count) | |
177 | return -EINVAL; | |
afefdbb2 | 178 | d_ino = ino; |
8f3f655d AV |
179 | if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) { |
180 | buf->error = -EOVERFLOW; | |
afefdbb2 | 181 | return -EOVERFLOW; |
8f3f655d | 182 | } |
672c40a1 TM |
183 | if (hide_name(name, namlen) && buf->ctx.romnt) |
184 | return 0; | |
1da177e4 LT |
185 | dirent = buf->previous; |
186 | if (dirent) { | |
187 | if (__put_user(offset, &dirent->d_off)) | |
188 | goto efault; | |
189 | } | |
190 | dirent = buf->current_dir; | |
afefdbb2 | 191 | if (__put_user(d_ino, &dirent->d_ino)) |
1da177e4 LT |
192 | goto efault; |
193 | if (__put_user(reclen, &dirent->d_reclen)) | |
194 | goto efault; | |
195 | if (copy_to_user(dirent->d_name, name, namlen)) | |
196 | goto efault; | |
197 | if (__put_user(0, dirent->d_name + namlen)) | |
198 | goto efault; | |
199 | if (__put_user(d_type, (char __user *) dirent + reclen - 1)) | |
200 | goto efault; | |
201 | buf->previous = dirent; | |
202 | dirent = (void __user *)dirent + reclen; | |
203 | buf->current_dir = dirent; | |
204 | buf->count -= reclen; | |
205 | return 0; | |
206 | efault: | |
207 | buf->error = -EFAULT; | |
208 | return -EFAULT; | |
209 | } | |
210 | ||
20f37034 HC |
211 | SYSCALL_DEFINE3(getdents, unsigned int, fd, |
212 | struct linux_dirent __user *, dirent, unsigned int, count) | |
1da177e4 | 213 | { |
2903ff01 | 214 | struct fd f; |
1da177e4 LT |
215 | struct linux_dirent __user * lastdirent; |
216 | struct getdents_callback buf; | |
217 | int error; | |
218 | ||
1da177e4 | 219 | if (!access_ok(VERIFY_WRITE, dirent, count)) |
863ced7f | 220 | return -EFAULT; |
1da177e4 | 221 | |
2903ff01 AV |
222 | f = fdget(fd); |
223 | if (!f.file) | |
863ced7f | 224 | return -EBADF; |
1da177e4 LT |
225 | |
226 | buf.current_dir = dirent; | |
227 | buf.previous = NULL; | |
228 | buf.count = count; | |
229 | buf.error = 0; | |
0f837692 | 230 | buf.ctx.actor = filldir; |
1da177e4 | 231 | |
0f837692 | 232 | error = iterate_dir(f.file, &buf.ctx); |
53c9c5c0 AV |
233 | if (error >= 0) |
234 | error = buf.error; | |
1da177e4 LT |
235 | lastdirent = buf.previous; |
236 | if (lastdirent) { | |
0f63c0e8 | 237 | if (put_user(buf.ctx.pos, &lastdirent->d_off)) |
1da177e4 LT |
238 | error = -EFAULT; |
239 | else | |
240 | error = count - buf.count; | |
241 | } | |
2903ff01 | 242 | fdput(f); |
1da177e4 LT |
243 | return error; |
244 | } | |
245 | ||
1da177e4 | 246 | struct getdents_callback64 { |
0f837692 | 247 | struct dir_context ctx; |
1da177e4 LT |
248 | struct linux_dirent64 __user * current_dir; |
249 | struct linux_dirent64 __user * previous; | |
250 | int count; | |
251 | int error; | |
252 | }; | |
253 | ||
254 | static int filldir64(void * __buf, const char * name, int namlen, loff_t offset, | |
afefdbb2 | 255 | u64 ino, unsigned int d_type) |
1da177e4 LT |
256 | { |
257 | struct linux_dirent64 __user *dirent; | |
258 | struct getdents_callback64 * buf = (struct getdents_callback64 *) __buf; | |
85c9fe8f KW |
259 | int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1, |
260 | sizeof(u64)); | |
1da177e4 LT |
261 | |
262 | buf->error = -EINVAL; /* only used if we fail.. */ | |
263 | if (reclen > buf->count) | |
264 | return -EINVAL; | |
672c40a1 TM |
265 | if (hide_name(name, namlen) && buf->ctx.romnt) |
266 | return 0; | |
1da177e4 LT |
267 | dirent = buf->previous; |
268 | if (dirent) { | |
269 | if (__put_user(offset, &dirent->d_off)) | |
270 | goto efault; | |
271 | } | |
272 | dirent = buf->current_dir; | |
273 | if (__put_user(ino, &dirent->d_ino)) | |
274 | goto efault; | |
275 | if (__put_user(0, &dirent->d_off)) | |
276 | goto efault; | |
277 | if (__put_user(reclen, &dirent->d_reclen)) | |
278 | goto efault; | |
279 | if (__put_user(d_type, &dirent->d_type)) | |
280 | goto efault; | |
281 | if (copy_to_user(dirent->d_name, name, namlen)) | |
282 | goto efault; | |
283 | if (__put_user(0, dirent->d_name + namlen)) | |
284 | goto efault; | |
285 | buf->previous = dirent; | |
286 | dirent = (void __user *)dirent + reclen; | |
287 | buf->current_dir = dirent; | |
288 | buf->count -= reclen; | |
289 | return 0; | |
290 | efault: | |
291 | buf->error = -EFAULT; | |
292 | return -EFAULT; | |
293 | } | |
294 | ||
20f37034 HC |
295 | SYSCALL_DEFINE3(getdents64, unsigned int, fd, |
296 | struct linux_dirent64 __user *, dirent, unsigned int, count) | |
1da177e4 | 297 | { |
2903ff01 | 298 | struct fd f; |
1da177e4 LT |
299 | struct linux_dirent64 __user * lastdirent; |
300 | struct getdents_callback64 buf; | |
301 | int error; | |
302 | ||
1da177e4 | 303 | if (!access_ok(VERIFY_WRITE, dirent, count)) |
863ced7f | 304 | return -EFAULT; |
1da177e4 | 305 | |
2903ff01 AV |
306 | f = fdget(fd); |
307 | if (!f.file) | |
863ced7f | 308 | return -EBADF; |
1da177e4 LT |
309 | |
310 | buf.current_dir = dirent; | |
311 | buf.previous = NULL; | |
312 | buf.count = count; | |
313 | buf.error = 0; | |
0f837692 | 314 | buf.ctx.actor = filldir64; |
1da177e4 | 315 | |
0f837692 | 316 | error = iterate_dir(f.file, &buf.ctx); |
53c9c5c0 AV |
317 | if (error >= 0) |
318 | error = buf.error; | |
1da177e4 LT |
319 | lastdirent = buf.previous; |
320 | if (lastdirent) { | |
0f63c0e8 | 321 | typeof(lastdirent->d_off) d_off = buf.ctx.pos; |
1da177e4 | 322 | if (__put_user(d_off, &lastdirent->d_off)) |
53c9c5c0 AV |
323 | error = -EFAULT; |
324 | else | |
325 | error = count - buf.count; | |
1da177e4 | 326 | } |
2903ff01 | 327 | fdput(f); |
1da177e4 LT |
328 | return error; |
329 | } |