]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
net: use sock_kmemdup for ip_options
authorGeliang Tang <tanggeliang@kylinos.cn>
Fri, 28 Feb 2025 10:01:32 +0000 (18:01 +0800)
committerJakub Kicinski <kuba@kernel.org>
Tue, 4 Mar 2025 01:16:34 +0000 (17:16 -0800)
Instead of using sock_kmalloc() to allocate an ip_options and then
immediately duplicate another ip_options to the newly allocated one in
ipv6_dup_options(), mptcp_copy_ip_options() and sctp_v4_copy_ip_options(),
the newly added sock_kmemdup() helper can be used to simplify the code.

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Acked-by: Matthieu Baerts (NGI0) <matttbe@kernel.org>
Link: https://patch.msgid.link/91ae749d66600ec6fb679e0e518fda6acb5c3e6f.1740735165.git.tanggeliang@kylinos.cn
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/ipv6/exthdrs.c
net/mptcp/protocol.c
net/sctp/protocol.c

index 6789623b2b0d1db4d8c13dc505cb362c456c5160..457de0745a338ea0cb542694b84140a7d25d467d 100644 (file)
@@ -1204,10 +1204,9 @@ ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt)
 {
        struct ipv6_txoptions *opt2;
 
-       opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC);
+       opt2 = sock_kmemdup(sk, opt, opt->tot_len, GFP_ATOMIC);
        if (opt2) {
                long dif = (char *)opt2 - (char *)opt;
-               memcpy(opt2, opt, opt->tot_len);
                if (opt2->hopopt)
                        *((char **)&opt2->hopopt) += dif;
                if (opt2->dst0opt)
index 6b61b7dee33be10294ae1101f9206144878a3192..ec23e65ef0f11928c35e3c992b8f750266b504f8 100644 (file)
@@ -3178,12 +3178,9 @@ static void mptcp_copy_ip_options(struct sock *newsk, const struct sock *sk)
        rcu_read_lock();
        inet_opt = rcu_dereference(inet->inet_opt);
        if (inet_opt) {
-               newopt = sock_kmalloc(newsk, sizeof(*inet_opt) +
+               newopt = sock_kmemdup(newsk, inet_opt, sizeof(*inet_opt) +
                                      inet_opt->opt.optlen, GFP_ATOMIC);
-               if (newopt)
-                       memcpy(newopt, inet_opt, sizeof(*inet_opt) +
-                              inet_opt->opt.optlen);
-               else
+               if (!newopt)
                        net_warn_ratelimited("%s: Failed to copy ip options\n", __func__);
        }
        RCU_INIT_POINTER(newinet->inet_opt, newopt);
index 29727ed1008ed385060277d6f27f42f6df3329be..5407a3922101de59c9f0303db3ab119918dedbb7 100644 (file)
@@ -185,12 +185,9 @@ static void sctp_v4_copy_ip_options(struct sock *sk, struct sock *newsk)
        rcu_read_lock();
        inet_opt = rcu_dereference(inet->inet_opt);
        if (inet_opt) {
-               newopt = sock_kmalloc(newsk, sizeof(*inet_opt) +
+               newopt = sock_kmemdup(newsk, inet_opt, sizeof(*inet_opt) +
                                      inet_opt->opt.optlen, GFP_ATOMIC);
-               if (newopt)
-                       memcpy(newopt, inet_opt, sizeof(*inet_opt) +
-                              inet_opt->opt.optlen);
-               else
+               if (!newopt)
                        pr_err("%s: Failed to copy ip options\n", __func__);
        }
        RCU_INIT_POINTER(newinet->inet_opt, newopt);