Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | /* * linux/fs/read_write.c * * Copyright (C) 1991, 1992 Linus Torvalds */ #include <linux/types.h> #include <linux/errno.h> #include <linux/stat.h> #include <linux/kernel.h> #include <linux/sched.h> #include <linux/fcntl.h> #include <linux/mm.h> #include <linux/uio.h> #include <asm/segment.h> asmlinkage int sys_lseek(unsigned int fd, off_t offset, unsigned int origin) { struct file * file; int tmp = -1; if (fd >= NR_OPEN || !(file=current->files->fd[fd]) || !(file->f_inode)) return -EBADF; if (origin > 2) return -EINVAL; if (file->f_op && file->f_op->lseek) return file->f_op->lseek(file->f_inode,file,offset,origin); /* this is the default handler if no lseek handler is present */ switch (origin) { case 0: tmp = offset; break; case 1: tmp = file->f_pos + offset; break; case 2: if (!file->f_inode) return -EINVAL; tmp = file->f_inode->i_size + offset; break; } if (tmp < 0) return -EINVAL; if (tmp != file->f_pos) { file->f_pos = tmp; file->f_reada = 0; file->f_version = ++event; } return file->f_pos; } asmlinkage int sys_llseek(unsigned int fd, unsigned long offset_high, unsigned long offset_low, loff_t * result, unsigned int origin) { struct file * file; loff_t tmp = -1; loff_t offset; int err; if (fd >= NR_OPEN || !(file=current->files->fd[fd]) || !(file->f_inode)) return -EBADF; if (origin > 2) return -EINVAL; if ((err = verify_area(VERIFY_WRITE, result, sizeof(loff_t)))) return err; offset = (loff_t) (((unsigned long long) offset_high << 32) | offset_low); /* if there is a fs-specific handler, we can't just ignore it.. */ /* accept llseek() only for the signed long subset of long long */ if (file->f_op && file->f_op->lseek) { if (offset != (long) offset) return -EINVAL; return file->f_op->lseek(file->f_inode,file,offset,origin); } switch (origin) { case 0: tmp = offset; break; case 1: tmp = file->f_pos + offset; break; case 2: if (!file->f_inode) return -EINVAL; tmp = file->f_inode->i_size + offset; break; } if (tmp < 0) return -EINVAL; if (tmp != file->f_pos) { file->f_pos = tmp; file->f_reada = 0; file->f_version = ++event; } memcpy_tofs(result, &file->f_pos, sizeof(loff_t)); return 0; } asmlinkage int sys_read(unsigned int fd,char * buf,unsigned int count) { int error; struct file * file; struct inode * inode; if (fd>=NR_OPEN || !(file=current->files->fd[fd]) || !(inode=file->f_inode)) return -EBADF; if (!(file->f_mode & 1)) return -EBADF; if (!file->f_op || !file->f_op->read) return -EINVAL; if (!count) return 0; error = verify_area(VERIFY_WRITE,buf,count); if (error) return error; return file->f_op->read(inode,file,buf,count); } asmlinkage int sys_write(unsigned int fd,char * buf,unsigned int count) { int error; struct file * file; struct inode * inode; int written; if (fd>=NR_OPEN || !(file=current->files->fd[fd]) || !(inode=file->f_inode)) return -EBADF; if (!(file->f_mode & 2)) return -EBADF; if (!file->f_op || !file->f_op->write) return -EINVAL; if (!count) return 0; error = verify_area(VERIFY_READ,buf,count); if (error) return error; /* * If data has been written to the file, remove the setuid and * the setgid bits. We do it anyway otherwise there is an * extremely exploitable race - does your OS get it right |-> * * Set ATTR_FORCE so it will always be changed. */ if (!suser() && (inode->i_mode & (S_ISUID | S_ISGID))) { struct iattr newattrs; newattrs.ia_mode = inode->i_mode & ~(S_ISUID | S_ISGID); newattrs.ia_valid = ATTR_CTIME | ATTR_MODE | ATTR_FORCE; notify_change(inode, &newattrs); } down(&inode->i_sem); written = file->f_op->write(inode,file,buf,count); up(&inode->i_sem); return written; } static int sock_readv_writev(int type, struct inode * inode, struct file * file, const struct iovec * iov, long count, long size) { struct msghdr msg; struct socket *sock; sock = &inode->u.socket_i; if (!sock->ops) return -EOPNOTSUPP; msg.msg_name = NULL; msg.msg_namelen = 0; msg.msg_accrights = NULL; msg.msg_iov = (struct iovec *) iov; msg.msg_iovlen = count; /* read() does a VERIFY_WRITE */ if (type == VERIFY_WRITE) { if (!sock->ops->recvmsg) return -EOPNOTSUPP; return sock->ops->recvmsg(sock, &msg, size, (file->f_flags & O_NONBLOCK), 0, NULL); } if (!sock->ops->sendmsg) return -EOPNOTSUPP; return sock->ops->sendmsg(sock, &msg, size, (file->f_flags & O_NONBLOCK), 0); } typedef int (*IO_fn_t)(struct inode *, struct file *, char *, int); static int do_readv_writev(int type, struct inode * inode, struct file * file, const struct iovec * vector, unsigned long count) { size_t tot_len; struct iovec iov[MAX_IOVEC]; int retval, i; IO_fn_t fn; /* * First get the "struct iovec" from user memory and * verify all the pointers */ if (!count) return 0; if (count > MAX_IOVEC) return -EINVAL; retval = verify_area(VERIFY_READ, vector, count*sizeof(*vector)); if (retval) return retval; memcpy_fromfs(iov, vector, count*sizeof(*vector)); tot_len = 0; for (i = 0 ; i < count ; i++) { tot_len += iov[i].iov_len; retval = verify_area(type, iov[i].iov_base, iov[i].iov_len); if (retval) return retval; } /* * Then do the actual IO. Note that sockets need to be handled * specially as they have atomicity guarantees and can handle * iovec's natively */ if (inode->i_sock) return sock_readv_writev(type, inode, file, iov, count, tot_len); if (!file->f_op) return -EINVAL; /* VERIFY_WRITE actually means a read, as we write to user space */ fn = file->f_op->read; if (type == VERIFY_READ) fn = (IO_fn_t) file->f_op->write; vector = iov; while (count > 0) { void * base; int len, nr; base = vector->iov_base; len = vector->iov_len; vector++; count--; nr = fn(inode, file, base, len); if (nr < 0) { if (retval) break; retval = nr; break; } retval += nr; if (nr != len) break; } return retval; } asmlinkage int sys_readv(unsigned long fd, const struct iovec * vector, long count) { struct file * file; struct inode * inode; if (fd >= NR_OPEN || !(file = current->files->fd[fd]) || !(inode = file->f_inode)) return -EBADF; if (!(file->f_mode & 1)) return -EBADF; return do_readv_writev(VERIFY_WRITE, inode, file, vector, count); } asmlinkage int sys_writev(unsigned long fd, const struct iovec * vector, long count) { int error; struct file * file; struct inode * inode; if (fd >= NR_OPEN || !(file = current->files->fd[fd]) || !(inode = file->f_inode)) return -EBADF; if (!(file->f_mode & 2)) return -EBADF; down(&inode->i_sem); error = do_readv_writev(VERIFY_READ, inode, file, vector, count); up(&inode->i_sem); return error; } |