Skip to content

Commit 0015eb6

Browse files
dmantipovSteve French
authored and
Steve French
committed
smb: client, common: fix fortify warnings
When compiling with gcc version 14.0.0 20231126 (experimental) and CONFIG_FORTIFY_SOURCE=y, I've noticed the following: In file included from ./include/linux/string.h:295, from ./include/linux/bitmap.h:12, from ./include/linux/cpumask.h:12, from ./arch/x86/include/asm/paravirt.h:17, from ./arch/x86/include/asm/cpuid.h:62, from ./arch/x86/include/asm/processor.h:19, from ./arch/x86/include/asm/cpufeature.h:5, from ./arch/x86/include/asm/thread_info.h:53, from ./include/linux/thread_info.h:60, from ./arch/x86/include/asm/preempt.h:9, from ./include/linux/preempt.h:79, from ./include/linux/spinlock.h:56, from ./include/linux/wait.h:9, from ./include/linux/wait_bit.h:8, from ./include/linux/fs.h:6, from fs/smb/client/smb2pdu.c:18: In function 'fortify_memcpy_chk', inlined from '__SMB2_close' at fs/smb/client/smb2pdu.c:3480:4: ./include/linux/fortify-string.h:588:25: warning: call to '__read_overflow2_field' declared with attribute warning: detected read beyond size of field (2nd parameter); maybe use struct_group()? [-Wattribute-warning] 588 | __read_overflow2_field(q_size_field, size); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ and: In file included from ./include/linux/string.h:295, from ./include/linux/bitmap.h:12, from ./include/linux/cpumask.h:12, from ./arch/x86/include/asm/paravirt.h:17, from ./arch/x86/include/asm/cpuid.h:62, from ./arch/x86/include/asm/processor.h:19, from ./arch/x86/include/asm/cpufeature.h:5, from ./arch/x86/include/asm/thread_info.h:53, from ./include/linux/thread_info.h:60, from ./arch/x86/include/asm/preempt.h:9, from ./include/linux/preempt.h:79, from ./include/linux/spinlock.h:56, from ./include/linux/wait.h:9, from ./include/linux/wait_bit.h:8, from ./include/linux/fs.h:6, from fs/smb/client/cifssmb.c:17: In function 'fortify_memcpy_chk', inlined from 'CIFS_open' at fs/smb/client/cifssmb.c:1248:3: ./include/linux/fortify-string.h:588:25: warning: call to '__read_overflow2_field' declared with attribute warning: detected read beyond size of field (2nd parameter); maybe use struct_group()? [-Wattribute-warning] 588 | __read_overflow2_field(q_size_field, size); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In both cases, the fortification logic inteprets calls to 'memcpy()' as an attempts to copy an amount of data which exceeds the size of the specified field (i.e. more than 8 bytes from __le64 value) and thus issues an overread warning. Both of these warnings may be silenced by using the convenient 'struct_group()' quirk. Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru> Acked-by: Namjae Jeon <linkinjeon@kernel.org> Signed-off-by: Steve French <stfrench@microsoft.com>
1 parent 8801015 commit 0015eb6

File tree

5 files changed

+40
-31
lines changed

5 files changed

+40
-31
lines changed

fs/smb/client/cifspdu.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -882,11 +882,13 @@ typedef struct smb_com_open_rsp {
882882
__u8 OplockLevel;
883883
__u16 Fid;
884884
__le32 CreateAction;
885-
__le64 CreationTime;
886-
__le64 LastAccessTime;
887-
__le64 LastWriteTime;
888-
__le64 ChangeTime;
889-
__le32 FileAttributes;
885+
struct_group(common_attributes,
886+
__le64 CreationTime;
887+
__le64 LastAccessTime;
888+
__le64 LastWriteTime;
889+
__le64 ChangeTime;
890+
__le32 FileAttributes;
891+
);
890892
__le64 AllocationSize;
891893
__le64 EndOfFile;
892894
__le16 FileType;
@@ -2264,11 +2266,13 @@ typedef struct {
22642266
/* QueryFileInfo/QueryPathinfo (also for SetPath/SetFile) data buffer formats */
22652267
/******************************************************************************/
22662268
typedef struct { /* data block encoding of response to level 263 QPathInfo */
2267-
__le64 CreationTime;
2268-
__le64 LastAccessTime;
2269-
__le64 LastWriteTime;
2270-
__le64 ChangeTime;
2271-
__le32 Attributes;
2269+
struct_group(common_attributes,
2270+
__le64 CreationTime;
2271+
__le64 LastAccessTime;
2272+
__le64 LastWriteTime;
2273+
__le64 ChangeTime;
2274+
__le32 Attributes;
2275+
);
22722276
__u32 Pad1;
22732277
__le64 AllocationSize;
22742278
__le64 EndOfFile; /* size ie offset to first free byte in file */

fs/smb/client/cifssmb.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,8 +1244,10 @@ CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock,
12441244
*oplock |= CIFS_CREATE_ACTION;
12451245

12461246
if (buf) {
1247-
/* copy from CreationTime to Attributes */
1248-
memcpy((char *)buf, (char *)&rsp->CreationTime, 36);
1247+
/* copy commonly used attributes */
1248+
memcpy(&buf->common_attributes,
1249+
&rsp->common_attributes,
1250+
sizeof(buf->common_attributes));
12491251
/* the file_info buf is endian converted by caller */
12501252
buf->AllocationSize = rsp->AllocationSize;
12511253
buf->EndOfFile = rsp->EndOfFile;

fs/smb/client/smb2pdu.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3472,12 +3472,10 @@ __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
34723472
} else {
34733473
trace_smb3_close_done(xid, persistent_fid, tcon->tid,
34743474
ses->Suid);
3475-
/*
3476-
* Note that have to subtract 4 since struct network_open_info
3477-
* has a final 4 byte pad that close response does not have
3478-
*/
34793475
if (pbuf)
3480-
memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4);
3476+
memcpy(&pbuf->network_open_info,
3477+
&rsp->network_open_info,
3478+
sizeof(pbuf->network_open_info));
34813479
}
34823480

34833481
atomic_dec(&tcon->num_remote_opens);

fs/smb/client/smb2pdu.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,15 @@ struct smb2_file_reparse_point_info {
319319
} __packed;
320320

321321
struct smb2_file_network_open_info {
322-
__le64 CreationTime;
323-
__le64 LastAccessTime;
324-
__le64 LastWriteTime;
325-
__le64 ChangeTime;
326-
__le64 AllocationSize;
327-
__le64 EndOfFile;
328-
__le32 Attributes;
322+
struct_group(network_open_info,
323+
__le64 CreationTime;
324+
__le64 LastAccessTime;
325+
__le64 LastWriteTime;
326+
__le64 ChangeTime;
327+
__le64 AllocationSize;
328+
__le64 EndOfFile;
329+
__le32 Attributes;
330+
);
329331
__le32 Reserved;
330332
} __packed; /* level 34 Query also similar returned in close rsp and open rsp */
331333

fs/smb/common/smb2pdu.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -702,13 +702,16 @@ struct smb2_close_rsp {
702702
__le16 StructureSize; /* 60 */
703703
__le16 Flags;
704704
__le32 Reserved;
705-
__le64 CreationTime;
706-
__le64 LastAccessTime;
707-
__le64 LastWriteTime;
708-
__le64 ChangeTime;
709-
__le64 AllocationSize; /* Beginning of FILE_STANDARD_INFO equivalent */
710-
__le64 EndOfFile;
711-
__le32 Attributes;
705+
struct_group(network_open_info,
706+
__le64 CreationTime;
707+
__le64 LastAccessTime;
708+
__le64 LastWriteTime;
709+
__le64 ChangeTime;
710+
/* Beginning of FILE_STANDARD_INFO equivalent */
711+
__le64 AllocationSize;
712+
__le64 EndOfFile;
713+
__le32 Attributes;
714+
);
712715
} __packed;
713716

714717

0 commit comments

Comments
 (0)