ksmbd: use LOOKUP_BENEATH to prevent the out of share access
Subsystems (2)
PATCH v3 1/1 ksmbd: use LOOKUP_BENEATH to prevent the out of share access
instead of removing '..' in a given path, call kern_path with LOOKUP_BENEATH flag to prevent the out of share access.
ran various test on this:
smb2-cat-async smb://127.0.0.1/homes/../out_of_share smb2-cat-async smb://127.0.0.1/homes/foo/../../out_of_share smbclient //127.0.0.1/homes -c "mkdir ../foo2" smbclient //127.0.0.1/homes -c "rename bar ../bar"
---
Changes from v1:
- pass path of file that is relative to share to ksmbd vfs functions.
Changes from v2:
- fix smbtorture smb2.streams.rename, smb2.streams.rename2 failure
fs/ksmbd/misc.c | 100 ++++++---------------------- fs/ksmbd/misc.h | 7 +- fs/ksmbd/smb2pdu.c | 74 ++++++++------------- fs/ksmbd/vfs.c | 162 ++++++++++++++++++++++++--------------------- fs/ksmbd/vfs.h | 9 ++- 5 files changed, 143 insertions(+), 209 deletions(-)
fs/ksmbd/misc.c +19 -81
@@ -158,25 +158,21 @@ int parse_stream_name(char *filename, char **stream_name, int *s_type)* Return : windows path string or error */ -char *convert_to_nt_pathname (char *filename,char *sharepath) +char *convert_to_nt_pathname (char *filename) {char *ab_pathname; -int len, name_len; - name_len =strlen (filename); - ab_pathname =kmalloc (name_len,GFP_KERNEL ); -if (!ab_pathname) -return NULL ; - - ab_pathname[0 ] ='\\' ; - ab_pathname[1 ] ='\0' ; +if (strlen (filename) ==0 ) { + ab_pathname =kmalloc (2 ,GFP_KERNEL ); + ab_pathname[0 ] ='\\' ; + ab_pathname[1 ] ='\0' ; + }else { + ab_pathname =kstrdup (filename,GFP_KERNEL ); +if (!ab_pathname) +return NULL ; - len =strlen (sharepath); -if (!strncmp (filename, sharepath, len) && name_len != len) { -strscpy (ab_pathname, &filename[len], name_len);ksmbd_conv_path_to_windows (ab_pathname); } -return ab_pathname; }
@@ -191,77 +187,19 @@ int get_nlink(struct kstat *st)return nlink; } -char *ksmbd_conv_path_to_unix (char *path) +void ksmbd_conv_path_to_unix (char *path) { -size_t path_len, remain_path_len, out_path_len; -char *out_path, *out_next; -int i, pre_dotdot_cnt =0 , slash_cnt =0 ; -bool is_last; -strreplace (path,'\\' ,'/' ); - path_len =strlen (path); - remain_path_len = path_len; -if (path_len ==0 ) -return ERR_PTR (-EINVAL ); - - out_path =kzalloc (path_len +2 ,GFP_KERNEL ); -if (!out_path) -return ERR_PTR (-ENOMEM ); - out_path_len =0 ; - out_next = out_path; - -do { -char *name = path + path_len - remain_path_len; -char *next =strchrnul (name,'/' ); -size_t name_len = next - name; - - is_last = !next[0 ]; -if (name_len ==2 && name[0 ] =='.' && name[1 ] =='.' ) { - pre_dotdot_cnt++; -/* handle the case that path ends with "/.." */ -if (is_last) - goto follow_dotdot; - }else { -if (pre_dotdot_cnt) { -follow_dotdot: - slash_cnt =0 ; -for (i = out_path_len -1 ; i >=0 ; i--) { -if (out_path[i] =='/' && - ++slash_cnt == pre_dotdot_cnt +1 ) -break ; - } - -if (i <0 && - slash_cnt != pre_dotdot_cnt) { -kfree (out_path); -return ERR_PTR (-EINVAL ); - } - - out_next = &out_path[i+1 ]; - *out_next ='\0' ; - out_path_len = i +1 ; - - } - -if (name_len !=0 && - !(name_len ==1 && name[0 ] =='.' ) && - !(name_len ==2 && name[0 ] =='.' && name[1 ] =='.' )) { - next[0 ] ='\0' ; -sprintf (out_next,"%s/" , name); - out_next += name_len +1 ; - out_path_len += name_len +1 ; - next[0 ] ='/' ; - } - pre_dotdot_cnt =0 ; - } +} - remain_path_len -= name_len +1 ; - }while (!is_last); +void ksmbd_strip_last_slash (char *path) +{ +int len =strlen (path); -if (out_path_len >0 ) - out_path[out_path_len-1 ] ='\0' ; - path[path_len] ='\0' ; -return out_path; +while (len && path[len -1 ] =='/' ) { + path[len -1 ] ='\0' ; + len--; + } }void ksmbd_conv_path_to_windows (char *path)
@@ -298,7 +236,7 @@ char *ksmbd_extract_sharename(char *treename)* * Return: converted name on success, otherwise NULL */ -char *convert_to_unix_name (struct ksmbd_share_config *share,char *name) +char *convert_to_unix_name (struct ksmbd_share_config *share,const char *name) {int no_slash =0 , name_len, path_len;char *new_name;
fs/ksmbd/misc.h +4 -3
@@ -14,12 +14,13 @@ struct ksmbd_file;int match_pattern (const char *str,size_t len,const char *pattern);int ksmbd_validate_filename (char *filename);int parse_stream_name (char *filename,char **stream_name,int *s_type); -char *convert_to_nt_pathname (char *filename,char *sharepath); +char *convert_to_nt_pathname (char *filename);int get_nlink (struct kstat *st); -char *ksmbd_conv_path_to_unix (char *path); +void ksmbd_conv_path_to_unix (char *path); +void ksmbd_strip_last_slash (char *path);void ksmbd_conv_path_to_windows (char *path);char *ksmbd_extract_sharename (char *treename); -char *convert_to_unix_name (struct ksmbd_share_config *share,char *name); +char *convert_to_unix_name (struct ksmbd_share_config *share,const char *name);#define KSMBD_DIR_INFO_ALIGNMENT 8struct ksmbd_dir_info ;
fs/ksmbd/smb2pdu.c +28 -46
179 lines not on this page · raw message
fs/ksmbd/vfs.c +85 -77
302 lines not on this page · raw message
fs/ksmbd/vfs.h +7 -2
@@ -126,7 +126,7 @@ int ksmbd_vfs_link(struct ksmbd_work *work,int ksmbd_vfs_getattr (struct path *path,struct kstat *stat);int ksmbd_vfs_fp_rename (struct ksmbd_work *work,struct ksmbd_file *fp,char *newname); -int ksmbd_vfs_truncate (struct ksmbd_work *work,const char *name, +int ksmbd_vfs_truncate (struct ksmbd_work *work,struct ksmbd_file *fp,loff_t size);struct srv_copychunk ; int ksmbd_vfs_copy_file_ranges(struct ksmbd_work *work,
@@ -152,8 +152,13 @@ int ksmbd_vfs_xattr_stream_name(char *stream_name, char **xattr_stream_name, size_t *xattr_stream_name_size, int s_type);int ksmbd_vfs_remove_xattr (struct user_namespace *user_ns,struct dentry *dentry,char *attr_name); -int ksmbd_vfs_kern_path (char *name,unsigned int flags,struct path *path, +int ksmbd_vfs_kern_path (struct ksmbd_work *work, +char *name,unsigned int flags,struct path *path,bool caseless); +struct dentry *ksmbd_vfs_kern_path_create (struct ksmbd_work *work, +const char *name, +unsigned int flags, +struct path *path);int ksmbd_vfs_empty_dir (struct ksmbd_file *fp);void ksmbd_vfs_set_fadvise (struct file *filp,__le32 option); int ksmbd_vfs_zero_data(struct ksmbd_work *work, struct ksmbd_file *fp,
Signature · 1 line
2.25.1
PATCH v4 1/1 ksmbd: add buffer validation for SMB2_CREATE_CONTEXT
Add buffer validation for SMB2_CREATE_CONTEXT.
---
Changes from v2:
- check struct create_context's fields more in smb2_find_context_vals()
Changes from v3:
- fix possible overflow when accessing create_context structure in smb2_find_context_vals()
- fix possible overflow when accessing smb_ace structure in parse_dacl()
fs/ksmbd/oplock.c | 41 +++++++++++++++++++++++++++++++---------- fs/ksmbd/smb2pdu.c | 25 ++++++++++++++++++++++++- fs/ksmbd/smbacl.c | 21 +++++++++++++++++++-- 3 files changed, 74 insertions(+), 13 deletions(-)
fs/ksmbd/oplock.c +31 -10
@@ -1451,26 +1451,47 @@ struct lease_ctx_info *parse_lease_state(void *open_req)*/ struct create_context *smb2_find_context_vals (void *open_req,const char *tag) { -char *data_offset;struct create_context *cc;unsigned int next =0 ;char *name;struct smb2_create_req *req = (struct smb2_create_req *)open_req; +unsigned int remain_len, name_off, name_len, value_off, value_len, + cc_len; - data_offset = (char *)req +4 +le32_to_cpu (req->CreateContextsOffset); - cc = (struct create_context *)data_offset; +/* +* CreateContextsOffset and CreateContextsLength are guaranteed to +* be valid because of ksmbd_smb2_check_message(). +*/ + cc = (struct create_context *)((char *)req +4 + +le32_to_cpu (req->CreateContextsOffset)); + remain_len =le32_to_cpu (req->CreateContextsLength);do { -int val; - cc = (struct create_context *)((char *)cc + next); - name =le16_to_cpu (cc->NameOffset) + (char *)cc; - val =le16_to_cpu (cc->NameLength); -if (val <4 ) +if (remain_len < offsetof(struct create_context , Buffer))return ERR_PTR (-EINVAL ); -if (memcmp (name, tag, val) ==0 ) -return cc; next =le32_to_cpu (cc->Next); + name_off =le16_to_cpu (cc->NameOffset); + name_len =le16_to_cpu (cc->NameLength); + value_off =le16_to_cpu (cc->DataOffset); + value_len =le32_to_cpu (cc->DataLength); + cc_len = next ? next : remain_len; + +if ((next &0x7 ) !=0 || + next > remain_len || + name_off != offsetof(struct create_context , Buffer) || + name_len <4 || + name_off + name_len > cc_len || + (value_off &0x7 ) !=0 || + (value_off && (value_off < name_off + name_len)) || + ((u64 )value_off + value_len > cc_len)) +return ERR_PTR (-EINVAL ); + + name = (char *)cc + name_off; +if (memcmp (name, tag, name_len) ==0 ) +return cc; + + remain_len -= next; }while (next !=0 );return NULL ;
fs/ksmbd/smb2pdu.c +24 -1
@@ -2427,6 +2427,10 @@ static int smb2_create_sd_buffer(struct ksmbd_work *work,ksmbd_debug (SMB ,"Set ACLs using SMB2_CREATE_SD_BUFFER context\n" ); sd_buf = (struct create_sd_buf_req *)context; +if (le16_to_cpu (context->DataOffset) + +le32_to_cpu (context->DataLength) < +sizeof (struct create_sd_buf_req )) +return -EINVAL ;return set_info_sec (work->conn, work->tcon, path, &sd_buf->ntsd,le32_to_cpu (sd_buf->ccontext.DataLength), true); }
@@ -2621,6 +2625,12 @@ int smb2_open(struct ksmbd_work *work) goto err_out1; }else if (context) { ea_buf = (struct create_ea_buf_req *)context; +if (le16_to_cpu (context->DataOffset) + +le32_to_cpu (context->DataLength) < +sizeof (struct create_ea_buf_req )) { + rc = -EINVAL ; + goto err_out1; + }if (req->CreateOptions &FILE_NO_EA_KNOWLEDGE_LE ) { rsp->hdr.Status =STATUS_ACCESS_DENIED ; rc = -EACCES ;
@@ -2659,6 +2669,12 @@ int smb2_open(struct ksmbd_work *work) }else if (context) {struct create_posix *posix = (struct create_posix *)context; +if (le16_to_cpu (context->DataOffset) + +le32_to_cpu (context->DataLength) < +sizeof (struct create_posix )) { + rc = -EINVAL ; + goto err_out1; + }ksmbd_debug (SMB ,"get posix context\n" ); posix_mode =le32_to_cpu (posix->Mode);
@@ -3049,9 +3065,16 @@ int smb2_open(struct ksmbd_work *work) rc =PTR_ERR (az_req); goto err_out; }else if (az_req) { -loff_t alloc_size =le64_to_cpu (az_req->AllocationSize); +loff_t alloc_size;int err; +if (le16_to_cpu (az_req->ccontext.DataOffset) + +le32_to_cpu (az_req->ccontext.DataLength) < +sizeof (struct create_alloc_size_req )) { + rc = -EINVAL ; + goto err_out; + } + alloc_size =le64_to_cpu (az_req->AllocationSize);ksmbd_debug (SMB ,"request smb2 create allocate size : %llu\n" , alloc_size);
fs/ksmbd/smbacl.c +19 -2
@@ -380,7 +380,7 @@ static void parse_dacl(struct user_namespace *user_ns, {int i, ret;int num_aces =0 ; -int acl_size; +unsigned int acl_size;char *acl_base;struct smb_ace **ppace;struct posix_acl_entry *cf_pace, *cf_pdace;
@@ -392,7 +392,7 @@ static void parse_dacl(struct user_namespace *user_ns,return ;/* validate that we do not go past end of acl */ -if (end_of_acl <= (char *)pdacl || +if (end_of_acl < (char *)pdacl +sizeof (struct smb_acl ) || end_of_acl < (char *)pdacl +le16_to_cpu (pdacl->size)) {pr_err ("ACL too small to parse DACL\n" );return ;
@@ -431,8 +431,22 @@ static void parse_dacl(struct user_namespace *user_ns,* user/group/other have no permissions */ for (i =0 ; i < num_aces; ++i) { +if (end_of_acl - acl_base < acl_size) +break ; + ppace[i] = (struct smb_ace *)(acl_base + acl_size); acl_base = (char *)ppace[i]; + acl_size = offsetof(struct smb_ace , sid) + + offsetof(struct smb_sid , sub_auth); + +if (end_of_acl - acl_base < acl_size || + ppace[i]->sid.num_subauth >SID_MAX_SUB_AUTHORITIES || + (end_of_acl - acl_base < + acl_size +sizeof (__le32) * ppace[i]->sid.num_subauth) || + (le16_to_cpu (ppace[i]->size) < + acl_size +sizeof (__le32) * ppace[i]->sid.num_subauth)) +break ; + acl_size =le16_to_cpu (ppace[i]->size); ppace[i]->access_req =smb_map_generic_desired_access (ppace[i]->access_req);
@@ -807,6 +821,9 @@ int parse_sec_desc(struct user_namespace *user_ns, struct smb_ntsd *pntsd,if (!pntsd)return -EIO ; +if (acl_len <sizeof (struct smb_ntsd )) +return -EINVAL ; + owner_sid_ptr = (struct smb_sid *)((char *)pntsd +le32_to_cpu (pntsd->osidoffset)); group_sid_ptr = (struct smb_sid *)((char *)pntsd +
Signature · 1 line
2.25.1