diff options
Diffstat (limited to 'net/9p/protocol.c')
-rw-r--r-- | net/9p/protocol.c | 49 |
1 files changed, 16 insertions, 33 deletions
diff --git a/net/9p/protocol.c b/net/9p/protocol.c index 45c15f491401..1e308f210928 100644 --- a/net/9p/protocol.c +++ b/net/9p/protocol.c | |||
@@ -27,31 +27,16 @@ | |||
27 | 27 | ||
28 | #include <linux/module.h> | 28 | #include <linux/module.h> |
29 | #include <linux/errno.h> | 29 | #include <linux/errno.h> |
30 | #include <linux/kernel.h> | ||
30 | #include <linux/uaccess.h> | 31 | #include <linux/uaccess.h> |
31 | #include <linux/slab.h> | 32 | #include <linux/slab.h> |
32 | #include <linux/sched.h> | 33 | #include <linux/sched.h> |
34 | #include <linux/stddef.h> | ||
33 | #include <linux/types.h> | 35 | #include <linux/types.h> |
34 | #include <net/9p/9p.h> | 36 | #include <net/9p/9p.h> |
35 | #include <net/9p/client.h> | 37 | #include <net/9p/client.h> |
36 | #include "protocol.h" | 38 | #include "protocol.h" |
37 | 39 | ||
38 | #ifndef MIN | ||
39 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | ||
40 | #endif | ||
41 | |||
42 | #ifndef MAX | ||
43 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) | ||
44 | #endif | ||
45 | |||
46 | #ifndef offset_of | ||
47 | #define offset_of(type, memb) \ | ||
48 | ((unsigned long)(&((type *)0)->memb)) | ||
49 | #endif | ||
50 | #ifndef container_of | ||
51 | #define container_of(obj, type, memb) \ | ||
52 | ((type *)(((char *)obj) - offset_of(type, memb))) | ||
53 | #endif | ||
54 | |||
55 | static int | 40 | static int |
56 | p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...); | 41 | p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...); |
57 | 42 | ||
@@ -104,7 +89,7 @@ EXPORT_SYMBOL(p9stat_free); | |||
104 | 89 | ||
105 | static size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size) | 90 | static size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size) |
106 | { | 91 | { |
107 | size_t len = MIN(pdu->size - pdu->offset, size); | 92 | size_t len = min(pdu->size - pdu->offset, size); |
108 | memcpy(data, &pdu->sdata[pdu->offset], len); | 93 | memcpy(data, &pdu->sdata[pdu->offset], len); |
109 | pdu->offset += len; | 94 | pdu->offset += len; |
110 | return size - len; | 95 | return size - len; |
@@ -112,7 +97,7 @@ static size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size) | |||
112 | 97 | ||
113 | static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size) | 98 | static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size) |
114 | { | 99 | { |
115 | size_t len = MIN(pdu->capacity - pdu->size, size); | 100 | size_t len = min(pdu->capacity - pdu->size, size); |
116 | memcpy(&pdu->sdata[pdu->size], data, len); | 101 | memcpy(&pdu->sdata[pdu->size], data, len); |
117 | pdu->size += len; | 102 | pdu->size += len; |
118 | return size - len; | 103 | return size - len; |
@@ -121,7 +106,7 @@ static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size) | |||
121 | static size_t | 106 | static size_t |
122 | pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size) | 107 | pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size) |
123 | { | 108 | { |
124 | size_t len = MIN(pdu->capacity - pdu->size, size); | 109 | size_t len = min(pdu->capacity - pdu->size, size); |
125 | if (copy_from_user(&pdu->sdata[pdu->size], udata, len)) | 110 | if (copy_from_user(&pdu->sdata[pdu->size], udata, len)) |
126 | len = 0; | 111 | len = 0; |
127 | 112 | ||
@@ -193,27 +178,24 @@ p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt, | |||
193 | break; | 178 | break; |
194 | case 's':{ | 179 | case 's':{ |
195 | char **sptr = va_arg(ap, char **); | 180 | char **sptr = va_arg(ap, char **); |
196 | int16_t len; | 181 | uint16_t len; |
197 | int size; | ||
198 | 182 | ||
199 | errcode = p9pdu_readf(pdu, proto_version, | 183 | errcode = p9pdu_readf(pdu, proto_version, |
200 | "w", &len); | 184 | "w", &len); |
201 | if (errcode) | 185 | if (errcode) |
202 | break; | 186 | break; |
203 | 187 | ||
204 | size = MAX(len, 0); | 188 | *sptr = kmalloc(len + 1, GFP_KERNEL); |
205 | |||
206 | *sptr = kmalloc(size + 1, GFP_KERNEL); | ||
207 | if (*sptr == NULL) { | 189 | if (*sptr == NULL) { |
208 | errcode = -EFAULT; | 190 | errcode = -EFAULT; |
209 | break; | 191 | break; |
210 | } | 192 | } |
211 | if (pdu_read(pdu, *sptr, size)) { | 193 | if (pdu_read(pdu, *sptr, len)) { |
212 | errcode = -EFAULT; | 194 | errcode = -EFAULT; |
213 | kfree(*sptr); | 195 | kfree(*sptr); |
214 | *sptr = NULL; | 196 | *sptr = NULL; |
215 | } else | 197 | } else |
216 | (*sptr)[size] = 0; | 198 | (*sptr)[len] = 0; |
217 | } | 199 | } |
218 | break; | 200 | break; |
219 | case 'Q':{ | 201 | case 'Q':{ |
@@ -249,15 +231,15 @@ p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt, | |||
249 | } | 231 | } |
250 | break; | 232 | break; |
251 | case 'D':{ | 233 | case 'D':{ |
252 | int32_t *count = va_arg(ap, int32_t *); | 234 | uint32_t *count = va_arg(ap, uint32_t *); |
253 | void **data = va_arg(ap, void **); | 235 | void **data = va_arg(ap, void **); |
254 | 236 | ||
255 | errcode = | 237 | errcode = |
256 | p9pdu_readf(pdu, proto_version, "d", count); | 238 | p9pdu_readf(pdu, proto_version, "d", count); |
257 | if (!errcode) { | 239 | if (!errcode) { |
258 | *count = | 240 | *count = |
259 | MIN(*count, | 241 | min_t(uint32_t, *count, |
260 | pdu->size - pdu->offset); | 242 | pdu->size - pdu->offset); |
261 | *data = &pdu->sdata[pdu->offset]; | 243 | *data = &pdu->sdata[pdu->offset]; |
262 | } | 244 | } |
263 | } | 245 | } |
@@ -419,9 +401,10 @@ p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt, | |||
419 | break; | 401 | break; |
420 | case 's':{ | 402 | case 's':{ |
421 | const char *sptr = va_arg(ap, const char *); | 403 | const char *sptr = va_arg(ap, const char *); |
422 | int16_t len = 0; | 404 | uint16_t len = 0; |
423 | if (sptr) | 405 | if (sptr) |
424 | len = MIN(strlen(sptr), USHRT_MAX); | 406 | len = min_t(uint16_t, strlen(sptr), |
407 | USHRT_MAX); | ||
425 | 408 | ||
426 | errcode = p9pdu_writef(pdu, proto_version, | 409 | errcode = p9pdu_writef(pdu, proto_version, |
427 | "w", len); | 410 | "w", len); |
@@ -453,7 +436,7 @@ p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt, | |||
453 | stbuf->n_gid, stbuf->n_muid); | 436 | stbuf->n_gid, stbuf->n_muid); |
454 | } break; | 437 | } break; |
455 | case 'D':{ | 438 | case 'D':{ |
456 | int32_t count = va_arg(ap, int32_t); | 439 | uint32_t count = va_arg(ap, uint32_t); |
457 | const void *data = va_arg(ap, const void *); | 440 | const void *data = va_arg(ap, const void *); |
458 | 441 | ||
459 | errcode = p9pdu_writef(pdu, proto_version, "d", | 442 | errcode = p9pdu_writef(pdu, proto_version, "d", |