Skip to content

Commit 76fd1e8

Browse files
Gerrit RenkerArnaldo Carvalho de Melo
authored andcommitted
[DCCP]: Unaligned pointer access
This fixes `unaligned (read) access' errors of the type Kernel unaligned access at TPC[100f970c] dccp_parse_options+0x4f4/0x7e0 [dccp] Kernel unaligned access at TPC[1011f2e4] ccid3_hc_tx_parse_options+0x1ac/0x380 [dccp_ccid3] Kernel unaligned access at TPC[100f9898] dccp_parse_options+0x680/0x880 [dccp] by using the get_unaligned macro for parsing options. Commiter note: Preserved the sparse __be{16,32} annotations. Signed-off-by: Gerrit Renker <gerrit@erg.abdn.ac.uk> Signed-off-by: Ian McDonald <ian.mcdonald@jandi.co.nz> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent d8ef2c2 commit 76fd1e8

2 files changed

Lines changed: 29 additions & 15 deletions

File tree

‎net/dccp/ccids/ccid3.c‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
#include "lib/tfrc.h"
4141
#include "ccid3.h"
4242

43+
#include <asm/unaligned.h>
44+
4345
#ifdef CONFIG_IP_DCCP_CCID3_DEBUG
4446
static int ccid3_debug;
4547
#define ccid3_pr_debug(format, a...) DCCP_PR_DEBUG(ccid3_debug, format, ##a)
@@ -544,6 +546,7 @@ static int ccid3_hc_tx_parse_options(struct sock *sk, unsigned char option,
544546
const struct dccp_sock *dp = dccp_sk(sk);
545547
struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
546548
struct ccid3_options_received *opt_recv;
549+
__be32 opt_val;
547550

548551
opt_recv = &hctx->ccid3hctx_options_received;
549552

@@ -563,8 +566,8 @@ static int ccid3_hc_tx_parse_options(struct sock *sk, unsigned char option,
563566
dccp_role(sk), sk, len);
564567
rc = -EINVAL;
565568
} else {
566-
opt_recv->ccid3or_loss_event_rate =
567-
ntohl(*(__be32 *)value);
569+
opt_val = get_unaligned((__be32 *)value);
570+
opt_recv->ccid3or_loss_event_rate = ntohl(opt_val);
568571
ccid3_pr_debug("%s(%p), LOSS_EVENT_RATE=%u\n",
569572
dccp_role(sk), sk,
570573
opt_recv->ccid3or_loss_event_rate);
@@ -585,8 +588,8 @@ static int ccid3_hc_tx_parse_options(struct sock *sk, unsigned char option,
585588
dccp_role(sk), sk, len);
586589
rc = -EINVAL;
587590
} else {
588-
opt_recv->ccid3or_receive_rate =
589-
ntohl(*(__be32 *)value);
591+
opt_val = get_unaligned((__be32 *)value);
592+
opt_recv->ccid3or_receive_rate = ntohl(opt_val);
590593
ccid3_pr_debug("%s(%p), RECEIVE_RATE=%u\n",
591594
dccp_role(sk), sk,
592595
opt_recv->ccid3or_receive_rate);

‎net/dccp/options.c‎

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/dccp.h>
1515
#include <linux/module.h>
1616
#include <linux/types.h>
17+
#include <asm/unaligned.h>
1718
#include <linux/kernel.h>
1819
#include <linux/skbuff.h>
1920

@@ -59,6 +60,7 @@ int dccp_parse_options(struct sock *sk, struct sk_buff *skb)
5960
unsigned char opt, len;
6061
unsigned char *value;
6162
u32 elapsed_time;
63+
__be32 opt_val;
6264
int rc;
6365
int mandatory = 0;
6466

@@ -145,7 +147,8 @@ int dccp_parse_options(struct sock *sk, struct sk_buff *skb)
145147
if (len != 4)
146148
goto out_invalid_option;
147149

148-
opt_recv->dccpor_timestamp = ntohl(*(__be32 *)value);
150+
opt_val = get_unaligned((__be32 *)value);
151+
opt_recv->dccpor_timestamp = ntohl(opt_val);
149152

150153
dp->dccps_timestamp_echo = opt_recv->dccpor_timestamp;
151154
dp->dccps_timestamp_time = ktime_get_real();
@@ -159,7 +162,8 @@ int dccp_parse_options(struct sock *sk, struct sk_buff *skb)
159162
if (len != 4 && len != 6 && len != 8)
160163
goto out_invalid_option;
161164

162-
opt_recv->dccpor_timestamp_echo = ntohl(*(__be32 *)value);
165+
opt_val = get_unaligned((__be32 *)value);
166+
opt_recv->dccpor_timestamp_echo = ntohl(opt_val);
163167

164168
dccp_pr_debug("%s rx opt: TIMESTAMP_ECHO=%u, len=%d, "
165169
"ackno=%llu", dccp_role(sk),
@@ -168,16 +172,20 @@ int dccp_parse_options(struct sock *sk, struct sk_buff *skb)
168172
(unsigned long long)
169173
DCCP_SKB_CB(skb)->dccpd_ack_seq);
170174

175+
value += 4;
171176

172-
if (len == 4) {
177+
if (len == 4) { /* no elapsed time included */
173178
dccp_pr_debug_cat("\n");
174179
break;
175180
}
176181

177-
if (len == 6)
178-
elapsed_time = ntohs(*(__be16 *)(value + 4));
179-
else
180-
elapsed_time = ntohl(*(__be32 *)(value + 4));
182+
if (len == 6) { /* 2-byte elapsed time */
183+
__be16 opt_val2 = get_unaligned((__be16 *)value);
184+
elapsed_time = ntohs(opt_val2);
185+
} else { /* 4-byte elapsed time */
186+
opt_val = get_unaligned((__be32 *)value);
187+
elapsed_time = ntohl(opt_val);
188+
}
181189

182190
dccp_pr_debug_cat(", ELAPSED_TIME=%u\n", elapsed_time);
183191

@@ -192,10 +200,13 @@ int dccp_parse_options(struct sock *sk, struct sk_buff *skb)
192200
if (pkt_type == DCCP_PKT_DATA)
193201
continue;
194202

195-
if (len == 2)
196-
elapsed_time = ntohs(*(__be16 *)value);
197-
else
198-
elapsed_time = ntohl(*(__be32 *)value);
203+
if (len == 2) {
204+
__be16 opt_val2 = get_unaligned((__be16 *)value);
205+
elapsed_time = ntohs(opt_val2);
206+
} else {
207+
opt_val = get_unaligned((__be32 *)value);
208+
elapsed_time = ntohl(opt_val);
209+
}
199210

200211
if (elapsed_time > opt_recv->dccpor_elapsed_time)
201212
opt_recv->dccpor_elapsed_time = elapsed_time;

0 commit comments

Comments
 (0)