Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/readdir.c |
| 3 | * |
| 4 | * Copyright (C) 1995 Linus Torvalds |
| 5 | */ |
| 6 | |
Kevin Winchester | 85c9fe8 | 2010-08-09 17:20:22 -0700 | [diff] [blame] | 7 | #include <linux/stddef.h> |
Milind Arun Choudhary | 022a169 | 2007-05-08 00:29:02 -0700 | [diff] [blame] | 8 | #include <linux/kernel.h> |
Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 9 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/fs.h> |
Heinrich Schuchardt | d4c7cf6 | 2014-06-04 16:05:41 -0700 | [diff] [blame] | 16 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/dirent.h> |
| 18 | #include <linux/security.h> |
| 19 | #include <linux/syscalls.h> |
| 20 | #include <linux/unistd.h> |
| 21 | |
| 22 | #include <asm/uaccess.h> |
| 23 | |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 24 | int iterate_dir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 26 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | int res = -ENOTDIR; |
Al Viro | 72c2d53 | 2013-09-22 16:27:52 -0400 | [diff] [blame] | 28 | if (!file->f_op->iterate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | goto out; |
| 30 | |
| 31 | res = security_file_permission(file, MAY_READ); |
| 32 | if (res) |
| 33 | goto out; |
| 34 | |
Liam R. Howlett | da78451 | 2007-12-06 17:39:54 -0500 | [diff] [blame] | 35 | res = mutex_lock_killable(&inode->i_mutex); |
| 36 | if (res) |
| 37 | goto out; |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | res = -ENOENT; |
| 40 | if (!IS_DEADDIR(inode)) { |
Al Viro | 2233f31 | 2013-05-22 21:44:23 -0400 | [diff] [blame] | 41 | ctx->pos = file->f_pos; |
| 42 | res = file->f_op->iterate(file, ctx); |
| 43 | file->f_pos = ctx->pos; |
Heinrich Schuchardt | d4c7cf6 | 2014-06-04 16:05:41 -0700 | [diff] [blame] | 44 | fsnotify_access(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | file_accessed(file); |
| 46 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 47 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | out: |
| 49 | return res; |
| 50 | } |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 51 | EXPORT_SYMBOL(iterate_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | /* |
| 54 | * Traditional linux readdir() handling.. |
| 55 | * |
| 56 | * "count=1" is a special case, meaning that the buffer is one |
| 57 | * dirent-structure in size and that the code can't handle more |
| 58 | * anyway. Thus the special "fillonedir()" function for that |
| 59 | * case (the low-level handlers don't need to care about this). |
| 60 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | #ifdef __ARCH_WANT_OLD_READDIR |
| 63 | |
| 64 | struct old_linux_dirent { |
| 65 | unsigned long d_ino; |
| 66 | unsigned long d_offset; |
| 67 | unsigned short d_namlen; |
| 68 | char d_name[1]; |
| 69 | }; |
| 70 | |
| 71 | struct readdir_callback { |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 72 | struct dir_context ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | struct old_linux_dirent __user * dirent; |
| 74 | int result; |
| 75 | }; |
| 76 | |
| 77 | static int fillonedir(void * __buf, const char * name, int namlen, loff_t offset, |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 78 | u64 ino, unsigned int d_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 80 | struct readdir_callback *buf = (struct readdir_callback *) __buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | struct old_linux_dirent __user * dirent; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 82 | unsigned long d_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | if (buf->result) |
| 85 | return -EINVAL; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 86 | d_ino = ino; |
Al Viro | 8f3f655 | 2008-08-12 00:28:24 -0400 | [diff] [blame] | 87 | if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) { |
| 88 | buf->result = -EOVERFLOW; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 89 | return -EOVERFLOW; |
Al Viro | 8f3f655 | 2008-08-12 00:28:24 -0400 | [diff] [blame] | 90 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | buf->result++; |
| 92 | dirent = buf->dirent; |
| 93 | if (!access_ok(VERIFY_WRITE, dirent, |
| 94 | (unsigned long)(dirent->d_name + namlen + 1) - |
| 95 | (unsigned long)dirent)) |
| 96 | goto efault; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 97 | if ( __put_user(d_ino, &dirent->d_ino) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | __put_user(offset, &dirent->d_offset) || |
| 99 | __put_user(namlen, &dirent->d_namlen) || |
| 100 | __copy_to_user(dirent->d_name, name, namlen) || |
| 101 | __put_user(0, dirent->d_name + namlen)) |
| 102 | goto efault; |
| 103 | return 0; |
| 104 | efault: |
| 105 | buf->result = -EFAULT; |
| 106 | return -EFAULT; |
| 107 | } |
| 108 | |
Heiko Carstens | d4e8204 | 2009-01-14 14:14:34 +0100 | [diff] [blame] | 109 | SYSCALL_DEFINE3(old_readdir, unsigned int, fd, |
| 110 | struct old_linux_dirent __user *, dirent, unsigned int, count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { |
| 112 | int error; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 113 | struct fd f = fdget(fd); |
Al Viro | ac6614b | 2013-05-22 22:22:04 -0400 | [diff] [blame] | 114 | struct readdir_callback buf = { |
| 115 | .ctx.actor = fillonedir, |
| 116 | .dirent = dirent |
| 117 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 119 | if (!f.file) |
Al Viro | 863ced7 | 2012-04-21 18:40:32 -0400 | [diff] [blame] | 120 | return -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 122 | error = iterate_dir(f.file, &buf.ctx); |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 123 | if (buf.result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | error = buf.result; |
| 125 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 126 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | return error; |
| 128 | } |
| 129 | |
| 130 | #endif /* __ARCH_WANT_OLD_READDIR */ |
| 131 | |
| 132 | /* |
| 133 | * New, all-improved, singing, dancing, iBCS2-compliant getdents() |
| 134 | * interface. |
| 135 | */ |
| 136 | struct linux_dirent { |
| 137 | unsigned long d_ino; |
| 138 | unsigned long d_off; |
| 139 | unsigned short d_reclen; |
| 140 | char d_name[1]; |
| 141 | }; |
| 142 | |
| 143 | struct getdents_callback { |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 144 | struct dir_context ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | struct linux_dirent __user * current_dir; |
| 146 | struct linux_dirent __user * previous; |
| 147 | int count; |
| 148 | int error; |
| 149 | }; |
| 150 | |
| 151 | static int filldir(void * __buf, const char * name, int namlen, loff_t offset, |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 152 | u64 ino, unsigned int d_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
| 154 | struct linux_dirent __user * dirent; |
| 155 | struct getdents_callback * buf = (struct getdents_callback *) __buf; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 156 | unsigned long d_ino; |
Kevin Winchester | 85c9fe8 | 2010-08-09 17:20:22 -0700 | [diff] [blame] | 157 | int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2, |
| 158 | sizeof(long)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
| 160 | buf->error = -EINVAL; /* only used if we fail.. */ |
| 161 | if (reclen > buf->count) |
| 162 | return -EINVAL; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 163 | d_ino = ino; |
Al Viro | 8f3f655 | 2008-08-12 00:28:24 -0400 | [diff] [blame] | 164 | if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) { |
| 165 | buf->error = -EOVERFLOW; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 166 | return -EOVERFLOW; |
Al Viro | 8f3f655 | 2008-08-12 00:28:24 -0400 | [diff] [blame] | 167 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | dirent = buf->previous; |
| 169 | if (dirent) { |
| 170 | if (__put_user(offset, &dirent->d_off)) |
| 171 | goto efault; |
| 172 | } |
| 173 | dirent = buf->current_dir; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 174 | if (__put_user(d_ino, &dirent->d_ino)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | goto efault; |
| 176 | if (__put_user(reclen, &dirent->d_reclen)) |
| 177 | goto efault; |
| 178 | if (copy_to_user(dirent->d_name, name, namlen)) |
| 179 | goto efault; |
| 180 | if (__put_user(0, dirent->d_name + namlen)) |
| 181 | goto efault; |
| 182 | if (__put_user(d_type, (char __user *) dirent + reclen - 1)) |
| 183 | goto efault; |
| 184 | buf->previous = dirent; |
| 185 | dirent = (void __user *)dirent + reclen; |
| 186 | buf->current_dir = dirent; |
| 187 | buf->count -= reclen; |
| 188 | return 0; |
| 189 | efault: |
| 190 | buf->error = -EFAULT; |
| 191 | return -EFAULT; |
| 192 | } |
| 193 | |
Heiko Carstens | 20f3703 | 2009-01-14 14:14:23 +0100 | [diff] [blame] | 194 | SYSCALL_DEFINE3(getdents, unsigned int, fd, |
| 195 | struct linux_dirent __user *, dirent, unsigned int, count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 197 | struct fd f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | struct linux_dirent __user * lastdirent; |
Al Viro | ac6614b | 2013-05-22 22:22:04 -0400 | [diff] [blame] | 199 | struct getdents_callback buf = { |
| 200 | .ctx.actor = filldir, |
| 201 | .count = count, |
| 202 | .current_dir = dirent |
| 203 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | int error; |
| 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | if (!access_ok(VERIFY_WRITE, dirent, count)) |
Al Viro | 863ced7 | 2012-04-21 18:40:32 -0400 | [diff] [blame] | 207 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 209 | f = fdget(fd); |
| 210 | if (!f.file) |
Al Viro | 863ced7 | 2012-04-21 18:40:32 -0400 | [diff] [blame] | 211 | return -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 213 | error = iterate_dir(f.file, &buf.ctx); |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 214 | if (error >= 0) |
| 215 | error = buf.error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | lastdirent = buf.previous; |
| 217 | if (lastdirent) { |
Al Viro | bb6f619 | 2013-05-15 18:49:12 -0400 | [diff] [blame] | 218 | if (put_user(buf.ctx.pos, &lastdirent->d_off)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | error = -EFAULT; |
| 220 | else |
| 221 | error = count - buf.count; |
| 222 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 223 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | return error; |
| 225 | } |
| 226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | struct getdents_callback64 { |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 228 | struct dir_context ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | struct linux_dirent64 __user * current_dir; |
| 230 | struct linux_dirent64 __user * previous; |
| 231 | int count; |
| 232 | int error; |
| 233 | }; |
| 234 | |
| 235 | static int filldir64(void * __buf, const char * name, int namlen, loff_t offset, |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 236 | u64 ino, unsigned int d_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | { |
| 238 | struct linux_dirent64 __user *dirent; |
| 239 | struct getdents_callback64 * buf = (struct getdents_callback64 *) __buf; |
Kevin Winchester | 85c9fe8 | 2010-08-09 17:20:22 -0700 | [diff] [blame] | 240 | int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1, |
| 241 | sizeof(u64)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
| 243 | buf->error = -EINVAL; /* only used if we fail.. */ |
| 244 | if (reclen > buf->count) |
| 245 | return -EINVAL; |
| 246 | dirent = buf->previous; |
| 247 | if (dirent) { |
| 248 | if (__put_user(offset, &dirent->d_off)) |
| 249 | goto efault; |
| 250 | } |
| 251 | dirent = buf->current_dir; |
| 252 | if (__put_user(ino, &dirent->d_ino)) |
| 253 | goto efault; |
| 254 | if (__put_user(0, &dirent->d_off)) |
| 255 | goto efault; |
| 256 | if (__put_user(reclen, &dirent->d_reclen)) |
| 257 | goto efault; |
| 258 | if (__put_user(d_type, &dirent->d_type)) |
| 259 | goto efault; |
| 260 | if (copy_to_user(dirent->d_name, name, namlen)) |
| 261 | goto efault; |
| 262 | if (__put_user(0, dirent->d_name + namlen)) |
| 263 | goto efault; |
| 264 | buf->previous = dirent; |
| 265 | dirent = (void __user *)dirent + reclen; |
| 266 | buf->current_dir = dirent; |
| 267 | buf->count -= reclen; |
| 268 | return 0; |
| 269 | efault: |
| 270 | buf->error = -EFAULT; |
| 271 | return -EFAULT; |
| 272 | } |
| 273 | |
Heiko Carstens | 20f3703 | 2009-01-14 14:14:23 +0100 | [diff] [blame] | 274 | SYSCALL_DEFINE3(getdents64, unsigned int, fd, |
| 275 | struct linux_dirent64 __user *, dirent, unsigned int, count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 277 | struct fd f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | struct linux_dirent64 __user * lastdirent; |
Al Viro | ac6614b | 2013-05-22 22:22:04 -0400 | [diff] [blame] | 279 | struct getdents_callback64 buf = { |
| 280 | .ctx.actor = filldir64, |
| 281 | .count = count, |
| 282 | .current_dir = dirent |
| 283 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | int error; |
| 285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | if (!access_ok(VERIFY_WRITE, dirent, count)) |
Al Viro | 863ced7 | 2012-04-21 18:40:32 -0400 | [diff] [blame] | 287 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 289 | f = fdget(fd); |
| 290 | if (!f.file) |
Al Viro | 863ced7 | 2012-04-21 18:40:32 -0400 | [diff] [blame] | 291 | return -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 293 | error = iterate_dir(f.file, &buf.ctx); |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 294 | if (error >= 0) |
| 295 | error = buf.error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | lastdirent = buf.previous; |
| 297 | if (lastdirent) { |
Al Viro | bb6f619 | 2013-05-15 18:49:12 -0400 | [diff] [blame] | 298 | typeof(lastdirent->d_off) d_off = buf.ctx.pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | if (__put_user(d_off, &lastdirent->d_off)) |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 300 | error = -EFAULT; |
| 301 | else |
| 302 | error = count - buf.count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 304 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | return error; |
| 306 | } |