]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/compat.c
3e94559c452e5a0a99a64b135940696fad2eae17
[karo-tx-linux.git] / fs / compat.c
1 /*
2  *  linux/fs/compat.c
3  *
4  *  Kernel compatibililty routines for e.g. 32 bit syscall support
5  *  on 64 bit kernels.
6  *
7  *  Copyright (C) 2002       Stephen Rothwell, IBM Corporation
8  *  Copyright (C) 1997-2000  Jakub Jelinek  (jakub@redhat.com)
9  *  Copyright (C) 1998       Eddie C. Dost  (ecd@skynet.be)
10  *  Copyright (C) 2001,2002  Andi Kleen, SuSE Labs 
11  *  Copyright (C) 2003       Pavel Machek (pavel@ucw.cz)
12  *
13  *  This program is free software; you can redistribute it and/or modify
14  *  it under the terms of the GNU General Public License version 2 as
15  *  published by the Free Software Foundation.
16  */
17
18 #include <linux/stddef.h>
19 #include <linux/kernel.h>
20 #include <linux/linkage.h>
21 #include <linux/compat.h>
22 #include <linux/errno.h>
23 #include <linux/time.h>
24 #include <linux/cred.h>
25 #include <linux/fs.h>
26 #include <linux/fcntl.h>
27 #include <linux/namei.h>
28 #include <linux/file.h>
29 #include <linux/fdtable.h>
30 #include <linux/vfs.h>
31 #include <linux/ioctl.h>
32 #include <linux/init.h>
33 #include <linux/ncp_mount.h>
34 #include <linux/nfs4_mount.h>
35 #include <linux/syscalls.h>
36 #include <linux/ctype.h>
37 #include <linux/dirent.h>
38 #include <linux/fsnotify.h>
39 #include <linux/highuid.h>
40 #include <linux/personality.h>
41 #include <linux/rwsem.h>
42 #include <linux/tsacct_kern.h>
43 #include <linux/security.h>
44 #include <linux/highmem.h>
45 #include <linux/signal.h>
46 #include <linux/mm.h>
47 #include <linux/fs_struct.h>
48 #include <linux/slab.h>
49 #include <linux/pagemap.h>
50 #include <linux/aio.h>
51
52 #include <linux/uaccess.h>
53 #include <asm/mmu_context.h>
54 #include <asm/ioctls.h>
55 #include "internal.h"
56
57 /* A write operation does a read from user space and vice versa */
58 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
59
60 ssize_t compat_rw_copy_check_uvector(int type,
61                 const struct compat_iovec __user *uvector, unsigned long nr_segs,
62                 unsigned long fast_segs, struct iovec *fast_pointer,
63                 struct iovec **ret_pointer)
64 {
65         compat_ssize_t tot_len;
66         struct iovec *iov = *ret_pointer = fast_pointer;
67         ssize_t ret = 0;
68         int seg;
69
70         /*
71          * SuS says "The readv() function *may* fail if the iovcnt argument
72          * was less than or equal to 0, or greater than {IOV_MAX}.  Linux has
73          * traditionally returned zero for zero segments, so...
74          */
75         if (nr_segs == 0)
76                 goto out;
77
78         ret = -EINVAL;
79         if (nr_segs > UIO_MAXIOV)
80                 goto out;
81         if (nr_segs > fast_segs) {
82                 ret = -ENOMEM;
83                 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
84                 if (iov == NULL)
85                         goto out;
86         }
87         *ret_pointer = iov;
88
89         ret = -EFAULT;
90         if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
91                 goto out;
92
93         /*
94          * Single unix specification:
95          * We should -EINVAL if an element length is not >= 0 and fitting an
96          * ssize_t.
97          *
98          * In Linux, the total length is limited to MAX_RW_COUNT, there is
99          * no overflow possibility.
100          */
101         tot_len = 0;
102         ret = -EINVAL;
103         for (seg = 0; seg < nr_segs; seg++) {
104                 compat_uptr_t buf;
105                 compat_ssize_t len;
106
107                 if (__get_user(len, &uvector->iov_len) ||
108                    __get_user(buf, &uvector->iov_base)) {
109                         ret = -EFAULT;
110                         goto out;
111                 }
112                 if (len < 0)    /* size_t not fitting in compat_ssize_t .. */
113                         goto out;
114                 if (type >= 0 &&
115                     !access_ok(vrfy_dir(type), compat_ptr(buf), len)) {
116                         ret = -EFAULT;
117                         goto out;
118                 }
119                 if (len > MAX_RW_COUNT - tot_len)
120                         len = MAX_RW_COUNT - tot_len;
121                 tot_len += len;
122                 iov->iov_base = compat_ptr(buf);
123                 iov->iov_len = (compat_size_t) len;
124                 uvector++;
125                 iov++;
126         }
127         ret = tot_len;
128
129 out:
130         return ret;
131 }
132
133 struct compat_ncp_mount_data {
134         compat_int_t version;
135         compat_uint_t ncp_fd;
136         __compat_uid_t mounted_uid;
137         compat_pid_t wdog_pid;
138         unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
139         compat_uint_t time_out;
140         compat_uint_t retry_count;
141         compat_uint_t flags;
142         __compat_uid_t uid;
143         __compat_gid_t gid;
144         compat_mode_t file_mode;
145         compat_mode_t dir_mode;
146 };
147
148 struct compat_ncp_mount_data_v4 {
149         compat_int_t version;
150         compat_ulong_t flags;
151         compat_ulong_t mounted_uid;
152         compat_long_t wdog_pid;
153         compat_uint_t ncp_fd;
154         compat_uint_t time_out;
155         compat_uint_t retry_count;
156         compat_ulong_t uid;
157         compat_ulong_t gid;
158         compat_ulong_t file_mode;
159         compat_ulong_t dir_mode;
160 };
161
162 static void *do_ncp_super_data_conv(void *raw_data)
163 {
164         int version = *(unsigned int *)raw_data;
165
166         if (version == 3) {
167                 struct compat_ncp_mount_data *c_n = raw_data;
168                 struct ncp_mount_data *n = raw_data;
169
170                 n->dir_mode = c_n->dir_mode;
171                 n->file_mode = c_n->file_mode;
172                 n->gid = c_n->gid;
173                 n->uid = c_n->uid;
174                 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
175                 n->wdog_pid = c_n->wdog_pid;
176                 n->mounted_uid = c_n->mounted_uid;
177         } else if (version == 4) {
178                 struct compat_ncp_mount_data_v4 *c_n = raw_data;
179                 struct ncp_mount_data_v4 *n = raw_data;
180
181                 n->dir_mode = c_n->dir_mode;
182                 n->file_mode = c_n->file_mode;
183                 n->gid = c_n->gid;
184                 n->uid = c_n->uid;
185                 n->retry_count = c_n->retry_count;
186                 n->time_out = c_n->time_out;
187                 n->ncp_fd = c_n->ncp_fd;
188                 n->wdog_pid = c_n->wdog_pid;
189                 n->mounted_uid = c_n->mounted_uid;
190                 n->flags = c_n->flags;
191         } else if (version != 5) {
192                 return NULL;
193         }
194
195         return raw_data;
196 }
197
198
199 struct compat_nfs_string {
200         compat_uint_t len;
201         compat_uptr_t data;
202 };
203
204 static inline void compat_nfs_string(struct nfs_string *dst,
205                                      struct compat_nfs_string *src)
206 {
207         dst->data = compat_ptr(src->data);
208         dst->len = src->len;
209 }
210
211 struct compat_nfs4_mount_data_v1 {
212         compat_int_t version;
213         compat_int_t flags;
214         compat_int_t rsize;
215         compat_int_t wsize;
216         compat_int_t timeo;
217         compat_int_t retrans;
218         compat_int_t acregmin;
219         compat_int_t acregmax;
220         compat_int_t acdirmin;
221         compat_int_t acdirmax;
222         struct compat_nfs_string client_addr;
223         struct compat_nfs_string mnt_path;
224         struct compat_nfs_string hostname;
225         compat_uint_t host_addrlen;
226         compat_uptr_t host_addr;
227         compat_int_t proto;
228         compat_int_t auth_flavourlen;
229         compat_uptr_t auth_flavours;
230 };
231
232 static int do_nfs4_super_data_conv(void *raw_data)
233 {
234         int version = *(compat_uint_t *) raw_data;
235
236         if (version == 1) {
237                 struct compat_nfs4_mount_data_v1 *raw = raw_data;
238                 struct nfs4_mount_data *real = raw_data;
239
240                 /* copy the fields backwards */
241                 real->auth_flavours = compat_ptr(raw->auth_flavours);
242                 real->auth_flavourlen = raw->auth_flavourlen;
243                 real->proto = raw->proto;
244                 real->host_addr = compat_ptr(raw->host_addr);
245                 real->host_addrlen = raw->host_addrlen;
246                 compat_nfs_string(&real->hostname, &raw->hostname);
247                 compat_nfs_string(&real->mnt_path, &raw->mnt_path);
248                 compat_nfs_string(&real->client_addr, &raw->client_addr);
249                 real->acdirmax = raw->acdirmax;
250                 real->acdirmin = raw->acdirmin;
251                 real->acregmax = raw->acregmax;
252                 real->acregmin = raw->acregmin;
253                 real->retrans = raw->retrans;
254                 real->timeo = raw->timeo;
255                 real->wsize = raw->wsize;
256                 real->rsize = raw->rsize;
257                 real->flags = raw->flags;
258                 real->version = raw->version;
259         }
260
261         return 0;
262 }
263
264 #define NCPFS_NAME      "ncpfs"
265 #define NFS4_NAME       "nfs4"
266
267 COMPAT_SYSCALL_DEFINE5(mount, const char __user *, dev_name,
268                        const char __user *, dir_name,
269                        const char __user *, type, compat_ulong_t, flags,
270                        const void __user *, data)
271 {
272         char *kernel_type;
273         void *options;
274         char *kernel_dev;
275         int retval;
276
277         kernel_type = copy_mount_string(type);
278         retval = PTR_ERR(kernel_type);
279         if (IS_ERR(kernel_type))
280                 goto out;
281
282         kernel_dev = copy_mount_string(dev_name);
283         retval = PTR_ERR(kernel_dev);
284         if (IS_ERR(kernel_dev))
285                 goto out1;
286
287         options = copy_mount_options(data);
288         retval = PTR_ERR(options);
289         if (IS_ERR(options))
290                 goto out2;
291
292         if (kernel_type && options) {
293                 if (!strcmp(kernel_type, NCPFS_NAME)) {
294                         do_ncp_super_data_conv(options);
295                 } else if (!strcmp(kernel_type, NFS4_NAME)) {
296                         retval = -EINVAL;
297                         if (do_nfs4_super_data_conv(options))
298                                 goto out3;
299                 }
300         }
301
302         retval = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
303
304  out3:
305         kfree(options);
306  out2:
307         kfree(kernel_dev);
308  out1:
309         kfree(kernel_type);
310  out:
311         return retval;
312 }