Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp754420pxt; Thu, 5 Aug 2021 10:47:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyQVyfSFOyvZ+WftsYPYBwVbUtVEUk37FHSgmXO3DXPKC4V1DPm5xgtFyV9ul/uVDJ63e3g X-Received: by 2002:a05:6402:1d25:: with SMTP id dh5mr8130212edb.377.1628185678215; Thu, 05 Aug 2021 10:47:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628185678; cv=none; d=google.com; s=arc-20160816; b=NT+ev80tsjMhMr6fXhiEP1ha0RhTDV/nvfFhIoZzTUDnD4kB8CPGIgjEC9YICu6EnB iFT71itlXwkyryUBUzdMXOlZTPrd7x8UuSW3Dm7Yc5XQCfGYJNJfRO9c5/t2b50qaKGk CFY7TLwrkt4VwRJrp7Z15wO7XM/zuV8IDCC0fOP0e9bLzN1H5bgbN41BOUh4j2T04QQf bTXybS89hbZ8TEf96y0iILSpBOFy9+lypsxJDZ/ignq0iWG/Nsh64xbyeXfpa9g+66B/ ptCEGo7Ycn1t1ls5hSJvLriKRjOUyNDpg9Vt6jYPQp02bSaeHfEZq+5nn62VSAVXnouI xcXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=BU77sf7xzIvRwh5goFFmMNFMuNqmBOnkMiEiz3vg3H8=; b=ufl9Mbr3Cn4IjKMR7gad+HG2FSsGQbHXLg++6VJdZZ5J9K75kTZs4UQzktPVxxmkTq Q/4DPPMOp3esVYwxxujaKETyGL1xhmlodrtk6ap2zh9fGOGp0Z0PTnqmSr1VMfz+lBZZ RsLi59K68DaZALDewvS8rMbEIjwjVa/99MolNac1M8d9slZzYeuw21iBPupiin69fVnh /ZIbk7EXWheiGKxAPGyXeA8OnPgI3VyOOv1waM4ZpKcMFSxxCE8TC/zkz8E57LogzJi4 dQEE/iIMlq4sZLCj+C28cwrC7WqSOwFPBh9dPNGwSMT6fEt+JZfnLFbhZUUVuGVIm4eM OVMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=lPu8QbrC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id hp8si7206396ejc.49.2021.08.05.10.47.34; Thu, 05 Aug 2021 10:47:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=lPu8QbrC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238861AbhHEMxT (ORCPT + 99 others); Thu, 5 Aug 2021 08:53:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48246 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241535AbhHEMxQ (ORCPT ); Thu, 5 Aug 2021 08:53:16 -0400 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89531C06179B; Thu, 5 Aug 2021 05:53:01 -0700 (PDT) Received: by mail-pj1-x102d.google.com with SMTP id pj14-20020a17090b4f4eb029017786cf98f9so9153081pjb.2; Thu, 05 Aug 2021 05:53:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding:content-language; bh=BU77sf7xzIvRwh5goFFmMNFMuNqmBOnkMiEiz3vg3H8=; b=lPu8QbrCC0GfZOFqovmt2ITkMV4/39Mn3HuQdnvfqMJ0/7ePozTMm45XiQU8R7bZYg wUgt9DgxjVuwrMXwvgtoL6eR8UFX3B4a5rONheat0OJnfUedXZKQ/kQesp7UYYbMNde5 +hD9Nx/U6mVO4uG00X0xNLzwmSDuoEaQWVupk/XqESszul8IpNMIy5h7IKQmlk9Dpvnv yxMVlk/x4JTVM6Zr/gmIq9LJ3l2wnQChJ/STI64CL2IA5LYzi8pwgtRJQH9f0OXv6D3I fiyQWEAEqM4Ugw3kLe6j2ojLNItBuUU3p1E1NUicBh7s4ZPL3pTkwcTBYqhEgQ8gcwAt Qy4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=BU77sf7xzIvRwh5goFFmMNFMuNqmBOnkMiEiz3vg3H8=; b=LXSl9IGc2BwaE25O5mYABbvxgsNtfjLINmp5aMfd7ksoKVTfF766UPiBVvt36ma8DY Vu9slHwceliXcaqBiFUq4DbciUn7+ggbdf7CMXF35IN+1bYwdkKlXjPYzVPFAG4tdAC3 Hd0wPF5nd0vHjHbe8qaf5K3Kd9MvjcOMeTrK94j6A+rXd+yiksNib9g77wN9vvYd0B3k 1c4+MblFLswNbNs2s8Jips59tjDAAR2E4cSS3JCXya19lc6YoFGfReWsZzJWllQvdZrN g4gHeNEbh3BtQ64sdF8Yfya7wwpL1oUaz0CtogMXjFOPL0aSRRwekQYXt1JkLeYG4dHm EkXQ== X-Gm-Message-State: AOAM533iRDIgUVpJ0vIStGF8hn37Y1qGag7wVUcUQE0RJJ5f+9WaZkvc ytPMlsRCgwIo7E/59D7vldI= X-Received: by 2002:a17:90a:f002:: with SMTP id bt2mr4795630pjb.142.1628167981168; Thu, 05 Aug 2021 05:53:01 -0700 (PDT) Received: from [10.114.0.66] ([45.135.186.81]) by smtp.gmail.com with ESMTPSA id g6sm6789881pfh.111.2021.08.05.05.52.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 05 Aug 2021 05:53:00 -0700 (PDT) Subject: Re: [PATCH] ceph: fix possible null-pointer dereference in ceph_mdsmap_decode() To: Jeff Layton , idryomov@gmail.com Cc: ceph-devel@vger.kernel.org, linux-kernel@vger.kernel.org, baijiaju1990@gmail.com, TOTE Robot References: <20210805122015.129824-1-islituo@gmail.com> From: Tuo Li Message-ID: <2f3266d3-cf95-ca8c-2b61-8a906e21bc38@gmail.com> Date: Thu, 5 Aug 2021 20:52:57 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=iso-8859-15; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Thanks for your feedback. It sounds good to put an "if (m->m_info)" around the for loop to fix this bug, as well as avoid other potential bugs caused by calling ceph_mdsmap_destroy(). I think we can prepare a V2 patch according to your advice. On 2021/8/5 20:29, Jeff Layton wrote: > On Thu, 2021-08-05 at 05:20 -0700, Tuo Li wrote: >> kcalloc() is called to allocate memory for m->m_info, and if it fails, >> ceph_mdsmap_destroy() behind the label out_err will be called: >> ceph_mdsmap_destroy(m); >> >> In ceph_mdsmap_destroy(), m->m_info is dereferenced through: >> kfree(m->m_info[i].export_targets); >> >> To fix this possible null-pointer dereference, if memory allocation >> for m->m_info fails, free m and return -ENOMEM. >> >> Reported-by: TOTE Robot >> Signed-off-by: Tuo Li >> --- >> fs/ceph/mdsmap.c | 6 ++++-- >> 1 file changed, 4 insertions(+), 2 deletions(-) >> >> diff --git a/fs/ceph/mdsmap.c b/fs/ceph/mdsmap.c >> index abd9af7727ad..7d73e4b64b12 100644 >> --- a/fs/ceph/mdsmap.c >> +++ b/fs/ceph/mdsmap.c >> @@ -166,8 +166,10 @@ struct ceph_mdsmap *ceph_mdsmap_decode(void **p, void *end, bool msgr2) >> m->possible_max_rank = max(m->m_num_active_mds, m->m_max_mds); >> >> m->m_info = kcalloc(m->possible_max_rank, sizeof(*m->m_info), GFP_NOFS); >> - if (!m->m_info) >> - goto nomem; >> + if (!m->m_info) { >> + kfree(m); >> + return ERR_PTR(-ENOMEM); >> + } >> >> /* pick out active nodes from mds_info (state > 0) */ >> for (i = 0; i < n; i++) { > Good catch. This function is already pretty complex. How about we > instead fix this in ceph_mdsmap_destroy and make it safe to call that > with the mdsmap in this state? > > Basically, just put an "if (m->m_info)" around the for loop in that > function. Sound ok? >