Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp104913ybt; Thu, 25 Jun 2020 16:33:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwwH/r1MymWqGz5OVAnzisTKAqYwrzXFO3okhg5OP/r0ZSYL1/ejx3TDoMQTbOkA3eZgFDL X-Received: by 2002:a17:906:4086:: with SMTP id u6mr189198ejj.9.1593127993288; Thu, 25 Jun 2020 16:33:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593127993; cv=none; d=google.com; s=arc-20160816; b=s7I6QGHAi1HwkeCvLQb8qKJPI4pM1gZNC0SFjwSd5HCJXSZRRcT4nTTbXocYjA/iwE B8hQaRaS004TlNkpA8WJRQbwVVa+XWjTtvaFhV754Xet5NNSD0BEXTkRyXDmsOlDX/Vz htBa9SOwVAeWqn6EY0rN73G8E+3KRSuRHVRoNJgs72OndDbSQAf7hP+k34a6TEP5PcFV dxzFsjR4ndQyTXzxf7AxvAZGp+iMxxeVeoHUoNJKb4lgGzxoJjPsB1ZvWvv7fWktuAa/ sAkzaJMyozg9G0IIRakPtkCjsTnlYhQZ0oV6fm1y4VlsSK+VTLfhebKjNs8ltU8WlHOB YAWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:date:cc:to:from:subject:message-id; bh=ZYqQV4rEk3s+T00DZadI7758VgXatGuIxgCHEas4ZlY=; b=o3KzwPhXKTpL48YX5U2kb8ztE74hKnLUZXqTlN8Q2byIjJyqRS/Mtit5J/+7LECA3a RkUPbtY9URibEvKZ/N1eQ21zW+3r1qCubst80vko5NpDjlLaaxIGvRPfXpcsbZKlhudi qGEtbNakZ518EghzNYa4FDmZYS+DmazqeOkRwXEhmaQ+UpX+X0waoXQfC/oMFtlg1ymw T9Cw7c2P3Z7UexmdKxJ0C1VpeP+vj4KyW5fhwpdQDOED94dLe6kqzTSnrTH0w12vErfX kwb7coEOg+eJvJm/QvYHqLVppU6/Dmhb67LGiVCOrxIVuHt/QV7SnnQ0PIwFg7YIHO1g LB0A== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i7si17507098edb.116.2020.06.25.16.32.50; Thu, 25 Jun 2020 16:33:13 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2407110AbgFYUcY (ORCPT + 99 others); Thu, 25 Jun 2020 16:32:24 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:29146 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389406AbgFYUcV (ORCPT ); Thu, 25 Jun 2020 16:32:21 -0400 Received: from pps.filterd (m0098394.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 05PK2sGH007785; Thu, 25 Jun 2020 16:32:10 -0400 Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 31vx91947e-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 25 Jun 2020 16:32:10 -0400 Received: from m0098394.ppops.net (m0098394.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.36/8.16.0.36) with SMTP id 05PK4IjH011736; Thu, 25 Jun 2020 16:32:10 -0400 Received: from ppma06ams.nl.ibm.com (66.31.33a9.ip4.static.sl-reverse.com [169.51.49.102]) by mx0a-001b2d01.pphosted.com with ESMTP id 31vx91946b-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 25 Jun 2020 16:32:09 -0400 Received: from pps.filterd (ppma06ams.nl.ibm.com [127.0.0.1]) by ppma06ams.nl.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 05PKPiEe002905; Thu, 25 Jun 2020 20:32:07 GMT Received: from b06cxnps3075.portsmouth.uk.ibm.com (d06relay10.portsmouth.uk.ibm.com [9.149.109.195]) by ppma06ams.nl.ibm.com with ESMTP id 31uusjj4hb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 25 Jun 2020 20:32:07 +0000 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 05PKW51j52691146 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 Jun 2020 20:32:05 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 316F311C04C; Thu, 25 Jun 2020 20:32:05 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id B9AB411C050; Thu, 25 Jun 2020 20:32:03 +0000 (GMT) Received: from localhost.localdomain (unknown [9.85.142.225]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Thu, 25 Jun 2020 20:32:03 +0000 (GMT) Message-ID: <1593117123.27152.385.camel@linux.ibm.com> Subject: Re: [PATCH 02/12] ima: Create a function to free a rule entry From: Mimi Zohar To: Tyler Hicks Cc: Dmitry Kasatkin , James Morris , "Serge E . Hallyn" , Lakshmi Ramasubramanian , Prakhar Srivastava , linux-kernel@vger.kernel.org, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org Date: Thu, 25 Jun 2020 16:32:03 -0400 In-Reply-To: <20200625195647.GB4694@sequoia> References: <20200623003236.830149-1-tyhicks@linux.microsoft.com> <20200623003236.830149-3-tyhicks@linux.microsoft.com> <1593113613.27152.345.camel@linux.ibm.com> <20200625195647.GB4694@sequoia> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.20.5 (3.20.5-1.fc24) Mime-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.216,18.0.687 definitions=2020-06-25_16:2020-06-25,2020-06-25 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 suspectscore=2 lowpriorityscore=0 mlxlogscore=999 clxscore=1015 impostorscore=0 phishscore=0 spamscore=0 adultscore=0 malwarescore=0 priorityscore=1501 bulkscore=0 cotscore=-2147483648 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2004280000 definitions=main-2006250116 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 2020-06-25 at 14:56 -0500, Tyler Hicks wrote: > On 2020-06-25 15:33:33, Mimi Zohar wrote: > > On Mon, 2020-06-22 at 19:32 -0500, Tyler Hicks wrote: > > > There are several possible pieces of allocated memory in a rule entry. > > > Create a function that can free all allocated memory for a given rule > > > entry. > > > > > > This patch introduces no functional changes but sets the groundwork for > > > some memory leak fixes. > > > > > > Signed-off-by: Tyler Hicks > > > > Having a function to release all memory associated with a policy rule > > in general is a good idea.  However, in the case of the shallow copy, > > we're not removing any IMA rules, just updating the LSM info. > > > > There is an opportunity to transition from the builtin policy rules to > > a custom IMA policy.  Afterwards IMA policy rules may only be > > appended. > > > > An IMA custom policy based on LSM info may be loaded prior to the LSM > > policy.  These LSM based rules are inactive until the corresponding > > LSM rule is loaded.  In some environments, LSM policies are loaded and > > removed frequently.  The IMA rules themselves are not removed, just > > the LSM info is updated to reflect the current LSM info. > > > > > --- > > > security/integrity/ima/ima_policy.c | 33 +++++++++++++++++++++++++++-- > > > 1 file changed, 31 insertions(+), 2 deletions(-) > > > > > > diff --git a/security/integrity/ima/ima_policy.c b/security/integrity/ima/ima_policy.c > > > index 236a731492d1..1320333201c6 100644 > > > --- a/security/integrity/ima/ima_policy.c > > > +++ b/security/integrity/ima/ima_policy.c > > > @@ -261,6 +261,27 @@ static void ima_lsm_free_rule(struct ima_rule_entry *entry) > > > security_filter_rule_free(entry->lsm[i].rule); > > > kfree(entry->lsm[i].args_p); > > > } > > > +} > > > + > > > +static void ima_free_rule(struct ima_rule_entry *entry) > > > +{ > > > + if (!entry) > > > + return; > > > + > > > + /* > > > + * entry->template->fields may be allocated in ima_parse_rule() but that > > > + * reference is owned by the corresponding ima_template_desc element in > > > + * the defined_templates list and cannot be freed here > > > + */ > > > + > > > + /* > > > + * When freeing newly added ima_rule_entry members, consider if you > > > + * need to disown any references after the shallow copy in > > > + * ima_lsm_copy_rule() > > > + */ > > > + kfree(entry->fsname); > > > + kfree(entry->keyrings); > > > + ima_lsm_free_rule(entry); > > > kfree(entry); > > > } > > > > > > @@ -298,10 +319,18 @@ static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry) > > > pr_warn("rule for LSM \'%s\' is undefined\n", > > > (char *)entry->lsm[i].args_p); > > > } > > > + > > > + /* Disown all references that were shallow copied */ > > > + entry->fsname = NULL; > > > + entry->keyrings = NULL; > > > + entry->template = NULL; > > > return nentry; > > > > > > out_err: > > > - ima_lsm_free_rule(nentry); > > > + nentry->fsname = NULL; > > > + nentry->keyrings = NULL; > > > + nentry->template = NULL; > > > + ima_free_rule(nentry); > > > > > return NULL; > > > } > > > > > > @@ -315,7 +344,7 @@ static int ima_lsm_update_rule(struct ima_rule_entry *entry) > > > > > > list_replace_rcu(&entry->list, &nentry->list); > > > synchronize_rcu(); > > > - ima_lsm_free_rule(entry); > > > + ima_free_rule(entry); > > > > This should only update the LSM info, nothing else. > > That's effectively what's happening since the fsname, keyrings, and > template pointers are being set to NULL, before exiting > ima_lsm_copy_rule(), in the ima_rule_entry that's going to be freed. Ah, that clarified the reason for setting fsname, keyrings, ... to null before calling ima_free_rule. > > This patch is only introducing the function which can free all memory > associated with a rule and is starting to use it in place that a rule > entry is freed. > > Would you rather me introduce ima_free_rule() for the upcoming memory > leak fixes in the series but not make use of it in > ima_lsm_update_rule()? You could add a comment explaining the NULLs, but it might be clearer to keep the direct call to ima_lsm_free_rule(). Mimi