Skip to content

Commit 93fa3b7

Browse files
aescolardanieldegrasse
authored andcommitted
net: tcp: Avoid directly accessing address of unaligned struct
Use UNALIGNED_MEMBER_ADDR when getting the address of possibly unaligned structures members instead of attempting to directly get the address as an offset. Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
1 parent 34fece8 commit 93fa3b7

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

subsys/net/ip/tcp.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,20 +1262,20 @@ static int tcp_header_add(struct tcp *conn, struct net_pkt *pkt, uint8_t flags,
12621262

12631263
memset(th, 0, sizeof(struct tcphdr));
12641264

1265-
UNALIGNED_PUT(conn->src.sin.sin_port, &th->th_sport);
1266-
UNALIGNED_PUT(conn->dst.sin.sin_port, &th->th_dport);
1265+
UNALIGNED_PUT(conn->src.sin.sin_port, UNALIGNED_MEMBER_ADDR(th, th_sport));
1266+
UNALIGNED_PUT(conn->dst.sin.sin_port, UNALIGNED_MEMBER_ADDR(th, th_dport));
12671267
th->th_off = 5;
12681268

12691269
if (conn->send_options.mss_found) {
12701270
th->th_off++;
12711271
}
12721272

12731273
UNALIGNED_PUT(flags, &th->th_flags);
1274-
UNALIGNED_PUT(htons(conn->recv_win), &th->th_win);
1275-
UNALIGNED_PUT(htonl(seq), &th->th_seq);
1274+
UNALIGNED_PUT(htons(conn->recv_win), UNALIGNED_MEMBER_ADDR(th, th_win));
1275+
UNALIGNED_PUT(htonl(seq), UNALIGNED_MEMBER_ADDR(th, th_seq));
12761276

12771277
if (ACK & flags) {
1278-
UNALIGNED_PUT(htonl(conn->ack), &th->th_ack);
1278+
UNALIGNED_PUT(htonl(conn->ack), UNALIGNED_MEMBER_ADDR(th, th_ack));
12791279
}
12801280

12811281
return net_pkt_set_data(pkt, &tcp_access);
@@ -1415,13 +1415,13 @@ void net_tcp_reply_rst(struct net_pkt *pkt)
14151415

14161416
memset(th_rst, 0, sizeof(struct tcphdr));
14171417

1418-
UNALIGNED_PUT(th_pkt->th_dport, &th_rst->th_sport);
1419-
UNALIGNED_PUT(th_pkt->th_sport, &th_rst->th_dport);
1418+
UNALIGNED_PUT(th_pkt->th_dport, UNALIGNED_MEMBER_ADDR(th_rst, th_sport));
1419+
UNALIGNED_PUT(th_pkt->th_sport, UNALIGNED_MEMBER_ADDR(th_rst, th_dport));
14201420
th_rst->th_off = 5;
14211421

14221422
if (th_flags(th_pkt) & ACK) {
14231423
UNALIGNED_PUT(RST, &th_rst->th_flags);
1424-
UNALIGNED_PUT(th_pkt->th_ack, &th_rst->th_seq);
1424+
UNALIGNED_PUT(th_pkt->th_ack, UNALIGNED_MEMBER_ADDR(th_rst, th_seq));
14251425
} else {
14261426
uint32_t ack = ntohl(th_pkt->th_seq) + tcp_data_len(pkt);
14271427

@@ -1430,7 +1430,7 @@ void net_tcp_reply_rst(struct net_pkt *pkt)
14301430
}
14311431

14321432
UNALIGNED_PUT(RST | ACK, &th_rst->th_flags);
1433-
UNALIGNED_PUT(htonl(ack), &th_rst->th_ack);
1433+
UNALIGNED_PUT(htonl(ack), UNALIGNED_MEMBER_ADDR(th_rst, th_ack));
14341434
}
14351435

14361436
ret = net_pkt_set_data(rst, &tcp_access_rst);

subsys/net/ip/tcp_private.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@
66
*/
77

88
#include "tp.h"
9+
#include <zephyr/toolchain/gcc.h>
910

1011
#define is(_a, _b) (strcmp((_a), (_b)) == 0)
1112

1213
#ifndef MIN3
1314
#define MIN3(_a, _b, _c) MIN((_a), MIN((_b), (_c)))
1415
#endif
1516

16-
#define th_sport(_x) UNALIGNED_GET(&(_x)->th_sport)
17-
#define th_dport(_x) UNALIGNED_GET(&(_x)->th_dport)
18-
#define th_seq(_x) ntohl(UNALIGNED_GET(&(_x)->th_seq))
19-
#define th_ack(_x) ntohl(UNALIGNED_GET(&(_x)->th_ack))
17+
#define th_sport(_x) UNALIGNED_GET(UNALIGNED_MEMBER_ADDR((_x), th_sport))
18+
#define th_dport(_x) UNALIGNED_GET(UNALIGNED_MEMBER_ADDR((_x), th_dport))
19+
#define th_seq(_x) ntohl(UNALIGNED_GET(UNALIGNED_MEMBER_ADDR((_x), th_seq)))
20+
#define th_ack(_x) ntohl(UNALIGNED_GET(UNALIGNED_MEMBER_ADDR((_x), th_ack)))
21+
2022
#define th_off(_x) ((_x)->th_off)
21-
#define th_flags(_x) UNALIGNED_GET(&(_x)->th_flags)
22-
#define th_win(_x) UNALIGNED_GET(&(_x)->th_win)
23+
#define th_flags(_x) UNALIGNED_GET(UNALIGNED_MEMBER_ADDR((_x), th_flags))
24+
#define th_win(_x) UNALIGNED_GET(UNALIGNED_MEMBER_ADDR((_x), th_win))
2325

2426
#define tcp_slist(_conn, _slist, _op, _type, _link) \
2527
({ \

0 commit comments

Comments
 (0)