mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Gavrilov Ilia <Ilia.Gavrilov@infotecs.ru>
To: Paul Moore <paul@paul-moore.com>
Cc: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Huw Davies <huw@codeweavers.com>,
	"netdev@vger.kernel.org" <netdev@vger.kernel.org>,
	"linux-security-module@vger.kernel.org" 
	<linux-security-module@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"lvc-project@linuxtesting.org" <lvc-project@linuxtesting.org>
Subject: Re: [PATCH net] calipso: Fix memory leak in netlbl_calipso_add_pass()
Date: Thu, 23 Nov 2023 07:55:39 +0000	[thread overview]
Message-ID: <0a6fff0d-61df-4ac5-ae8e-e2204c60ebb5@infotecs.ru> (raw)
In-Reply-To: <CAHC9VhTiq1xPXXsETNKRBOtfkB5wohVwhBeae+5QW9uV-h5vvg@mail.gmail.com>

On 11/22/23 18:56, Paul Moore wrote:
> On Wed, Nov 22, 2023 at 8:55 AM Gavrilov Ilia <Ilia.Gavrilov@infotecs.ru> wrote:
>>
>> If IPv6 support is disabled at boot (ipv6.disable=1),
>> the calipso_init() -> netlbl_calipso_ops_register() function isn't called,
>> and the netlbl_calipso_ops_get() function always returns NULL.
>> In this case, the netlbl_calipso_add_pass() function allocates memory
>> for the doi_def variable but doesn't free it with the calipso_doi_free().
> 
> It looks like a better option would be to return an error code in
> netlbl_calipso_add() so we never allocate the memory in the first
> place.
> 
> Untested patch below, copy-n-paste'd so there is likely whitespace
> damage, but you get the idea.
> 
> diff --git a/net/netlabel/netlabel_calipso.c b/net/netlabel/netlabel_calipso.c
> index f1d5b8465217..26a504dc6e57 100644
> --- a/net/netlabel/netlabel_calipso.c
> +++ b/net/netlabel/netlabel_calipso.c
> @@ -54,8 +54,31 @@ static const struct nla_policy
> calipso_genl_policy[NLBL_CALIPSO_A_MAX + 1] = {
>         [NLBL_CALIPSO_A_MTYPE] = { .type = NLA_U32 },
> };
> 
> +static const struct netlbl_calipso_ops *calipso_ops;
> +
> +/**
> + * netlbl_calipso_ops_register - Register the CALIPSO operations
> + * @ops: ops to register
> + *
> + * Description:
> + * Register the CALIPSO packet engine operations.
> + *
> + */
> +const struct netlbl_calipso_ops *
> +netlbl_calipso_ops_register(const struct netlbl_calipso_ops *ops)
> +{
> +       return xchg(&calipso_ops, ops);
> +}
> +EXPORT_SYMBOL(netlbl_calipso_ops_register);
> +
> +static const struct netlbl_calipso_ops *netlbl_calipso_ops_get(void)
> +{
> +       return READ_ONCE(calipso_ops);
> +}
> +
> /* NetLabel Command Handlers
>   */
> +
> /**
>   * netlbl_calipso_add_pass - Adds a CALIPSO pass DOI definition
>   * @info: the Generic NETLINK info block
> @@ -100,10 +123,13 @@ static int netlbl_calipso_add(struct sk_buff
> *skb, struct genl_info *info)
> {
>         int ret_val = -EINVAL;
>         struct netlbl_audit audit_info;
> +       const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
> 
>         if (!info->attrs[NLBL_CALIPSO_A_DOI] ||
>             !info->attrs[NLBL_CALIPSO_A_MTYPE])
>                 return -EINVAL;
> +       if (!ops)
> +               return -EOPNOTSUPP;
> 
>         netlbl_netlink_auditinfo(&audit_info);
>         switch (nla_get_u32(info->attrs[NLBL_CALIPSO_A_MTYPE])) {
> @@ -363,28 +389,6 @@ int __init netlbl_calipso_genl_init(void)
>         return genl_register_family(&netlbl_calipso_gnl_family);
> }
> 
> -static const struct netlbl_calipso_ops *calipso_ops;
> -
> -/**
> - * netlbl_calipso_ops_register - Register the CALIPSO operations
> - * @ops: ops to register
> - *
> - * Description:
> - * Register the CALIPSO packet engine operations.
> - *
> - */
> -const struct netlbl_calipso_ops *
> -netlbl_calipso_ops_register(const struct netlbl_calipso_ops *ops)
> -{
> -       return xchg(&calipso_ops, ops);
> -}
> -EXPORT_SYMBOL(netlbl_calipso_ops_register);
> -
> -static const struct netlbl_calipso_ops *netlbl_calipso_ops_get(void)
> -{
> -       return READ_ONCE(calipso_ops);
> -}
> -
> /**
>   * calipso_doi_add - Add a new DOI to the CALIPSO protocol engine
>   * @doi_def: the DOI structure
> 

Thank you for review. I'll test and send this idea in the next version.

      reply	other threads:[~2023-11-23  7:55 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-22 13:55 Gavrilov Ilia
2023-11-22 15:56 ` Paul Moore
2023-11-23  7:55   ` Gavrilov Ilia [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0a6fff0d-61df-4ac5-ae8e-e2204c60ebb5@infotecs.ru \
    --to=ilia.gavrilov@infotecs.ru \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=huw@codeweavers.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=lvc-project@linuxtesting.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=paul@paul-moore.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®