Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp323409pxt; Fri, 6 Aug 2021 03:00:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyOQsRS5+2PRaICDiPPlwes0m+xxyZuE57Do+d78GqfOqO5NFUaCvRkDzlAbz7SiFNs35iM X-Received: by 2002:aa7:cd9a:: with SMTP id x26mr11861287edv.26.1628244004913; Fri, 06 Aug 2021 03:00:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628244004; cv=none; d=google.com; s=arc-20160816; b=Ar2pLv81CasoWj4RGogs8jItkcWIbdOZJWSbWI4MB5iCtwjFRLbNlJsMXFJYao/WGD PpLP/vVC7/4wBWarCPUjhWy637RTWXdTpQ3+DX3nDJKGv0w0GD+li4p12W00OnCsacCz vJVPECl2DroHDlFKvfnFpAfX5z6KUOOM26XeIK0twJVu5hTs1Q14E9hKgrc/Lr8nLHh6 kGKER0pWG9mLCieANze65kRVRjPX2YwtY+C5YzEEAIFH8IUgRis65m6nkJy9H5s1Ecd2 +4TDo7oiFbsxZ9i9HKfpriV6OITO2dwL6f7MgxpPYQ/3UkHvQXLgzxWGMOPgcF7li6CK 43aQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=2gyfKEgbT5BTjyNZs/aGzasGPnzV93pjDH6vDUCPUck=; b=bgWKPDal8tzCliaEJJnotJL7IEjlNEBfwDttHfhvrCmqBVnVEfagD4DwL/0yLrI7Z7 nyCRD+dqiZL9WICSbcyjk9D5S/jJcj+Aj5itp7bRMrHoMNfi2obrCnwlUGEbJxCqDtVQ h+b65JHnVrPwNhSV8I1iuTwL1Kn9oDME3ijNE8Fkb7ExWDvkbTxKmQgepYyhUOE3JEZ2 WpTEQacKZ8egbWEQ+PfkV3KrdjFU2fua8A6YiwJYQ2Wx5dH28RWjqWGu9NGSqYY5w5nq k4TAvWdWcN+p8lXONFDWNs+2TTLbrcXz+5r0XEht9qs8wfSGZzQ2sF+jT5oGW6kZFH1I jptg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PQ+8H0tv; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gz11si8719335ejb.158.2021.08.06.02.59.40; Fri, 06 Aug 2021 03:00:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-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=@redhat.com header.s=mimecast20190719 header.b=PQ+8H0tv; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244770AbhHFJ6z (ORCPT + 99 others); Fri, 6 Aug 2021 05:58:55 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:41627 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244771AbhHFJ6v (ORCPT ); Fri, 6 Aug 2021 05:58:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1628243915; 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: in-reply-to:in-reply-to:references:references; bh=2gyfKEgbT5BTjyNZs/aGzasGPnzV93pjDH6vDUCPUck=; b=PQ+8H0tvT9QE75aFD2CtV4t7wD6+aLQnkeAwb38Tyn081B00TIHvTZB9spT8x+G3AmbT5W hedz+pP899uTqs8MnNi5TWmK9Ijyt7BoOFl1YZn3ynt2lt8o5Av8cjSv8/b8ji8gHGa+BE /wzSsQvJ/UYk9QxIAFtBngEkkibi3Iw= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-385-RuLOoc0LMt-X-7SK0tjnKw-1; Fri, 06 Aug 2021 05:58:33 -0400 X-MC-Unique: RuLOoc0LMt-X-7SK0tjnKw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D8B66802929; Fri, 6 Aug 2021 09:58:32 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.193.198]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2AE7E81F76; Fri, 6 Aug 2021 09:58:32 +0000 (UTC) From: Lukas Czerner To: tytso@mit.edu Cc: linux-ext4@vger.kernel.org Subject: [PATCH 7/7] mkquota: Fix potental NULL pointer dereference Date: Fri, 6 Aug 2021 11:58:20 +0200 Message-Id: <20210806095820.83731-7-lczerner@redhat.com> In-Reply-To: <20210806095820.83731-1-lczerner@redhat.com> References: <20210806095820.83731-1-lczerner@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org get_dq() function can fail when the memory allocation fails and so we could end up dereferencing NULL pointer. Fix it. Also, we should really return -ENOMEM instead of -1, or even 0 from various functions in quotaio_tree.c when memory allocation fails. Fix it as well. Signed-off-by: Lukas Czerner --- lib/support/mkquota.c | 8 ++++++-- lib/support/quotaio_tree.c | 8 ++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/support/mkquota.c b/lib/support/mkquota.c index dce077e6..420ba503 100644 --- a/lib/support/mkquota.c +++ b/lib/support/mkquota.c @@ -433,7 +433,8 @@ void quota_data_sub(quota_ctx_t qctx, struct ext2_inode_large *inode, dict = qctx->quota_dict[qtype]; if (dict) { dq = get_dq(dict, get_qid(inode, qtype)); - dq->dq_dqb.dqb_curspace -= space; + if (dq) + dq->dq_dqb.dqb_curspace -= space; } } } @@ -460,7 +461,8 @@ void quota_data_inodes(quota_ctx_t qctx, struct ext2_inode_large *inode, dict = qctx->quota_dict[qtype]; if (dict) { dq = get_dq(dict, get_qid(inode, qtype)); - dq->dq_dqb.dqb_curinodes += adjust; + if (dq) + dq->dq_dqb.dqb_curinodes += adjust; } } } @@ -533,6 +535,8 @@ static int scan_dquots_callback(struct dquot *dquot, void *cb_data) struct dquot *dq; dq = get_dq(quota_dict, dquot->dq_id); + if (!dq) + return -ENOMEM; dq->dq_id = dquot->dq_id; dq->dq_flags |= DQF_SEEN; diff --git a/lib/support/quotaio_tree.c b/lib/support/quotaio_tree.c index 6cc4fb5b..65e68792 100644 --- a/lib/support/quotaio_tree.c +++ b/lib/support/quotaio_tree.c @@ -569,7 +569,7 @@ static int report_block(struct dquot *dquot, unsigned int blk, char *bitmap, int entries, i; if (!buf) - return -1; + return -ENOMEM; set_bit(bitmap, blk); read_blk(dquot->dq_h, blk, buf); @@ -601,7 +601,7 @@ static int report_tree(struct dquot *dquot, unsigned int blk, int depth, __le32 *ref = (__le32 *) buf; if (!buf) - return 0; + return -ENOMEM; read_blk(dquot->dq_h, blk, buf); if (depth == QT_TREEDEPTH - 1) { @@ -667,12 +667,12 @@ int qtree_scan_dquots(struct quota_handle *h, struct dquot *dquot = get_empty_dquot(); if (!dquot) - return -1; + return -ENOMEM; dquot->dq_h = h; if (ext2fs_get_memzero((info->dqi_blocks + 7) >> 3, &bitmap)) { ext2fs_free_mem(&dquot); - return -1; + return -ENOMEM; } ret = report_tree(dquot, QT_TREEOFF, 0, bitmap, process_dquot, data); if (ret < 0) -- 2.31.1