History

Merge tag 'cifs-fixes-7.3-rc1' of https://git.manguebit.org/linux

Pull smb client updates from Paulo Alcantara: - clear sensitive data after use (stack and heap cryptographic keys/hashes) - file size and cache synchronization fixes (fscache cookie serialization and truncation handling) - protocol validation and buffer safety fixes (prevent OOB access and loff_t underflow) - metadata and POSIX attribute fixes (proper hard-link counts and setuid/setgid stripping) - DFS cache and unmount fixes (prevent target-hint UAF and unmount hangs) - general client improvements (fix read request leaks, stats loops, handle servers that don't support O_TMPFILE) * tag 'cifs-fixes-7.3-rc1' of https://git.manguebit.org/linux: (33 commits) cifs: fix loff_t underflow in cifs_remap_file_range() when len == 0 smb: client: reject a tree connect response whose byte count is too small cifs: call pagecache_isize_extended() in cifs_setsize() when extending smb: client: fix copy-paste error in WSL EA length accounting for $LXDEV smb: client: remove redundant NULL check before kfree() smb: client: restore the data_offset bound in is_valid_oplock_break() cifs: clear tcon after cifsFileInfo_put() in cifs_file_set_size() smb: client: Avoid leaking sensitive data to the heap in connect.c smb: client: Clear sensitive stack data in smb1encrypt.c smb: client: Clear sensitive stack data in cifsencrypt.c smb: client: Clear sensitive stack and heap data in smb2ops.c smb: client: Clear sensitive stack data in smb2transport.c Revert "cifs: remove all cifs files before kill super" smb: client: fix use-before-check of ReparseDataLength in reparse_buf_ptr() smb: client: fix ALIGN() overflow in symlink_data() error context loop smb: client: simplify __build_path_from_dentry_optional_prefix() smb: client: fix UAF and buffer leak in cifs_check_trans2() for malformed secondary T2 smb: client: fix OOB read/write from unvalidated DataOffset in coalesce_t2() smb/client: decode reparse metadata using its payload type smb/client: preserve open info type across compound queries ...

Linus Torvalds committed 2 months ago

Commit ce14fe42 parents 66498c7 6c322f522 files · +223 −108Browse files

Unified · Split

22 files
  1. fs/smb/client/cifs_debug.c +1 −1
  2. fs/smb/client/cifsencrypt.c +9 −3
  3. fs/smb/client/cifsfs.c +14 −7
  4. fs/smb/client/cifsfs.h +1 −0
  5. fs/smb/client/cifssmb.c +6 −0
  6. fs/smb/client/connect.c +1 −4
  7. fs/smb/client/dfs_cache.c +26 −7
  8. fs/smb/client/dir.c +3 −5
  9. fs/smb/client/file.c +1 −0
  10. fs/smb/client/inode.c +57 −33
  11. fs/smb/client/ioctl.c +1 −2
  12. fs/smb/client/reparse.h +10 −7
  13. fs/smb/client/smb1encrypt.c +9 −10
  14. fs/smb/client/smb1misc.c +2 −1
  15. fs/smb/client/smb1ops.c +8 −2
  16. fs/smb/client/smb1transport.c +31 −4
  17. fs/smb/client/smb2file.c +4 −1
  18. fs/smb/client/smb2inode.c +14 −8
  19. fs/smb/client/smb2ops.c +5 −8
  20. fs/smb/client/smb2pdu.c +14 −4
  21. fs/smb/client/smb2transport.c +4 −0
  22. fs/smb/client/trace.h +2 −1
fs/smb/client/cifs_debug.c+1−1
@@ -754,7 +754,7 @@ static ssize_t cifs_stats_proc_write(struct file *file, · 753 unmodified lines
754754 atomic_set(&server->smb2slowcmd[i], 0);
755755 server->time_per_cmd[i] = 0;
756756 server->slowest_cmd[i] = 0;
757− server->fastest_cmd[0] = 0;
757+ server->fastest_cmd[i] = 0;
758758 }
759759 #endif /* CONFIG_CIFS_STATS2 */
760760 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
fs/smb/client/cifsencrypt.c+9−3
@@ -249,12 +249,13 @@ static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash, · 248 unmodified lines
249249 E_md4hash(ses->password, nt_hash, nls_cp);
250250
251251 hmac_md5_init_usingrawkey(&hmac_ctx, nt_hash, CIFS_NTHASH_SIZE);
252+ memzero_explicit(nt_hash, sizeof(nt_hash));
252253
253254 /* convert ses->user_name to unicode */
254255 len = ses->user_name ? strlen(ses->user_name) : 0;
255256 user = kmalloc(2 + (len * 2), GFP_KERNEL);
256257 if (user == NULL)
257− return -ENOMEM;
258+ goto out_nomem;
258259
259260 if (len) {
260261 len = cifs_strtoUTF16(user, ses->user_name, len, nls_cp);
@@ -272,7 +273,7 @@ static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash, · 11 unmodified lines
272273
273274 domain = kmalloc(2 + (len * 2), GFP_KERNEL);
274275 if (domain == NULL)
275− return -ENOMEM;
276+ goto out_nomem;
276277
277278 len = cifs_strtoUTF16((__le16 *)domain, ses->domainName, len,
278279 nls_cp);
@@ -284,7 +285,7 @@ static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash, · 5 unmodified lines
284285
285286 server = kmalloc(2 + (len * 2), GFP_KERNEL);
286287 if (server == NULL)
287− return -ENOMEM;
288+ goto out_nomem;
288289
289290 len = cifs_strtoUTF16((__le16 *)server, ses->ip_addr, len, nls_cp);
290291 hmac_md5_update(&hmac_ctx, (const u8 *)server, 2 * len);
@@ -293,6 +294,10 @@ static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash, · 2 unmodified lines
293294
294295 hmac_md5_final(&hmac_ctx, ntlmv2_hash);
295296 return 0;
297+
298+out_nomem:
299+ memzero_explicit(&hmac_ctx, sizeof(hmac_ctx));
300+ return -ENOMEM;
296301 }
297302
298303 static void CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash)
@@ -463,6 +468,7 @@ setup_ntlmv2_rsp(struct cifs_ses *ses, const struct nls_table *nls_cp) · 164 unmodified lines
463468 rc = 0;
464469 unlock:
465470 cifs_server_unlock(ses->server);
471+ memzero_explicit(ntlmv2_hash, sizeof(ntlmv2_hash));
466472 setup_ntlmv2_rsp_ret:
467473 kfree_sensitive(tiblob);
468474
fs/smb/client/cifsfs.c+14−7
@@ -1413,8 +1413,19 @@ static loff_t cifs_remap_file_range(struct file *src_file, loff_t off, · 1412 unmodified lines
14131413 */
14141414 lock_two_nondirectories(target_inode, src_inode);
14151415
1416− if (len == 0)
1417− len = src_inode->i_size - off;
1416+ if (len == 0) {
1417+ loff_t src_size = i_size_read(src_inode);
1418+
1419+ if (off > src_size) {
1420+ rc = -EINVAL;
1421+ goto unlock;
1422+ }
1423+ len = src_size - off;
1424+ if (!len) {
1425+ rc = 0;
1426+ goto unlock;
1427+ }
1428+ }
14181429
14191430 cifs_dbg(FYI, "clone range\n");
14201431
@@ -1466,11 +1477,7 @@ static loff_t cifs_remap_file_range(struct file *src_file, loff_t off, · 45 unmodified lines
14661477 if (target_tcon->ses->server->ops->duplicate_extents) {
14671478 rc = target_tcon->ses->server->ops->duplicate_extents(xid,
14681479 smb_file_src, smb_file_target, off, len, destoff);
1469− if (rc == 0 && new_size > i_size) {
1470− truncate_setsize(target_inode, new_size);
1471− fscache_resize_cookie(cifs_inode_cookie(target_inode),
1472− new_size);
1473− } else if (rc == -EOPNOTSUPP) {
1480+ if (rc == -EOPNOTSUPP) {
14741481 /*
14751482 * copy_file_range syscall man page indicates EINVAL
14761483 * is returned e.g when "fd_in and fd_out refer to the
fs/smb/client/cifsfs.h+1−0
@@ -147,6 +147,7 @@ ssize_t cifs_file_copychunk_range(unsigned int xid, struct file *src_file, · 146 unmodified lines
147147
148148 long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg);
149149 void cifs_setsize(struct inode *inode, loff_t offset);
150+void cifs_resize_file_locked(struct inode *inode, loff_t offset);
150151
151152 struct fs_context;
152153 struct smb3_fs_context;
fs/smb/client/cifssmb.c+6−0
@@ -615,6 +615,11 @@ CIFSTCon(const unsigned int xid, struct cifs_ses *ses, · 614 unmodified lines
615615 tcon->tid = smb_buffer_response->Tid;
616616 bcc_ptr = pByteArea(smb_buffer_response);
617617 bytes_left = get_bcc(smb_buffer_response);
618+ if (bytes_left < 2) {
619+ rc = smb_EIO2(smb_eio_trace_tcon_bcc_too_small,
620+ bytes_left, 2);
621+ goto out;
622+ }
618623 length = strnlen(bcc_ptr, bytes_left - 2);
619624 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
620625 is_unicode = true;
@@ -670,6 +675,7 @@ CIFSTCon(const unsigned int xid, struct cifs_ses *ses, · 49 unmodified lines
670675 reset_cifs_unix_caps(xid, tcon, NULL, NULL);
671676 }
672677 }
678+out:
673679 cifs_buf_release(smb_buffer);
674680 return rc;
675681 }
fs/smb/client/connect.c+1−4
@@ -1143,7 +1143,7 @@ clean_demultiplex_info(struct TCP_Server_Info *server) · 1142 unmodified lines
11431143 put_net(cifs_net_ns(server));
11441144 kfree(server->leaf_fullpath);
11451145 kfree(server->hostname);
1146− kfree(server);
1146+ kfree_sensitive(server);
11471147
11481148 length = atomic_dec_return(&tcpSesAllocCount);
11491149 if (length > 0)
@@ -4003,9 +4003,6 @@ cifs_umount(struct cifs_sb_info *cifs_sb) · 2853 unmodified lines
40034003 }
40044004 spin_unlock(&cifs_sb->tlink_tree_lock);
40054005
4006− flush_workqueue(serverclose_wq);
4007− flush_workqueue(fileinfo_put_wq);
4008−
40094006 kfree(cifs_sb->prepath);
40104007 call_rcu(&cifs_sb->rcu, delayed_free);
40114008 }
fs/smb/client/dfs_cache.c+26−7
@@ -122,6 +122,8 @@ static inline void free_tgts(struct cache_entry *ce) · 121 unmodified lines
122122 kfree(t->name);
123123 kfree(t);
124124 }
125+
126+ WRITE_ONCE(ce->tgthint, NULL);
125127 }
126128
127129 static inline void flush_cache_ent(struct cache_entry *ce)
@@ -869,13 +871,22 @@ int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses, const struct nl · 741 unmodified lines
869871 goto out_free_path;
870872 }
871873
872− if (ref)
873− rc = setup_referral(path, ce, ref, get_tgt_name(ce));
874− else
874+ if (ref) {
875+ char *target = get_tgt_name(ce);
876+
877+ if (IS_ERR(target)) {
878+ rc = PTR_ERR(target);
879+ goto out_unlock;
880+ }
881+ rc = setup_referral(path, ce, ref, target);
882+ } else {
875883 rc = 0;
884+ }
885+
876886 if (!rc && tgt_list)
877887 rc = get_targets(ce, tgt_list);
878888
889+out_unlock:
879890 up_read(&htable_rw_lock);
880891
881892 out_free_path:
@@ -915,10 +926,17 @@ int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref, · 33 unmodified lines
915926 goto out_unlock;
916927 }
917928
918− if (ref)
919− rc = setup_referral(path, ce, ref, get_tgt_name(ce));
920− else
929+ if (ref) {
930+ char *target = get_tgt_name(ce);
931+
932+ if (IS_ERR(target)) {
933+ rc = PTR_ERR(target);
934+ goto out_unlock;
935+ }
936+ rc = setup_referral(path, ce, ref, target);
937+ } else {
921938 rc = 0;
939+ }
922940 if (!rc && tgt_list)
923941 rc = get_targets(ce, tgt_list);
924942
@@ -959,7 +977,8 @@ void dfs_cache_noreq_update_tgthint(const char *path, const struct dfs_cache_tgt · 34 unmodified lines
959977
960978 t = READ_ONCE(ce->tgthint);
961979
962− if (unlikely(!strcasecmp(it->it_name, t->name)))
980+ /* Check 't' in case ce->tgthint was cleared by free_tgts() */
981+ if (t && unlikely(!strcasecmp(it->it_name, t->name)))
963982 goto out_unlock;
964983
965984 list_for_each_entry(t, &ce->tlist, list) {
fs/smb/client/dir.c+3−5
@@ -115,11 +115,7 @@ char *__build_path_from_dentry_optional_prefix(struct dentry *direntry, void *pa · 114 unmodified lines
115115 }
116116 if (dirsep != '/') {
117117 /* BB test paths to Windows with '/' in the midst of prepath */
118− char *p;
119−
120− for (p = s; *p; p++)
121− if (*p == '/')
122− *p = dirsep;
118+ strreplace(s, '/', dirsep);
123119 }
124120 if (dfsplen) {
125121 s -= dfsplen;
@@ -1138,6 +1134,8 @@ int cifs_tmpfile(struct mnt_idmap *idmap, struct inode *dir, · 1012 unmodified lines
11381134 } while (unlikely(rc == -EEXIST) && ++retries < max_retries);
11391135
11401136 if (rc) {
1137+ if (rc == -ENOENT)
1138+ rc = -EOPNOTSUPP;
11411139 cifs_del_pending_open(&open);
11421140 goto out;
11431141 }
fs/smb/client/file.c+1−0
@@ -1016,6 +1016,7 @@ static int cifs_do_truncate(const unsigned int xid, struct dentry *dentry) · 1015 unmodified lines
10161016 if (!rc) {
10171017 netfs_resize_file(&cinode->netfs, 0, true);
10181018 cifs_setsize(inode, 0);
1019+ cifs_invalidate_cache(inode, 0);
10191020 }
10201021 }
10211022 if (cfile)
fs/smb/client/inode.c+57−33
@@ -1215,7 +1215,7 @@ static int reparse_info_to_fattr(struct cifs_open_info_data *data, · 1214 unmodified lines
12151215 break;
12161216 case IO_REPARSE_TAG_INTERNAL:
12171217 rc = 0;
1218− if (le32_to_cpu(data->fi.Attributes) & ATTR_DIRECTORY) {
1218+ if (cifs_open_data_attrs(data) & ATTR_DIRECTORY) {
12191219 cifs_create_junction_fattr(fattr, sb);
12201220 goto out;
12211221 }
@@ -1239,7 +1239,7 @@ static int reparse_info_to_fattr(struct cifs_open_info_data *data, · 17 unmodified lines
12391239 */
12401240 if (rc == -EOPNOTSUPP &&
12411241 IS_REPARSE_TAG_NAME_SURROGATE(data->reparse.tag) &&
1242− (le32_to_cpu(data->fi.Attributes) & ATTR_DIRECTORY)) {
1242+ (cifs_open_data_attrs(data) & ATTR_DIRECTORY)) {
12431243 rc = 0;
12441244 cifs_create_junction_fattr(fattr, sb);
12451245 goto out;
@@ -1257,13 +1257,14 @@ static int reparse_info_to_fattr(struct cifs_open_info_data *data, · 11 unmodified lines
12571257 }
12581258
12591259 if (data->reparse.tag == IO_REPARSE_TAG_SYMLINK && !rc) {
1260− bool directory = le32_to_cpu(data->fi.Attributes) & ATTR_DIRECTORY;
1260+ bool directory = cifs_open_data_attrs(data) & ATTR_DIRECTORY;
1261+
12611262 rc = smb2_fix_symlink_target_type(&data->symlink_target, directory, cifs_sb);
12621263 }
12631264 break;
12641265 }
12651266
1266− if (tcon->posix_extensions)
1267+ if (data->contains_posix_file_info)
12671268 smb311_posix_info_to_fattr(fattr, data, sb);
12681269 else
12691270 cifs_open_info_to_fattr(fattr, data, sb);
@@ -2655,11 +2656,8 @@ unlink_target: · 1385 unmodified lines
26552656 if (d_really_is_positive(target_dentry)) {
26562657 if (!rc) {
26572658 struct inode *inode = d_inode(target_dentry);
2658− /*
2659− * Samba and ksmbd servers allow renaming a target
2660− * directory that is open, so make sure to update
2661− * ->i_nlink and then mark it as delete pending.
2662− */
2659+
2660+ /* Update the target link count after rename. */
26632661 if (S_ISDIR(inode->i_mode)) {
26642662 drop_cached_dir_by_name(xid, tcon, to_name, cifs_sb);
26652663 spin_lock(&inode->i_lock);
@@ -2670,6 +2668,10 @@ unlink_target: · 4 unmodified lines
26702668 CIFS_I(inode)->time = 0; /* force reval */
26712669 inode_set_ctime_current(inode);
26722670 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
2671+ } else {
2672+ cifs_mark_open_handles_for_deleted_file(inode, to_name);
2673+ cifs_drop_nlink(inode);
2674+ inode_set_ctime_current(inode);
26732675 }
26742676 } else if (rc == -EACCES || rc == -EEXIST) {
26752677 /*
@@ -3064,9 +3066,27 @@ void cifs_setsize(struct inode *inode, loff_t offset) · 388 unmodified lines
30643066 inode->i_blocks = blocks;
30653067 spin_unlock(&inode->i_lock);
30663068 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
3069+ if (offset > old_size)
3070+ pagecache_isize_extended(inode, old_size, offset);
30673071 truncate_pagecache(inode, offset);
30683072 netfs_wait_for_outstanding_io(inode);
3069− fscache_resize_cookie(cifs_inode_cookie(inode), offset);
3073+}
3074+
3075+void cifs_resize_file_locked(struct inode *inode, loff_t offset)
3076+{
3077+ struct fscache_cookie *cookie = cifs_inode_cookie(inode);
3078+
3079+ lockdep_assert_held_write(&inode->i_rwsem);
3080+
3081+ netfs_resize_file(netfs_inode(inode), offset, true);
3082+ cifs_setsize(inode, offset);
3083+
3084+ if (!cookie)
3085+ return;
3086+
3087+ fscache_use_cookie(cookie, true);
3088+ fscache_resize_cookie(cookie, offset);
3089+ cifs_fscache_unuse_inode_cookie(inode, true);
30703090 }
30713091
30723092 int cifs_file_set_size(const unsigned int xid, struct dentry *dentry,
@@ -3107,6 +3127,7 @@ int cifs_file_set_size(const unsigned int xid, struct dentry *dentry, · 34 unmodified lines
31073127 size, false);
31083128 cifs_dbg(FYI, "%s: set_file_size: rc = %d\n", __func__, rc);
31093129 cifsFileInfo_put(open_file);
3130+ tcon = NULL;
31103131 }
31113132 }
31123133
@@ -3132,10 +3153,8 @@ int cifs_file_set_size(const unsigned int xid, struct dentry *dentry, · 19 unmodified lines
31323153 cifs_put_tlink(tlink);
31333154
31343155 set_size_out:
3135− if (rc == 0) {
3136− netfs_resize_file(&cifsInode->netfs, size, true);
3137− cifs_setsize(inode, size);
3138− }
3156+ if (rc == 0)
3157+ cifs_resize_file_locked(inode, size);
31393158
31403159 return rc;
31413160 }
@@ -3221,9 +3240,13 @@ cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs) · 79 unmodified lines
32213240 attrs->ia_valid &= ~(ATTR_CTIME | ATTR_MTIME);
32223241 }
32233242
3224− /* skip mode change if it's just for clearing setuid/setgid */
3225− if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
3226− attrs->ia_valid &= ~ATTR_MODE;
3243+ /*
3244+ * This function is only called when Unix extensions are in effect,
3245+ * so the mode is always sent to and stored on the server. Do not
3246+ * skip the mode change when clearing setuid/setgid bits: dropping
3247+ * ATTR_MODE here would leave those bits set on the server after a
3248+ * write, which is a security issue.
3249+ */
32273250
32283251 args = kmalloc_obj(*args);
32293252 if (args == NULL) {
@@ -3296,13 +3319,6 @@ cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs) · 66 unmodified lines
32963319 if (rc)
32973320 goto out;
32983321
3299− if ((attrs->ia_valid & ATTR_SIZE) &&
3300− attrs->ia_size != i_size_read(inode)) {
3301− truncate_setsize(inode, attrs->ia_size);
3302− netfs_resize_file(&cifsInode->netfs, attrs->ia_size, true);
3303− fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
3304− }
3305−
33063322 setattr_copy(&nop_mnt_idmap, inode, attrs);
33073323 mark_inode_dirty(inode);
33083324
@@ -3432,8 +3448,23 @@ cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs) · 123 unmodified lines
34323448 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
34333449 }
34343450
3435− /* skip mode change if it's just for clearing setuid/setgid */
3436− if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
3451+ /*
3452+ * Skip the mode change if it is only being done to clear the
3453+ * setuid/setgid bits *and* the mode is emulated via the DOS
3454+ * read-only attribute (the default, non-ACL case), which cannot
3455+ * represent the setuid/setgid bits anyway.
3456+ *
3457+ * When the mode is instead stored on the server - i.e. with the
3458+ * cifsacl or modefromsid mount options (via an ACL) or with the
3459+ * SMB3.1.1 POSIX extensions - the cleared mode must be pushed to
3460+ * the server. Dropping ATTR_MODE here would leave the setuid/
3461+ * setgid bit set on the server after a write, which is a security
3462+ * issue (the bits are not stripped as they are on local
3463+ * filesystems).
3464+ */
3465+ if ((attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
3466+ !((sbflags & (CIFS_MOUNT_CIFS_ACL | CIFS_MOUNT_MODE_FROM_SID)) ||
3467+ cifs_sb_master_tcon(cifs_sb)->posix_extensions))
34373468 attrs->ia_valid &= ~ATTR_MODE;
34383469
34393470 if (attrs->ia_valid & ATTR_MODE) {
@@ -3508,13 +3539,6 @@ cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs) · 68 unmodified lines
35083539 if (rc)
35093540 goto cifs_setattr_exit;
35103541
3511− if ((attrs->ia_valid & ATTR_SIZE) &&
3512− attrs->ia_size != i_size_read(inode)) {
3513− truncate_setsize(inode, attrs->ia_size);
3514− netfs_resize_file(&cifsInode->netfs, attrs->ia_size, true);
3515− fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
3516− }
3517−
35183542 setattr_copy(&nop_mnt_idmap, inode, attrs);
35193543 mark_inode_dirty(inode);
35203544
fs/smb/client/ioctl.c+1−2
@@ -133,8 +133,7 @@ static int cifs_set_compression_by_path(unsigned int xid, struct file *filep, · 132 unmodified lines
133133
134134 close:
135135 server->ops->close(xid, tcon, &fid);
136− if (tmp_cfile)
137− kfree(tmp_cfile);
136+ kfree(tmp_cfile);
138137 cifs_free_open_info(&data);
139138 out:
140139 free_dentry_path(page);
fs/smb/client/reparse.h+10−7
@@ -98,15 +98,21 @@ static inline bool reparse_inode_match(struct inode *inode, · 97 unmodified lines
9898 timespec64_equal(&ctime, &fattr->cf_ctime);
9999 }
100100
101+static inline u32 cifs_open_data_attrs(const struct cifs_open_info_data *data)
102+{
103+ if (data->contains_posix_file_info)
104+ return le32_to_cpu(data->posix_fi.DosAttributes);
105+
106+ return le32_to_cpu(data->fi.Attributes);
107+}
108+
101109 static inline bool cifs_open_data_reparse(struct cifs_open_info_data *data)
102110 {
103− u32 attrs;
104− bool ret;
111+ u32 attrs = cifs_open_data_attrs(data);
105112
106113 if (data->contains_posix_file_info) {
107114 struct smb311_posix_qinfo *fi = &data->posix_fi;
108115
109− attrs = le32_to_cpu(fi->DosAttributes);
110116 if (data->reparse_point) {
111117 attrs |= ATTR_REPARSE_POINT;
112118 fi->DosAttributes = cpu_to_le32(attrs);
@@ -115,16 +121,13 @@ static inline bool cifs_open_data_reparse(struct cifs_open_info_data *data) · 2 unmodified lines
115121 } else {
116122 struct smb2_file_all_info *fi = &data->fi;
117123
118− attrs = le32_to_cpu(fi->Attributes);
119124 if (data->reparse_point) {
120125 attrs |= ATTR_REPARSE_POINT;
121126 fi->Attributes = cpu_to_le32(attrs);
122127 }
123128 }
124129
125− ret = attrs & ATTR_REPARSE_POINT;
126−
127− return ret;
130+ return attrs & ATTR_REPARSE_POINT;
128131 }
129132
130133 bool cifs_reparse_point_to_fattr(struct cifs_sb_info *cifs_sb,
fs/smb/client/smb1encrypt.c+9−10
@@ -81,6 +81,7 @@ int cifs_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server, · 80 unmodified lines
8181 else
8282 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
8383
84+ memzero_explicit(smb_signature, sizeof(smb_signature));
8485 return rc;
8586 }
8687
@@ -126,15 +127,13 @@ int cifs_verify_signature(struct smb_rqst *rqst, · 39 unmodified lines
126127 rc = cifs_calc_signature(rqst, server, what_we_think_sig_should_be);
127128 cifs_server_unlock(server);
128129
129− if (rc)
130− return rc;
131−
132−/* cifs_dump_mem("what we think it should be: ",
133− what_we_think_sig_should_be, 16); */
134−
135− if (crypto_memneq(server_response_sig, what_we_think_sig_should_be, 8))
136− return -EACCES;
137− else
138− return 0;
130+ if (!rc) {
131+ if (crypto_memneq(server_response_sig,
132+ what_we_think_sig_should_be, 8))
133+ rc = -EACCES;
134+ }
139135
136+ memzero_explicit(what_we_think_sig_should_be,
137+ sizeof(what_we_think_sig_should_be));
138+ return rc;
140139 }
fs/smb/client/smb1misc.c+2−1
@@ -80,7 +80,8 @@ is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv) · 79 unmodified lines
8080 (struct smb_com_transaction_change_notify_rsp *)buf;
8181 struct file_notify_information *pnotify;
8282 __u32 data_offset = 0;
83− size_t len = srv->total_read - srv->pdu_size;
83+ /* total_read excludes the RFC1002 preamble */
84+ size_t len = srv->total_read;
8485
8586 if (get_bcc(buf) > sizeof(struct file_notify_information)) {
8687 data_offset = le32_to_cpu(pSMBr->DataOffset);
fs/smb/client/smb1ops.c+8−2
@@ -542,6 +542,7 @@ static int cifs_query_path_info(const unsigned int xid, · 541 unmodified lines
542542
543543 data->reparse_point = false;
544544 data->adjust_tz = false;
545+ data->unknown_nlink = false;
545546
546547 /*
547548 * First try CIFSSMBQPathInfo() function which returns more info
@@ -608,6 +609,7 @@ static int cifs_query_path_info(const unsigned int xid, · 60 unmodified lines
608609 fi.EASize = di->EaSize;
609610 }
610611 fi.NumberOfLinks = cpu_to_le32(1);
612+ data->unknown_nlink = true;
611613 fi.DeletePending = 0;
612614 fi.Directory = !!(le32_to_cpu(fi.Attributes) & ATTR_DIRECTORY);
613615 cifs_buf_release(search_info.ntwrk_buf_start);
@@ -630,6 +632,8 @@ static int cifs_query_path_info(const unsigned int xid, · 16 unmodified lines
630632 rc = SMBQueryInformation(xid, tcon, full_path, &fi, cifs_sb->local_nls,
631633 cifs_remap(cifs_sb));
632634 data->adjust_tz = true;
635+ if (!rc)
636+ data->unknown_nlink = true;
633637 } else if ((rc == -EOPNOTSUPP || rc == -EINVAL) && non_unicode_wildcard) {
634638 /* Path with non-UNICODE wildcard character cannot exist. */
635639 rc = -ENOENT;
@@ -717,7 +721,7 @@ static int cifs_query_path_info(const unsigned int xid, · 81 unmodified lines
717721 ea->ea_value_length = cpu_to_le16(SMB2_WSL_XATTR_DEV_SIZE);
718722 memcpy(&ea->ea_data[0], SMB2_WSL_XATTR_DEV, SMB2_WSL_XATTR_NAME_LEN + 1);
719723 data->wsl.eas_len += ALIGN(sizeof(*ea) + SMB2_WSL_XATTR_NAME_LEN + 1 +
720− SMB2_WSL_XATTR_MODE_SIZE, 4);
724+ SMB2_WSL_XATTR_DEV_SIZE, 4);
721725 rc = 0;
722726 } else if (rc >= 0) {
723727 /* It is an error if EA $LXDEV has wrong size. */
@@ -893,8 +897,10 @@ static int cifs_open_file(const unsigned int xid, struct cifs_open_parms *oparms · 169 unmodified lines
893897 else
894898 rc = CIFS_open(xid, oparms, oplock, &fi);
895899
896− if (!rc && data)
900+ if (!rc && data) {
897901 move_cifs_info_to_smb2(&data->fi, &fi);
902+ data->unknown_nlink = true;
903+ }
898904
899905 return rc;
900906 }
fs/smb/client/smb1transport.c+31−4
@@ -375,12 +375,31 @@ coalesce_t2(char *second_buf, struct smb_hdr *target_hdr, unsigned int *pdu_len) · 374 unmodified lines
375375 data_area_of_tgt = (char *)&pSMBt->hdr.Protocol +
376376 get_unaligned_le16(&pSMBt->t2_rsp.DataOffset);
377377
378− /* validate target area */
379378 data_area_of_src = (char *)&pSMBs->hdr.Protocol +
380379 get_unaligned_le16(&pSMBs->t2_rsp.DataOffset);
381380
382381 data_area_of_tgt += total_in_tgt;
383382
383+ /*
384+ * DataOffset fields are server-supplied and not validated against
385+ * buffer bounds; check both data pointers before mutating the
386+ * target header.
387+ */
388+ if (data_area_of_tgt < (char *)target_hdr +
389+ sizeof(struct smb_t2_rsp) + sizeof(__le16) ||
390+ data_area_of_tgt + total_in_src >
391+ (char *)target_hdr + CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
392+ cifs_dbg(VFS, "%s: target data area out of bounds\n", __func__);
393+ return -EPROTO;
394+ }
395+ if (data_area_of_src < second_buf +
396+ sizeof(struct smb_t2_rsp) + sizeof(__le16) ||
397+ data_area_of_src + total_in_src >
398+ second_buf + smbCalcSize((struct smb_hdr *)second_buf)) {
399+ cifs_dbg(VFS, "%s: secondary data area out of bounds\n", __func__);
400+ return -EPROTO;
401+ }
402+
384403 total_in_tgt += total_in_src;
385404 /* is the result too big for the field? */
386405 if (total_in_tgt > USHRT_MAX) {
@@ -430,10 +449,18 @@ bool · 43 unmodified lines
430449 cifs_check_trans2(struct mid_q_entry *mid, struct TCP_Server_Info *server,
431450 char *buf, int malformed)
432451 {
433− if (malformed)
434− return false;
435− if (check2ndT2(buf) <= 0)
452+ if (malformed || check2ndT2(buf) <= 0) {
453+ /* mid->multiRsp blocks the server buf detach in handle_mid();
454+ * returning false here would leak resp_buf and leave a dangling
455+ * server->smallbuf/bigbuf after the user thread frees resp_buf.
456+ */
457+ if (mid->multiRsp) {
458+ mid->multiEnd = true;
459+ dequeue_mid(server, mid, true);
460+ return true;
461+ }
436462 return false;
463+ }
437464 mid->multiRsp = true;
438465 if (mid->resp_buf) {
439466 /* merge response - fix up 1st*/
fs/smb/client/smb2file.c+4−1
@@ -61,7 +61,10 @@ static struct smb2_symlink_err_rsp *symlink_data(const struct kvec *iov) · 60 unmodified lines
6161 cifs_dbg(FYI, "%s: skipping unhandled error context: 0x%x\n",
6262 __func__, le32_to_cpu(p->ErrorId));
6363
64− len = ALIGN(le32_to_cpu(p->ErrorDataLength), 8);
64+ len = le32_to_cpu(p->ErrorDataLength);
65+ if (len > end - ((u8 *)p + sizeof(*p)))
66+ return ERR_PTR(-EINVAL);
67+ len = ALIGN(len, 8);
6568 if (len > end - ((u8 *)p + sizeof(*p)))
6669 return ERR_PTR(-EINVAL);
6770
fs/smb/client/smb2inode.c+14−8
@@ -22,6 +22,7 @@ · 21 unmodified lines
2222 #include "smb2glob.h"
2323 #include "smb2proto.h"
2424 #include "cached_dir.h"
25+#include "reparse.h"
2526 #include "../common/smb2status.h"
2627 #include "../common/smbfsctl.h"
2728
@@ -40,9 +41,11 @@ static struct reparse_data_buffer *reparse_buf_ptr(struct kvec *iov) · 12 unmodified lines
4041
4142 buf = (struct reparse_data_buffer *)((u8 *)io + off);
4243 len = sizeof(*buf);
43− rdlen = le16_to_cpu(buf->ReparseDataLength);
44+ if (count < len)
45+ return ERR_PTR(smb_EIO2(smb_eio_trace_reparse_rdlen, count, 0));
4446
45− if (count < len || count < rdlen + len)
47+ rdlen = le16_to_cpu(buf->ReparseDataLength);
48+ if (count < rdlen + len)
4649 return ERR_PTR(smb_EIO2(smb_eio_trace_reparse_rdlen, count, rdlen));
4750 return buf;
4851 }
@@ -574,8 +577,10 @@ finished: · 525 unmodified lines
574577 idata->fi.Attributes = create_rsp->FileAttributes;
575578 idata->fi.AllocationSize = create_rsp->AllocationSize;
576579 idata->fi.EndOfFile = create_rsp->EndofFile;
580+ idata->contains_posix_file_info = false;
577581 if (le32_to_cpu(idata->fi.NumberOfLinks) == 0)
578582 idata->fi.NumberOfLinks = cpu_to_le32(1); /* dummy value */
583+ idata->unknown_nlink = true;
579584 idata->fi.DeletePending = 0; /* successful open = not delete pending */
580585 idata->fi.Directory = !!(le32_to_cpu(create_rsp->FileAttributes) & ATTR_DIRECTORY);
581586
@@ -596,7 +601,6 @@ finished: · 14 unmodified lines
596601 switch (cmds[i]) {
597602 case SMB2_OP_QUERY_INFO:
598603 idata = in_iov[i].iov_base;
599− idata->contains_posix_file_info = false;
600604 if (rc == 0 && cfile && cfile->symlink_target) {
601605 idata->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
602606 if (!idata->symlink_target)
@@ -609,6 +613,8 @@ finished: · 6 unmodified lines
609613 le16_to_cpu(qi_rsp->OutputBufferOffset),
610614 le32_to_cpu(qi_rsp->OutputBufferLength),
611615 &rsp_iov[i + 1], sizeof(idata->fi), (char *)&idata->fi);
616+ if (!rc)
617+ idata->contains_posix_file_info = false;
612618 }
613619 SMB2_query_info_free(&rqst[num_rqst++]);
614620 if (rc)
@@ -620,7 +626,6 @@ finished: · 5 unmodified lines
620626 break;
621627 case SMB2_OP_POSIX_QUERY_INFO:
622628 idata = in_iov[i].iov_base;
623− idata->contains_posix_file_info = true;
624629 if (rc == 0 && cfile && cfile->symlink_target) {
625630 idata->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
626631 if (!idata->symlink_target)
@@ -634,6 +639,8 @@ finished: · 7 unmodified lines
634639 le32_to_cpu(qi_rsp->OutputBufferLength),
635640 &rsp_iov[i + 1], sizeof(idata->posix_fi) /* add SIDs */,
636641 (char *)&idata->posix_fi);
642+ if (!rc)
643+ idata->contains_posix_file_info = true;
637644 }
638645 if (rc == 0)
639646 rc = parse_posix_sids(idata, &rsp_iov[i + 1]);
@@ -705,7 +712,6 @@ finished: · 65 unmodified lines
705712 idata = in_iov[i].iov_base;
706713 idata->reparse.io.iov = *iov;
707714 idata->reparse.io.buftype = resp_buftype[i + 1];
708− idata->contains_posix_file_info = false; /* BB VERIFY */
709715 rbuf = reparse_buf_ptr(iov);
710716 if (IS_ERR(rbuf)) {
711717 rc = PTR_ERR(rbuf);
@@ -727,7 +733,6 @@ finished: · 15 unmodified lines
727733 case SMB2_OP_QUERY_WSL_EA:
728734 if (!rc) {
729735 idata = in_iov[i].iov_base;
730− idata->contains_posix_file_info = false;
731736 qi_rsp = rsp_iov[i + 1].iov_base;
732737 data[0] = (u8 *)qi_rsp + le16_to_cpu(qi_rsp->OutputBufferOffset);
733738 size[0] = le32_to_cpu(qi_rsp->OutputBufferLength);
@@ -1000,12 +1005,13 @@ int smb2_query_path_info(const unsigned int xid, · 266 unmodified lines
10001005 /*
10011006 * If the symlink was already parsed in create response then it is needed to fix
10021007 * its type now (after the second call with OPEN_REPARSE_POINT which filled the
1003− * data->fi.Attributes). If the symlink was not parsed in create response then
1008+ * metadata attributes). If the symlink was not parsed in create response then
10041009 * the data->symlink_target was not filled yet and then the type will be fixed
10051010 * later after data->symlink_target is filled.
10061011 */
10071012 if (data->reparse.tag == IO_REPARSE_TAG_SYMLINK && !rc && data->symlink_target) {
1008− bool directory = le32_to_cpu(data->fi.Attributes) & ATTR_DIRECTORY;
1013+ bool directory = cifs_open_data_attrs(data) & ATTR_DIRECTORY;
1014+
10091015 rc = smb2_fix_symlink_target_type(&data->symlink_target, directory, cifs_sb);
10101016 }
10111017 break;
fs/smb/client/smb2ops.c+5−8
@@ -1569,7 +1569,7 @@ SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon, · 1568 unmodified lines
15691569 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
15701570
15711571 req_res_key_exit:
1572− kfree(res_key);
1572+ kfree_sensitive(res_key);
15731573 return rc;
15741574 }
15751575
@@ -2222,8 +2222,7 @@ smb2_duplicate_extents(const unsigned int xid, · 646 unmodified lines
22222222 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
22232223 if (rc)
22242224 goto duplicate_extents_out;
2225− netfs_resize_file(netfs_inode(inode), dest_off + len, true);
2226− cifs_setsize(inode, dest_off + len);
2225+ cifs_resize_file_locked(inode, dest_off + len);
22272226 }
22282227 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
22292228 trgtfile->fid.volatile_fid,
@@ -3776,8 +3775,7 @@ static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon, · 1546 unmodified lines
37763775 }
37773776
37783777 new_eof = off + len;
3779− netfs_resize_file(&cifsi->netfs, new_eof, true);
3780− cifs_setsize(inode, new_eof);
3778+ cifs_resize_file_locked(inode, new_eof);
37813779
37823780 qrc = SMB2_query_info(xid, tcon,
37833781 cfile->fid.persistent_fid,
@@ -3825,8 +3823,7 @@ static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon, · 41 unmodified lines
38253823 if (rc)
38263824 goto out;
38273825
3828− netfs_resize_file(&cifsi->netfs, new_eof, true);
3829− cifs_setsize(inode, new_eof);
3826+ cifs_resize_file_locked(inode, new_eof);
38303827
38313828 qrc = SMB2_query_info(xid, tcon,
38323829 cfile->fid.persistent_fid,
@@ -4636,7 +4633,7 @@ crypt_message(struct TCP_Server_Info *server, int num_rqst, · 803 unmodified lines
46364633 rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
46374634 else
46384635 rc = crypto_aead_setkey(tfm, key, SMB3_GCM128_CRYPTKEY_SIZE);
4639−
4636+ memzero_explicit(key, sizeof(key));
46404637 if (rc) {
46414638 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
46424639 return rc;
fs/smb/client/smb2pdu.c+14−4
@@ -3372,6 +3372,7 @@ replay_again: · 3371 unmodified lines
33723372 #endif /* CIFS_DEBUG2 */
33733373
33743374 if (file_info) {
3375+ buf->contains_posix_file_info = false;
33753376 file_info->CreationTime = rsp->CreationTime;
33763377 file_info->LastAccessTime = rsp->LastAccessTime;
33773378 file_info->LastWriteTime = rsp->LastWriteTime;
@@ -4564,8 +4565,10 @@ smb2_new_read_req(void **buf, unsigned int *total_len, · 1186 unmodified lines
45644565 if (rc)
45654566 return rc;
45664567
4567− if (server == NULL)
4568− return -ECONNABORTED;
4568+ if (!server) {
4569+ rc = -ECONNABORTED;
4570+ goto free_req;
4571+ }
45694572
45704573 shdr = &req->hdr;
45714574 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
@@ -4596,8 +4599,10 @@ smb2_new_read_req(void **buf, unsigned int *total_len, · 24 unmodified lines
45964599
45974600 rdata->mr = smbd_register_mr(server->smbd_conn, &rdata->subreq.io_iter,
45984601 true, need_invalidate);
4599− if (!rdata->mr)
4600− return -EAGAIN;
4602+ if (!rdata->mr) {
4603+ rc = -EAGAIN;
4604+ goto free_req;
4605+ }
46014606
46024607 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
46034608 if (need_invalidate)
@@ -4638,6 +4643,10 @@ smb2_new_read_req(void **buf, unsigned int *total_len, · 34 unmodified lines
46384643
46394644 *buf = req;
46404645 return rc;
4646+
4647+free_req:
4648+ cifs_small_buf_release(req);
4649+ return rc;
46414650 }
46424651
46434652 static void
@@ -4885,6 +4894,7 @@ out: · 241 unmodified lines
48854894 smb2_should_replay(tcon,
48864895 &rdata->retries,
48874896 &rdata->cur_sleep)) {
4897+ rdata->replay = true;
48884898 trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_retry_needed);
48894899 __set_bit(NETFS_SREQ_NEED_RETRY, &rdata->subreq.flags);
48904900 }
fs/smb/client/smb2transport.c+4−0
@@ -249,6 +249,8 @@ smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server) · 248 unmodified lines
249249 if (!rc)
250250 memcpy(shdr->Signature, smb2_signature, SMB2_SIGNATURE_SIZE);
251251
252+ memzero_explicit(key, sizeof(key));
253+ memzero_explicit(&hmac_ctx, sizeof(hmac_ctx));
252254 return rc;
253255 }
254256
@@ -283,6 +285,7 @@ static void generate_key(struct cifs_ses *ses, struct kvec label, · 28 unmodified lines
283285 hmac_sha256_final(&hmac_ctx, prfhash);
284286
285287 memcpy(key, prfhash, key_size);
288+ memzero_explicit(prfhash, sizeof(prfhash));
286289 }
287290
288291 struct derivation {
@@ -482,6 +485,7 @@ smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server) · 193 unmodified lines
482485 memset(shdr->Signature, 0x0, SMB2_SIGNATURE_SIZE);
483486
484487 rc = aes_cmac_preparekey(&cmac_key, key, SMB2_CMACAES_SIZE);
488+ memzero_explicit(key, sizeof(key));
485489 if (rc) {
486490 cifs_server_dbg(VFS, "%s: Could not set key for cmac aes\n", __func__);
487491 return rc;
fs/smb/client/trace.h+2−1
@@ -133,6 +133,7 @@ · 132 unmodified lines
133133 EM(smb_eio_trace_sym_slash, "sym_slash") \
134134 EM(smb_eio_trace_sym_target_len, "sym_target_len") \
135135 EM(smb_eio_trace_symlink_file_size, "symlink_file_size") \
136+ EM(smb_eio_trace_tcon_bcc_too_small, "tcon_bcc_too_small") \
136137 EM(smb_eio_trace_tdis_in_reconnect, "tdis_in_reconnect") \
137138 EM(smb_eio_trace_tx_chained_async, "tx_chained_async") \
138139 EM(smb_eio_trace_tx_compress_failed, "tx_compress_failed") \
@@ -213,7 +214,7 @@ · 74 unmodified lines
213214 #define EM(a, b) a,
214215 #define E_(a, b) a
215216
216−enum smb_eio_trace { smb_eio_traces } __mode(byte);
217+enum smb_eio_trace { smb_eio_traces };
217218 enum smb3_rw_credits_trace { smb3_rw_credits_traces } __mode(byte);
218219 enum smb3_tcon_ref_trace { smb3_tcon_ref_traces } __mode(byte);
219220

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t