Skip to contentSkip to conversation

smb: client: don't use read data that failed signature verification

PATCH In review Suyuan Wang 2 participants 2 messages Nov 19

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 smb: client: don't use read data that failed signature verification

From: Suyuan Wang <[email protected]>

When smb2_readv_callback() finds a bad signature, it sets the error in rdata->result but leaves rdata->got_bytes alone. Further down, a short read that reaches the remote EOF clears the error again:

    size_t trans = rdata->subreq.transferred + rdata->got_bytes;
    if (trans < rdata->subreq.len &&
        rdata->subreq.start + trans >= netfs_read_remote_i_size(inode)) {
            rdata->result = 0;

and got_bytes is then added to subreq.transferred. So the last part of a file, or all of a small one, is returned even though its signature is wrong.

Clearing got_bytes is not enough on its own. If the read starts at or past the cached remote size, for example because the file has grown on the server since the size was last fetched, the check above still matches and the error turns into EOF, or into a zero-filled tail when NETFS_SREQ_CLEAR_TAIL is set.

Clear got_bytes when the signature check fails, as the retry path in the same function already does, and only treat a short read as hitting the remote EOF when the read itself succeeded. That is what commit 1da29f2c39b6 ("netfs, cifs: Fix handling of short DIO read") describes:
"clear the error if a short read occurred without error and the read-to file position is now at the remote inode size".

Found with a static analysis tool and checked by hand.

Fixes 1da29f2c39b6 ("netfs, cifs: Fix handling of short DIO read")

---
v3:
- Only this patch is resent; 1/2 is in cifs-next.
- Also skip the remote-EOF check when the read has already failed (from sashiko's review). Clearing got_bytes alone still let a failed read that starts at or past the cached remote size come back as EOF.

Tested in QEMU against cifs-next, signed SMB 3.1.1 to Samba through a proxy that changes READ data without re-signing it. A cache=none file is opened (21 bytes), 23 bytes are appended through a second mount, and the file is read from offset 21:
v2: pread returns 0 (EOF), the signature error is lost
v3: pread returns -EACCES
Without the proxy both versions read the 23 new bytes, and a read past the real EOF returns 0.

One side effect: a short read that ends with a retryable error at the remote EOF is now retried instead of being reported as EOF. I haven't exercised that path.

v2: https://lore.kernel.org/linux-cifs/[email protected]/

---
 fs/smb/client/smb2pdu.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/smb/client/smb2pdu.c +3 -1
index b26bfb4bd91a..f7cb08f5e34f 100644
@@ -4819,6 +4819,8 @@ smb2_readv_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
 					      rc);
 				rdata->subreq.error = rc;
 				rdata->result = rc;
+				/* the data can't be trusted, don't count it */
+				rdata->got_bytes = 0;
 
 				if (is_replayable_error(rc)) {
 					trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_retry_needed);
@@ -4898,7 +4900,7 @@ smb2_readv_callback(struct TCP_Server_Info *server, struct mid_q_entry *mid)
 			__set_bit(NETFS_SREQ_CLEAR_TAIL, &rdata->subreq.flags);
 	} else {
 		size_t trans = rdata->subreq.transferred + rdata->got_bytes;
-		if (trans < rdata->subreq.len &&
+		if (!rdata->result && trans < rdata->subreq.len &&
 		    rdata->subreq.start + trans >= netfs_read_remote_i_size(inode)) {
 			rdata->result = 0;
 			if (rdata->subreq.start + trans >= i_size_read(inode))
maintainer
#1 lore raw

Suyuan Wang via B4 Relay <[email protected]>

Quoted 21 lines · writes:

From: Suyuan Wang <[email protected]>

When smb2_readv_callback() finds a bad signature, it sets the error in rdata->result but leaves rdata->got_bytes alone. Further down, a short read that reaches the remote EOF clears the error again:

    size_t trans = rdata->subreq.transferred + rdata->got_bytes;
    if (trans < rdata->subreq.len &&
        rdata->subreq.start + trans >= netfs_read_remote_i_size(inode)) {
            rdata->result = 0;

and got_bytes is then added to subreq.transferred. So the last part of a file, or all of a small one, is returned even though its signature is wrong.

Clearing got_bytes is not enough on its own. If the read starts at or past the cached remote size, for example because the file has grown on the server since the size was last fetched, the check above still matches and the error turns into EOF, or into a zero-filled tail when NETFS_SREQ_CLEAR_TAIL is set.

Quoted 7 lines

Clear got_bytes when the signature check fails, as the retry path in the same function already does, and only treat a short read as hitting the remote EOF when the read itself succeeded. That is what commit 1da29f2c39b6 ("netfs, cifs: Fix handling of short DIO read") describes:
"clear the error if a short read occurred without error and the read-to file position is now at the remote inode size".
...

Applied with the redudant 'rdata->result = 0' removed.