| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
#include <linux/module.h> |
|---|
| 8 |
#include <linux/time.h> |
|---|
| 9 |
#include <linux/mm.h> |
|---|
| 10 |
#include <linux/errno.h> |
|---|
| 11 |
#include <linux/stat.h> |
|---|
| 12 |
#include <linux/file.h> |
|---|
| 13 |
#include <linux/smp_lock.h> |
|---|
| 14 |
#include <linux/fs.h> |
|---|
| 15 |
#include <linux/dirent.h> |
|---|
| 16 |
#include <linux/security.h> |
|---|
| 17 |
#include <linux/unistd.h> |
|---|
| 18 |
|
|---|
| 19 |
#include <asm/uaccess.h> |
|---|
| 20 |
|
|---|
| 21 |
int vfs_readdir(struct file *file, filldir_t filler, void *buf) |
|---|
| 22 |
{ |
|---|
| 23 |
struct inode *inode = file->f_dentry->d_inode; |
|---|
| 24 |
int res = -ENOTDIR; |
|---|
| 25 |
if (!file->f_op || !file->f_op->readdir) |
|---|
| 26 |
goto out; |
|---|
| 27 |
|
|---|
| 28 |
res = security_file_permission(file, MAY_READ); |
|---|
| 29 |
if (res) |
|---|
| 30 |
goto out; |
|---|
| 31 |
|
|---|
| 32 |
down(&inode->i_sem); |
|---|
| 33 |
res = -ENOENT; |
|---|
| 34 |
if (!IS_DEADDIR(inode)) { |
|---|
| 35 |
res = file->f_op->readdir(file, buf, filler); |
|---|
| 36 |
file_accessed(file); |
|---|
| 37 |
} |
|---|
| 38 |
up(&inode->i_sem); |
|---|
| 39 |
out: |
|---|
| 40 |
return res; |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
EXPORT_SYMBOL(vfs_readdir); |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
#define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de))) |
|---|
| 54 |
#define ROUND_UP(x) (((x)+sizeof(long)-1) & ~(sizeof(long)-1)) |
|---|
| 55 |
|
|---|
| 56 |
#ifdef __ARCH_WANT_OLD_READDIR |
|---|
| 57 |
|
|---|
| 58 |
struct old_linux_dirent { |
|---|
| 59 |
unsigned long d_ino; |
|---|
| 60 |
unsigned long d_offset; |
|---|
| 61 |
unsigned short d_namlen; |
|---|
| 62 |
char d_name[1]; |
|---|
| 63 |
}; |
|---|
| 64 |
|
|---|
| 65 |
struct readdir_callback { |
|---|
| 66 |
struct old_linux_dirent __user * dirent; |
|---|
| 67 |
int result; |
|---|
| 68 |
}; |
|---|
| 69 |
|
|---|
| 70 |
static int fillonedir(void * __buf, const char * name, int namlen, loff_t offset, |
|---|
| 71 |
ino_t ino, unsigned int d_type) |
|---|
| 72 |
{ |
|---|
| 73 |
struct readdir_callback * buf = (struct readdir_callback *) __buf; |
|---|
| 74 |
struct old_linux_dirent __user * dirent; |
|---|
| 75 |
|
|---|
| 76 |
if (buf->result) |
|---|
| 77 |
return -EINVAL; |
|---|
| 78 |
buf->result++; |
|---|
| 79 |
dirent = buf->dirent; |
|---|
| 80 |
if (!access_ok(VERIFY_WRITE, dirent, |
|---|
| 81 |
(unsigned long)(dirent->d_name + namlen + 1) - |
|---|
| 82 |
(unsigned long)dirent)) |
|---|
| 83 |
goto efault; |
|---|
| 84 |
if ( __put_user(ino, &dirent->d_ino) || |
|---|
| 85 |
__put_user(offset, &dirent->d_offset) || |
|---|
| 86 |
__put_user(namlen, &dirent->d_namlen) || |
|---|
| 87 |
__copy_to_user(dirent->d_name, name, namlen) || |
|---|
| 88 |
__put_user(0, dirent->d_name + namlen)) |
|---|
| 89 |
goto efault; |
|---|
| 90 |
return 0; |
|---|
| 91 |
efault: |
|---|
| 92 |
buf->result = -EFAULT; |
|---|
| 93 |
return -EFAULT; |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
asmlinkage long old_readdir(unsigned int fd, struct old_linux_dirent __user * dirent, unsigned int count) |
|---|
| 97 |
{ |
|---|
| 98 |
int error; |
|---|
| 99 |
struct file * file; |
|---|
| 100 |
struct readdir_callback buf; |
|---|
| 101 |
|
|---|
| 102 |
error = -EBADF; |
|---|
| 103 |
file = fget(fd); |
|---|
| 104 |
if (!file) |
|---|
| 105 |
goto out; |
|---|
| 106 |
|
|---|
| 107 |
buf.result = 0; |
|---|
| 108 |
buf.dirent = dirent; |
|---|
| 109 |
|
|---|
| 110 |
error = vfs_readdir(file, fillonedir, &buf); |
|---|
| 111 |
if (error >= 0) |
|---|
| 112 |
error = buf.result; |
|---|
| 113 |
|
|---|
| 114 |
fput(file); |
|---|
| 115 |
out: |
|---|
| 116 |
return error; |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
#endif |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
struct linux_dirent { |
|---|
| 126 |
unsigned long d_ino; |
|---|
| 127 |
unsigned long d_off; |
|---|
| 128 |
unsigned short d_reclen; |
|---|
| 129 |
char d_name[1]; |
|---|
| 130 |
}; |
|---|
| 131 |
|
|---|
| 132 |
struct getdents_callback { |
|---|
| 133 |
struct linux_dirent __user * current_dir; |
|---|
| 134 |
struct linux_dirent __user * previous; |
|---|
| 135 |
int count; |
|---|
| 136 |
int error; |
|---|
| 137 |
}; |
|---|
| 138 |
|
|---|
| 139 |
static int filldir(void * __buf, const char * name, int namlen, loff_t offset, |
|---|
| 140 |
ino_t ino, unsigned int d_type) |
|---|
| 141 |
{ |
|---|
| 142 |
struct linux_dirent __user * dirent; |
|---|
| 143 |
struct getdents_callback * buf = (struct getdents_callback *) __buf; |
|---|
| 144 |
int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 2); |
|---|
| 145 |
|
|---|
| 146 |
buf->error = -EINVAL; |
|---|
| 147 |
if (reclen > buf->count) |
|---|
| 148 |
return -EINVAL; |
|---|
| 149 |
dirent = buf->previous; |
|---|
| 150 |
if (dirent) { |
|---|
| 151 |
if (__put_user(offset, &dirent->d_off)) |
|---|
| 152 |
goto efault; |
|---|
| 153 |
} |
|---|
| 154 |
dirent = buf->current_dir; |
|---|
| 155 |
if (__put_user(ino, &dirent->d_ino)) |
|---|
| 156 |
goto efault; |
|---|
| 157 |
if (__put_user(reclen, &dirent->d_reclen)) |
|---|
| 158 |
goto efault; |
|---|
| 159 |
if (copy_to_user(dirent->d_name, name, namlen)) |
|---|
| 160 |
goto efault; |
|---|
| 161 |
if (__put_user(0, dirent->d_name + namlen)) |
|---|
| 162 |
goto efault; |
|---|
| 163 |
if (__put_user(d_type, (char __user *) dirent + reclen - 1)) |
|---|
| 164 |
goto efault; |
|---|
| 165 |
buf->previous = dirent; |
|---|
| 166 |
dirent = (void __user *)dirent + reclen; |
|---|
| 167 |
buf->current_dir = dirent; |
|---|
| 168 |
buf->count -= reclen; |
|---|
| 169 |
return 0; |
|---|
| 170 |
efault: |
|---|
| 171 |
buf->error = -EFAULT; |
|---|
| 172 |
return -EFAULT; |
|---|
| 173 |
} |
|---|
| 174 |
|
|---|
| 175 |
asmlinkage long sys_getdents(unsigned int fd, struct linux_dirent __user * dirent, unsigned int count) |
|---|
| 176 |
{ |
|---|
| 177 |
struct file * file; |
|---|
| 178 |
struct linux_dirent __user * lastdirent; |
|---|
| 179 |
struct getdents_callback buf; |
|---|
| 180 |
int error; |
|---|
| 181 |
|
|---|
| 182 |
error = -EFAULT; |
|---|
| 183 |
if (!access_ok(VERIFY_WRITE, dirent, count)) |
|---|
| 184 |
goto out; |
|---|
| 185 |
|
|---|
| 186 |
error = -EBADF; |
|---|
| 187 |
file = fget(fd); |
|---|
| 188 |
if (!file) |
|---|
| 189 |
goto out; |
|---|
| 190 |
|
|---|
| 191 |
buf.current_dir = dirent; |
|---|
| 192 |
buf.previous = NULL; |
|---|
| 193 |
buf.count = count; |
|---|
| 194 |
buf.error = 0; |
|---|
| 195 |
|
|---|
| 196 |
error = vfs_readdir(file, filldir, &buf); |
|---|
| 197 |
if (error < 0) |
|---|
| 198 |
goto out_putf; |
|---|
| 199 |
error = buf.error; |
|---|
| 200 |
lastdirent = buf.previous; |
|---|
| 201 |
if (lastdirent) { |
|---|
| 202 |
if (put_user(file->f_pos, &lastdirent->d_off)) |
|---|
| 203 |
error = -EFAULT; |
|---|
| 204 |
else |
|---|
| 205 |
error = count - buf.count; |
|---|
| 206 |
} |
|---|
| 207 |
|
|---|
| 208 |
out_putf: |
|---|
| 209 |
fput(file); |
|---|
| 210 |
out: |
|---|
| 211 |
return error; |
|---|
| 212 |
} |
|---|
| 213 |
|
|---|
| 214 |
#define ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1)) |
|---|
| 215 |
|
|---|
| 216 |
struct getdents_callback64 { |
|---|
| 217 |
struct linux_dirent64 __user * current_dir; |
|---|
| 218 |
struct linux_dirent64 __user * previous; |
|---|
| 219 |
int count; |
|---|
| 220 |
int error; |
|---|
| 221 |
}; |
|---|
| 222 |
|
|---|
| 223 |
static int filldir64(void * __buf, const char * name, int namlen, loff_t offset, |
|---|
| 224 |
ino_t ino, unsigned int d_type) |
|---|
| 225 |
{ |
|---|
| 226 |
struct linux_dirent64 __user *dirent; |
|---|
| 227 |
struct getdents_callback64 * buf = (struct getdents_callback64 *) __buf; |
|---|
| 228 |
int reclen = ROUND_UP64(NAME_OFFSET(dirent) + namlen + 1); |
|---|
| 229 |
|
|---|
| 230 |
buf->error = -EINVAL; |
|---|
| 231 |
if (reclen > buf->count) |
|---|
| 232 |
return -EINVAL; |
|---|
| 233 |
dirent = buf->previous; |
|---|
| 234 |
if (dirent) { |
|---|
| 235 |
if (__put_user(offset, &dirent->d_off)) |
|---|
| 236 |
goto efault; |
|---|
| 237 |
} |
|---|
| 238 |
dirent = buf->current_dir; |
|---|
| 239 |
if (__put_user(ino, &dirent->d_ino)) |
|---|
| 240 |
goto efault; |
|---|
| 241 |
if (__put_user(0, &dirent->d_off)) |
|---|
| 242 |
goto efault; |
|---|
| 243 |
if (__put_user(reclen, &dirent->d_reclen)) |
|---|
| 244 |
goto efault; |
|---|
| 245 |
if (__put_user(d_type, &dirent->d_type)) |
|---|
| 246 |
goto efault; |
|---|
| 247 |
if (copy_to_user(dirent->d_name, name, namlen)) |
|---|
| 248 |
goto efault; |
|---|
| 249 |
if (__put_user(0, dirent->d_name + namlen)) |
|---|
| 250 |
goto efault; |
|---|
| 251 |
buf->previous = dirent; |
|---|
| 252 |
dirent = (void __user *)dirent + reclen; |
|---|
| 253 |
buf->current_dir = dirent; |
|---|
| 254 |
buf->count -= reclen; |
|---|
| 255 |
return 0; |
|---|
| 256 |
efault: |
|---|
| 257 |
buf->error = -EFAULT; |
|---|
| 258 |
return -EFAULT; |
|---|
| 259 |
} |
|---|
| 260 |
|
|---|
| 261 |
asmlinkage long sys_getdents64(unsigned int fd, struct linux_dirent64 __user * dirent, unsigned int count) |
|---|
| 262 |
{ |
|---|
| 263 |
struct file * file; |
|---|
| 264 |
struct linux_dirent64 __user * lastdirent; |
|---|
| 265 |
struct getdents_callback64 buf; |
|---|
| 266 |
int error; |
|---|
| 267 |
|
|---|
| 268 |
error = -EFAULT; |
|---|
| 269 |
if (!access_ok(VERIFY_WRITE, dirent, count)) |
|---|
| 270 |
goto out; |
|---|
| 271 |
|
|---|
| 272 |
error = -EBADF; |
|---|
| 273 |
file = fget(fd); |
|---|
| 274 |
if (!file) |
|---|
| 275 |
goto out; |
|---|
| 276 |
|
|---|
| 277 |
buf.current_dir = dirent; |
|---|
| 278 |
buf.previous = NULL; |
|---|
| 279 |
buf.count = count; |
|---|
| 280 |
buf.error = 0; |
|---|
| 281 |
|
|---|
| 282 |
error = vfs_readdir(file, filldir64, &buf); |
|---|
| 283 |
if (error < 0) |
|---|
| 284 |
goto out_putf; |
|---|
| 285 |
error = buf.error; |
|---|
| 286 |
lastdirent = buf.previous; |
|---|
| 287 |
if (lastdirent) { |
|---|
| 288 |
typeof(lastdirent->d_off) d_off = file->f_pos; |
|---|
| 289 |
__put_user(d_off, &lastdirent->d_off); |
|---|
| 290 |
error = count - buf.count; |
|---|
| 291 |
} |
|---|
| 292 |
|
|---|
| 293 |
out_putf: |
|---|
| 294 |
fput(file); |
|---|
| 295 |
out: |
|---|
| 296 |
return error; |
|---|
| 297 |
} |
|---|