From patchwork Sat Sep 2 16:52:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shigeru Yoshida X-Patchwork-Id: 13373129 X-Patchwork-Delegate: kuba@kernel.org Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C6F0E3C1E for ; Sat, 2 Sep 2023 16:52:30 +0000 (UTC) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2922E42 for ; Sat, 2 Sep 2023 09:52:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693673547; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=tZnuggjXiPxxrN0LJKuKy2l1BGtNDFo8WO5oMG65CzU=; b=GQsbTVyB0iGLohSVJRmqCbVNttqO9sQAAfMna8hiY3Ocw8A2tux/y/hAeCZ/QUZFIJR/7n obPWala1CtD80pj9Ubk3IpRyzbQ6QkVtnr/IyxngKTjWQQ16T+hLfpuC7IJHOhfW0TR0Tq R2v5CjI1qbynwvDG58Us5yDk4Y3t1P4= Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-629-NYyVYgm4PSiDuASt1TVtGA-1; Sat, 02 Sep 2023 12:52:26 -0400 X-MC-Unique: NYyVYgm4PSiDuASt1TVtGA-1 Received: by mail-pg1-f198.google.com with SMTP id 41be03b00d2f7-56f75ec7ca9so56134a12.3 for ; Sat, 02 Sep 2023 09:52:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693673545; x=1694278345; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=tZnuggjXiPxxrN0LJKuKy2l1BGtNDFo8WO5oMG65CzU=; b=OAW91aSUyCYY4KT1ZEi1HWP4m0bUzC5weGFLsRN/EdW7yPlvJkxiOEcls8N46i6zco LxEdTsVsAhdluTAdj9/TKQla1WFRN7iA0vyhShzjRcxr2/S8vmz696+ok+zyhVyLOY+c Sf9z0blOCVGhPlQWpbqW1ouNk17NerE4RuZXAOPuTPpUvdhZOdWP7Jzwpw5peoYj3hpT igMVD7ansOl1VkWEXrt7z7QjpzJw20/UO0EAndhVBTO1+1X7eurZs5jUqywKtQIAsDEW ZjtYvbVqddW3HsTdj6BammIHkraTGJgM5WYNrajlU2E+t9ztTxui1PcOF/PSk554PgJQ czaQ== X-Gm-Message-State: AOJu0YxyLamqRuZ/nF0jO1YipJuaT6CJndn1eM5NbTDjhIj7mvaqmWMy GfVSKyjr6lCT8GeFt+AYeVfzzQfYzUPEe83cT8kU7+LehVO+cEu1AaEXWIitsNBunU38ECvX4uj 3otMeKSEmziCQb8eM X-Received: by 2002:a05:6a20:728e:b0:149:7fea:d88e with SMTP id o14-20020a056a20728e00b001497fead88emr7866468pzk.24.1693673544982; Sat, 02 Sep 2023 09:52:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFAAbHUxNQwq8p+UPawwfWP3/BHApDvAuwx7oX4BXx18Uo4sD3lzf4FxsnqstM/GwwZl3J+Hg== X-Received: by 2002:a05:6a20:728e:b0:149:7fea:d88e with SMTP id o14-20020a056a20728e00b001497fead88emr7866454pzk.24.1693673544724; Sat, 02 Sep 2023 09:52:24 -0700 (PDT) Received: from kernel-devel.local ([240d:1a:c0d:9f00:245e:16ff:fe87:c960]) by smtp.gmail.com with ESMTPSA id t29-20020a63955d000000b0056a36ac322dsm4828323pgn.4.2023.09.02.09.52.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Sep 2023 09:52:24 -0700 (PDT) From: Shigeru Yoshida To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Shigeru Yoshida , syzbot+6f98de741f7dbbfc4ccb@syzkaller.appspotmail.com Subject: [PATCH net] kcm: Fix memory leak in error path of kcm_sendmsg() Date: Sun, 3 Sep 2023 01:52:16 +0900 Message-ID: <20230902165216.1721082-1-syoshida@redhat.com> X-Mailer: git-send-email 2.41.0 Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: kuba@kernel.org syzbot reported a memory leak like below [1]: BUG: memory leak unreferenced object 0xffff88810b088c00 (size 240): comm "syz-executor186", pid 5012, jiffies 4294943306 (age 13.680s) hex dump (first 32 bytes): 00 89 08 0b 81 88 ff ff 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ backtrace: [] __alloc_skb+0x1ef/0x230 net/core/skbuff.c:634 [] alloc_skb include/linux/skbuff.h:1289 [inline] [] kcm_sendmsg+0x269/0x1050 net/kcm/kcmsock.c:815 [] sock_sendmsg_nosec net/socket.c:725 [inline] [] sock_sendmsg+0x56/0xb0 net/socket.c:748 [] ____sys_sendmsg+0x365/0x470 net/socket.c:2494 [] ___sys_sendmsg+0xc9/0x130 net/socket.c:2548 [] __sys_sendmsg+0xa6/0x120 net/socket.c:2577 [] do_syscall_x64 arch/x86/entry/common.c:50 [inline] [] do_syscall_64+0x38/0xb0 arch/x86/entry/common.c:80 [] entry_SYSCALL_64_after_hwframe+0x63/0xcd In kcm_sendmsg(), newly allocated socket buffers can be added to skb->next. If an error occurred and jumped to out_error label, those newly allocated socket buffers can be leaked. This patch fixes this issue by remembering the last allocated socket buffer in kcm_tx_msg(head)->last_skb. Link: https://syzkaller.appspot.com/bug?extid=6f98de741f7dbbfc4ccb [1] Reported-by: syzbot+6f98de741f7dbbfc4ccb@syzkaller.appspotmail.com Fixes: ab7ac4eb9832 ("kcm: Kernel Connection Multiplexor module") Signed-off-by: Shigeru Yoshida --- net/kcm/kcmsock.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/net/kcm/kcmsock.c b/net/kcm/kcmsock.c index 393f01b2a7e6..34d4062f639a 100644 --- a/net/kcm/kcmsock.c +++ b/net/kcm/kcmsock.c @@ -939,6 +939,8 @@ static int kcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) if (head != kcm->seq_skb) kfree_skb(head); + else if (copied) + kcm_tx_msg(head)->last_skb = skb; err = sk_stream_error(sk, msg->msg_flags, err);