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