Pull smb client fixes from Steve French: "Address OOBs and NULL dereference found by Dr. Morris's recent analysis and fuzzing. All marked for stable as well" * tag '6.7-rc5-smb3-client-fixes' of git://git.samba.org/sfrench/cifs-2.6: smb: client: fix OOB in smb2_query_reparse_point() smb: client: fix NULL deref in asn1_ber_decoder() smb: client: fix potential OOBs in smb2_parse_contexts() smb: client: fix OOB in receive_encrypted_standard()
Unified · Split
| @@ -291,16 +291,23 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon, · 290 unmodified lines | ||
| 291 | 291 | oparms.fid->mid = le64_to_cpu(o_rsp->hdr.MessageId); |
| 292 | 292 | #endif /* CIFS_DEBUG2 */ |
| 293 | 293 | |
| 294 | − rc = -EINVAL; | |
| 294 | + | |
| 295 | 295 | if (o_rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE) { |
| 296 | + spin_unlock(&cfids->cfid_list_lock); | |
| 297 | + rc = -EINVAL; | |
| 298 | + goto oshr_free; | |
| 299 | + } | |
| 300 | + | |
| 301 | + rc = smb2_parse_contexts(server, rsp_iov, | |
| 302 | + &oparms.fid->epoch, | |
| 303 | + oparms.fid->lease_key, | |
| 304 | + &oplock, NULL, NULL); | |
| 305 | + if (rc) { | |
| 296 | 306 | spin_unlock(&cfids->cfid_list_lock); |
| 297 | 307 | goto oshr_free; |
| 298 | 308 | } |
| 299 | 309 | |
| 300 | − smb2_parse_contexts(server, o_rsp, | |
| 301 | − &oparms.fid->epoch, | |
| 302 | − oparms.fid->lease_key, &oplock, | |
| 303 | − NULL, NULL); | |
| 310 | + rc = -EINVAL; | |
| 304 | 311 | if (!(oplock & SMB2_LEASE_READ_CACHING_HE)) { |
| 305 | 312 | spin_unlock(&cfids->cfid_list_lock); |
| 306 | 313 | goto oshr_free; |
| @@ -313,6 +313,9 @@ static const bool has_smb2_data_area[NUMBER_OF_SMB2_COMMANDS] = { · 312 unmodified lines | ||
| 313 | 313 | char * |
| 314 | 314 | smb2_get_data_area_len(int *off, int *len, struct smb2_hdr *shdr) |
| 315 | 315 | { |
| 316 | + const int max_off = 4096; | |
| 317 | + const int max_len = 128 * 1024; | |
| 318 | + | |
| 316 | 319 | *off = 0; |
| 317 | 320 | *len = 0; |
| 318 | 321 | |
| @@ -384,29 +387,20 @@ smb2_get_data_area_len(int *off, int *len, struct smb2_hdr *shdr) · 65 unmodified lines | ||
| 384 | 387 | * Invalid length or offset probably means data area is invalid, but |
| 385 | 388 | * we have little choice but to ignore the data area in this case. |
| 386 | 389 | */ |
| 387 | − if (*off > 4096) { | |
| 388 | − cifs_dbg(VFS, "offset %d too large, data area ignored\n", *off); | |
| 389 | − *len = 0; | |
| 390 | − *off = 0; | |
| 391 | − } else if (*off < 0) { | |
| 392 | − cifs_dbg(VFS, "negative offset %d to data invalid ignore data area\n", | |
| 393 | − *off); | |
| 390 | + if (unlikely(*off < 0 || *off > max_off || | |
| 391 | + *len < 0 || *len > max_len)) { | |
| 392 | + cifs_dbg(VFS, "%s: invalid data area (off=%d len=%d)\n", | |
| 393 | + __func__, *off, *len); | |
| 394 | 394 | *off = 0; |
| 395 | 395 | *len = 0; |
| 396 | − } else if (*len < 0) { | |
| 397 | − cifs_dbg(VFS, "negative data length %d invalid, data area ignored\n", | |
| 398 | − *len); | |
| 399 | − *len = 0; | |
| 400 | − } else if (*len > 128 * 1024) { | |
| 401 | − cifs_dbg(VFS, "data area larger than 128K: %d\n", *len); | |
| 396 | + } else if (*off == 0) { | |
| 402 | 397 | *len = 0; |
| 403 | 398 | } |
| 404 | 399 | |
| 405 | 400 | /* return pointer to beginning of data area, ie offset from SMB start */ |
| 406 | − if ((*off != 0) && (*len != 0)) | |
| 401 | + if (*off > 0 && *len > 0) | |
| 407 | 402 | return (char *)shdr + *off; |
| 408 | − else | |
| 409 | − return NULL; | |
| 403 | + return NULL; | |
| 410 | 404 | } |
| 411 | 405 | |
| 412 | 406 | /* |
| @@ -3003,7 +3003,7 @@ static int smb2_query_reparse_point(const unsigned int xid, · 3002 unmodified lines | ||
| 3003 | 3003 | struct kvec *rsp_iov; |
| 3004 | 3004 | struct smb2_ioctl_rsp *ioctl_rsp; |
| 3005 | 3005 | struct reparse_data_buffer *reparse_buf; |
| 3006 | − u32 plen; | |
| 3006 | + u32 off, count, len; | |
| 3007 | 3007 | |
| 3008 | 3008 | cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path); |
| 3009 | 3009 | |
| @@ -3084,16 +3084,22 @@ static int smb2_query_reparse_point(const unsigned int xid, · 74 unmodified lines | ||
| 3084 | 3084 | */ |
| 3085 | 3085 | if (rc == 0) { |
| 3086 | 3086 | /* See MS-FSCC 2.3.23 */ |
| 3087 | + off = le32_to_cpu(ioctl_rsp->OutputOffset); | |
| 3088 | + count = le32_to_cpu(ioctl_rsp->OutputCount); | |
| 3089 | + if (check_add_overflow(off, count, &len) || | |
| 3090 | + len > rsp_iov[1].iov_len) { | |
| 3091 | + cifs_tcon_dbg(VFS, "%s: invalid ioctl: off=%d count=%d\n", | |
| 3092 | + __func__, off, count); | |
| 3093 | + rc = -EIO; | |
| 3094 | + goto query_rp_exit; | |
| 3095 | + } | |
| 3087 | 3096 | |
| 3088 | − reparse_buf = (struct reparse_data_buffer *) | |
| 3089 | − ((char *)ioctl_rsp + | |
| 3090 | − le32_to_cpu(ioctl_rsp->OutputOffset)); | |
| 3091 | − plen = le32_to_cpu(ioctl_rsp->OutputCount); | |
| 3092 | − | |
| 3093 | − if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) > | |
| 3094 | − rsp_iov[1].iov_len) { | |
| 3095 | − cifs_tcon_dbg(FYI, "srv returned invalid ioctl len: %d\n", | |
| 3096 | − plen); | |
| 3097 | + reparse_buf = (void *)((u8 *)ioctl_rsp + off); | |
| 3098 | + len = sizeof(*reparse_buf); | |
| 3099 | + if (count < len || | |
| 3100 | + count < le16_to_cpu(reparse_buf->ReparseDataLength) + len) { | |
| 3101 | + cifs_tcon_dbg(VFS, "%s: invalid ioctl: off=%d count=%d\n", | |
| 3102 | + __func__, off, count); | |
| 3097 | 3103 | rc = -EIO; |
| 3098 | 3104 | goto query_rp_exit; |
| 3099 | 3105 | } |
| @@ -4943,6 +4949,7 @@ receive_encrypted_standard(struct TCP_Server_Info *server, · 1843 unmodified lines | ||
| 4943 | 4949 | struct smb2_hdr *shdr; |
| 4944 | 4950 | unsigned int pdu_length = server->pdu_size; |
| 4945 | 4951 | unsigned int buf_size; |
| 4952 | + unsigned int next_cmd; | |
| 4946 | 4953 | struct mid_q_entry *mid_entry; |
| 4947 | 4954 | int next_is_large; |
| 4948 | 4955 | char *next_buffer = NULL; |
| @@ -4971,14 +4978,15 @@ receive_encrypted_standard(struct TCP_Server_Info *server, · 22 unmodified lines | ||
| 4971 | 4978 | next_is_large = server->large_buf; |
| 4972 | 4979 | one_more: |
| 4973 | 4980 | shdr = (struct smb2_hdr *)buf; |
| 4974 | − if (shdr->NextCommand) { | |
| 4981 | + next_cmd = le32_to_cpu(shdr->NextCommand); | |
| 4982 | + if (next_cmd) { | |
| 4983 | + if (WARN_ON_ONCE(next_cmd > pdu_length)) | |
| 4984 | + return -1; | |
| 4975 | 4985 | if (next_is_large) |
| 4976 | 4986 | next_buffer = (char *)cifs_buf_get(); |
| 4977 | 4987 | else |
| 4978 | 4988 | next_buffer = (char *)cifs_small_buf_get(); |
| 4979 | − memcpy(next_buffer, | |
| 4980 | − buf + le32_to_cpu(shdr->NextCommand), | |
| 4981 | − pdu_length - le32_to_cpu(shdr->NextCommand)); | |
| 4989 | + memcpy(next_buffer, buf + next_cmd, pdu_length - next_cmd); | |
| 4982 | 4990 | } |
| 4983 | 4991 | |
| 4984 | 4992 | mid_entry = smb2_find_mid(server, buf); |
| @@ -5002,8 +5010,8 @@ one_more: · 17 unmodified lines | ||
| 5002 | 5010 | else |
| 5003 | 5011 | ret = cifs_handle_standard(server, mid_entry); |
| 5004 | 5012 | |
| 5005 | − if (ret == 0 && shdr->NextCommand) { | |
| 5006 | − pdu_length -= le32_to_cpu(shdr->NextCommand); | |
| 5013 | + if (ret == 0 && next_cmd) { | |
| 5014 | + pdu_length -= next_cmd; | |
| 5007 | 5015 | server->large_buf = next_is_large; |
| 5008 | 5016 | if (next_is_large) |
| 5009 | 5017 | server->bigbuf = buf = next_buffer; |
| @@ -2236,17 +2236,18 @@ parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info, · 2235 unmodified lines | ||
| 2236 | 2236 | posix->nlink, posix->mode, posix->reparse_tag); |
| 2237 | 2237 | } |
| 2238 | 2238 | |
| 2239 | −void | |
| 2240 | −smb2_parse_contexts(struct TCP_Server_Info *server, | |
| 2241 | − struct smb2_create_rsp *rsp, | |
| 2242 | − unsigned int *epoch, char *lease_key, __u8 *oplock, | |
| 2243 | − struct smb2_file_all_info *buf, | |
| 2244 | − struct create_posix_rsp *posix) | |
| 2239 | +int smb2_parse_contexts(struct TCP_Server_Info *server, | |
| 2240 | + struct kvec *rsp_iov, | |
| 2241 | + unsigned int *epoch, | |
| 2242 | + char *lease_key, __u8 *oplock, | |
| 2243 | + struct smb2_file_all_info *buf, | |
| 2244 | + struct create_posix_rsp *posix) | |
| 2245 | 2245 | { |
| 2246 | − char *data_offset; | |
| 2246 | + struct smb2_create_rsp *rsp = rsp_iov->iov_base; | |
| 2247 | 2247 | struct create_context *cc; |
| 2248 | − unsigned int next; | |
| 2249 | − unsigned int remaining; | |
| 2248 | + size_t rem, off, len; | |
| 2249 | + size_t doff, dlen; | |
| 2250 | + size_t noff, nlen; | |
| 2250 | 2251 | char *name; |
| 2251 | 2252 | static const char smb3_create_tag_posix[] = { |
| 2252 | 2253 | 0x93, 0xAD, 0x25, 0x50, 0x9C, |
| @@ -2255,45 +2256,63 @@ smb2_parse_contexts(struct TCP_Server_Info *server, · 2 unmodified lines | ||
| 2255 | 2256 | }; |
| 2256 | 2257 | |
| 2257 | 2258 | *oplock = 0; |
| 2258 | − data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset); | |
| 2259 | − remaining = le32_to_cpu(rsp->CreateContextsLength); | |
| 2260 | − cc = (struct create_context *)data_offset; | |
| 2259 | + | |
| 2260 | + off = le32_to_cpu(rsp->CreateContextsOffset); | |
| 2261 | + rem = le32_to_cpu(rsp->CreateContextsLength); | |
| 2262 | + if (check_add_overflow(off, rem, &len) || len > rsp_iov->iov_len) | |
| 2263 | + return -EINVAL; | |
| 2264 | + cc = (struct create_context *)((u8 *)rsp + off); | |
| 2261 | 2265 | |
| 2262 | 2266 | /* Initialize inode number to 0 in case no valid data in qfid context */ |
| 2263 | 2267 | if (buf) |
| 2264 | 2268 | buf->IndexNumber = 0; |
| 2265 | 2269 | |
| 2266 | − while (remaining >= sizeof(struct create_context)) { | |
| 2267 | − name = le16_to_cpu(cc->NameOffset) + (char *)cc; | |
| 2268 | − if (le16_to_cpu(cc->NameLength) == 4 && | |
| 2269 | − strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0) | |
| 2270 | − *oplock = server->ops->parse_lease_buf(cc, epoch, | |
| 2271 | − lease_key); | |
| 2272 | − else if (buf && (le16_to_cpu(cc->NameLength) == 4) && | |
| 2273 | − strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0) | |
| 2274 | − parse_query_id_ctxt(cc, buf); | |
| 2275 | − else if ((le16_to_cpu(cc->NameLength) == 16)) { | |
| 2276 | − if (posix && | |
| 2277 | − memcmp(name, smb3_create_tag_posix, 16) == 0) | |
| 2270 | + while (rem >= sizeof(*cc)) { | |
| 2271 | + doff = le16_to_cpu(cc->DataOffset); | |
| 2272 | + dlen = le32_to_cpu(cc->DataLength); | |
| 2273 | + if (check_add_overflow(doff, dlen, &len) || len > rem) | |
| 2274 | + return -EINVAL; | |
| 2275 | + | |
| 2276 | + noff = le16_to_cpu(cc->NameOffset); | |
| 2277 | + nlen = le16_to_cpu(cc->NameLength); | |
| 2278 | + if (noff + nlen >= doff) | |
| 2279 | + return -EINVAL; | |
| 2280 | + | |
| 2281 | + name = (char *)cc + noff; | |
| 2282 | + switch (nlen) { | |
| 2283 | + case 4: | |
| 2284 | + if (!strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4)) { | |
| 2285 | + *oplock = server->ops->parse_lease_buf(cc, epoch, | |
| 2286 | + lease_key); | |
| 2287 | + } else if (buf && | |
| 2288 | + !strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4)) { | |
| 2289 | + parse_query_id_ctxt(cc, buf); | |
| 2290 | + } | |
| 2291 | + break; | |
| 2292 | + case 16: | |
| 2293 | + if (posix && !memcmp(name, smb3_create_tag_posix, 16)) | |
| 2278 | 2294 | parse_posix_ctxt(cc, buf, posix); |
| 2295 | + break; | |
| 2296 | + default: | |
| 2297 | + cifs_dbg(FYI, "%s: unhandled context (nlen=%zu dlen=%zu)\n", | |
| 2298 | + __func__, nlen, dlen); | |
| 2299 | + if (IS_ENABLED(CONFIG_CIFS_DEBUG2)) | |
| 2300 | + cifs_dump_mem("context data: ", cc, dlen); | |
| 2301 | + break; | |
| 2279 | 2302 | } |
| 2280 | − /* else { | |
| 2281 | − cifs_dbg(FYI, "Context not matched with len %d\n", | |
| 2282 | − le16_to_cpu(cc->NameLength)); | |
| 2283 | − cifs_dump_mem("Cctxt name: ", name, 4); | |
| 2284 | − } */ | |
| 2285 | − | |
| 2286 | − next = le32_to_cpu(cc->Next); | |
| 2287 | − if (!next) | |
| 2303 | + | |
| 2304 | + off = le32_to_cpu(cc->Next); | |
| 2305 | + if (!off) | |
| 2288 | 2306 | break; |
| 2289 | − remaining -= next; | |
| 2290 | − cc = (struct create_context *)((char *)cc + next); | |
| 2307 | + if (check_sub_overflow(rem, off, &rem)) | |
| 2308 | + return -EINVAL; | |
| 2309 | + cc = (struct create_context *)((u8 *)cc + off); | |
| 2291 | 2310 | } |
| 2292 | 2311 | |
| 2293 | 2312 | if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE) |
| 2294 | 2313 | *oplock = rsp->OplockLevel; |
| 2295 | 2314 | |
| 2296 | − return; | |
| 2315 | + return 0; | |
| 2297 | 2316 | } |
| 2298 | 2317 | |
| 2299 | 2318 | static int |
| @@ -3124,8 +3143,8 @@ SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path, · 824 unmodified lines | ||
| 3124 | 3143 | } |
| 3125 | 3144 | |
| 3126 | 3145 | |
| 3127 | − smb2_parse_contexts(server, rsp, &oparms->fid->epoch, | |
| 3128 | − oparms->fid->lease_key, oplock, buf, posix); | |
| 3146 | + rc = smb2_parse_contexts(server, &rsp_iov, &oparms->fid->epoch, | |
| 3147 | + oparms->fid->lease_key, oplock, buf, posix); | |
| 3129 | 3148 | creat_exit: |
| 3130 | 3149 | SMB2_open_free(&rqst); |
| 3131 | 3150 | free_rsp_buf(resp_buftype, rsp); |
| @@ -251,11 +251,13 @@ extern int smb3_validate_negotiate(const unsigned int, struct cifs_tcon *); · 250 unmodified lines | ||
| 251 | 251 | |
| 252 | 252 | extern enum securityEnum smb2_select_sectype(struct TCP_Server_Info *, |
| 253 | 253 | enum securityEnum); |
| 254 | −extern void smb2_parse_contexts(struct TCP_Server_Info *server, | |
| 255 | − struct smb2_create_rsp *rsp, | |
| 256 | − unsigned int *epoch, char *lease_key, | |
| 257 | − __u8 *oplock, struct smb2_file_all_info *buf, | |
| 258 | − struct create_posix_rsp *posix); | |
| 254 | +int smb2_parse_contexts(struct TCP_Server_Info *server, | |
| 255 | + struct kvec *rsp_iov, | |
| 256 | + unsigned int *epoch, | |
| 257 | + char *lease_key, __u8 *oplock, | |
| 258 | + struct smb2_file_all_info *buf, | |
| 259 | + struct create_posix_rsp *posix); | |
| 260 | + | |
| 259 | 261 | extern int smb3_encryption_required(const struct cifs_tcon *tcon); |
| 260 | 262 | extern int smb2_validate_iov(unsigned int offset, unsigned int buffer_length, |
| 261 | 263 | struct kvec *iov, unsigned int min_buf_size); |