Received: by 2002:a05:6a10:6006:0:0:0:0 with SMTP id w6csp95006pxa; Wed, 26 Aug 2020 05:51:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJynBi8U7uK+/fy4Of9R0WHKd3r/oGKDyHT231bZpiQqvju3PsoJZYuBClYZlJmIA3PYHgMr X-Received: by 2002:a05:6402:1b02:: with SMTP id by2mr11663845edb.95.1598446279124; Wed, 26 Aug 2020 05:51:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598446279; cv=none; d=google.com; s=arc-20160816; b=X1rSD3V0o0utPOqnnv1nS5cgAJ0HuyicvxVi7Y6OX9AtwOEw01TUO+NQlxwIoIIOo2 sGZQKHTTR4RNcgVDmwAkfQjMn3mb+kXAbTG93KWL0/pbHQ/r8CjElmmwXObawCufvTVq aSoXt/9XfjN4qaRg9jSbCrF4xJY2j+oKdXbLpXPiBBaAZuHnIQnhQu21yPu3kZdQiJ+h amusrG8ktgaNGom8iaTy8qrIGKgB9N7abHJAOa/q2m2mbzwT4dQorysL0IO/VFhR4Wgz 8Iq+bWCmx1aaqfDeQy6h1SQXW1d8fx9+Zx68imYtREV8iJmKwRiljj4PReRP3irDx2/J I+6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=T25WQv3ojJsvp0cUA7/bFzow+dOFqG8TcJqudwPS8Ck=; b=D+Bq59NbpyfpGOqm1zfcXqvgNosBESxNmxC5qWuLyAeDFr3RJdCDDNIaO1Hnl5VK9C 3WvHr01dYMbVmNk41f/kJfmP8ajaAmHhxxYs8zYR5AzD+neOAgtPSsZOl3puYJcTMBex bGgVrlgKIvHMWZ2JW7izfPf+N0iwiMyfeQDn99i9G7gZLZXQdS5C+O8YiZZb+82PSQpF tz3pz8MfKYof1vaNgR4CaJfFrO35v7wONwGseC8wEZDWg6BtDBC2sVhJ+WVY+95OVBsw Vd1WSlnTobSvu1Q8WJdmLsX7tQDHo6GKQQGACffPn1RhMMrYWRLsONfZ1E2EsQIMTKT5 1GSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=M9r2Jt4M; 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 r1si1540375eja.170.2020.08.26.05.50.55; Wed, 26 Aug 2020 05:51:19 -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=M9r2Jt4M; 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 S1730005AbgHZMuC (ORCPT + 99 others); Wed, 26 Aug 2020 08:50:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729391AbgHZMt7 (ORCPT ); Wed, 26 Aug 2020 08:49:59 -0400 Received: from mail-oi1-x241.google.com (mail-oi1-x241.google.com [IPv6:2607:f8b0:4864:20::241]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 59725C061574; Wed, 26 Aug 2020 05:49:59 -0700 (PDT) Received: by mail-oi1-x241.google.com with SMTP id u24so1380112oic.7; Wed, 26 Aug 2020 05:49:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=T25WQv3ojJsvp0cUA7/bFzow+dOFqG8TcJqudwPS8Ck=; b=M9r2Jt4MOONXPJmGuvp2bXRT5rYmoi/jifgzU/DP+1hL/xMrn0eQbwBxkSrnwxDcyk 5vqeBnTSsYKYnMjtxkOxmA6fjvZ2cxr54/rUVbjtvGWmrDnEcfSRxcFjHLN3A78rGPcp DtFfObU6K50X3Z+P09n64wZP8ruDrQL0ZfuMc1at40685Bth/e4My2gZRlff24tzd2+b mPLbYdHr+WOLGkwhso5X8hev6VAN1/WqJah+nLXAYhjX9R/r/twoKfFAZoVl2x98n+Dg s0s6+4hIVqwcgmRD2cC4ov749QBgGBhBPyXXzKEUiNTkreRvxy0vI/33P7csKTp+Bhik L0xA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=T25WQv3ojJsvp0cUA7/bFzow+dOFqG8TcJqudwPS8Ck=; b=QsVFk9G8p519YbbLS2oVpKYvJ0Uq+BMFl9nK44wCkrkqByCsOdR7uO5aU8vNrvC9EV bS1zPvSI8gquObTbaGFhCizYOSk4LLjME57PJKDLEQbx3+C2CJz6YcWFTrHKarg/v240 G62bxWqQ2PXb2dGqOVZ1U8caf1C7q7F6VPnEyLBxexCykcNNsrt9oLCFvnlOk2wkOnrX tGjNcSA9MCzDfbhYdmjtoNxutZUBmaSRv3Q4xeVsHV9uDxr38WEOLOsB1Mqfgua8iiXM 3Z0mWMiZkc1guxqr+sNCfQ2yo2Ms268EDxTKp0TvboQg3mLSipI+Fr88yWztJnYikCA+ BNZA== X-Gm-Message-State: AOAM530jqLlOOf7PQBLXVr4b96Mzq2LzqzxiDqXSGd02YBXqOMIIlhBj MeRP4rSx+Xgeio3tsFkcvZG4qeV7snTSI8uARxxZwynZQWU= X-Received: by 2002:a05:6808:310:: with SMTP id i16mr2879397oie.160.1598446198754; Wed, 26 Aug 2020 05:49:58 -0700 (PDT) MIME-Version: 1.0 References: <20200826113148.GA393664@mwanda> In-Reply-To: <20200826113148.GA393664@mwanda> From: Stephen Smalley Date: Wed, 26 Aug 2020 08:49:47 -0400 Message-ID: Subject: Re: [PATCH] selinux: fix error handling bugs in security_load_policy() To: Dan Carpenter Cc: Paul Moore , Eric Paris , Ondrej Mosnacek , Jeff Vander Stoep , SElinux list , linux-kernel , kernel-janitors@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Aug 26, 2020 at 7:32 AM Dan Carpenter wrote: > > There are a few bugs in the error handling for security_load_policy(). > > 1) If the newpolicy->sidtab allocation fails then it leads to a NULL > dereference. Also the error code was not set to -ENOMEM on that > path. > 2) If policydb_read() failed then we call policydb_destroy() twice > which meands we call kvfree(p->sym_val_to_name[i]) twice. > 3) If policydb_load_isids() failed then we call sidtab_destroy() twice > and that results in a double free in the sidtab_destroy_tree() > function because entry.ptr_inner and entry.ptr_leaf are not set to > NULL. > > One thing that makes this code nice to deal with is that none of the > functions return partially allocated data. In other words, the > policydb_read() either allocates everything successfully or it frees > all the data it allocates. It never returns a mix of allocated and > not allocated data. > > I re-wrote this to only free the successfully allocated data which > avoids the double frees. I also re-ordered selinux_policy_free() so > it's in the reverse order of the allocation function. > > Fixes: c7c556f1e81b ("selinux: refactor changing booleans") > Signed-off-by: Dan Carpenter I guess this wasn't against current selinux next branch? patching file security/selinux/ss/services.c Hunk #1 succeeded at 2145 (offset 18 lines). Hunk #2 succeeded at 2263 (offset 39 lines). Hunk #3 succeeded at 2303 with fuzz 1 (offset 47 lines). Hunk #4 succeeded at 2323 (offset 42 lines). But otherwise it looked good to me. Acked-by: Stephen Smalley