From: Christophe JAILLET <christophe.jaillet@wanadoo.fr> To: Guenter Roeck <linux@roeck-us.net>, Dan Carpenter <dan.carpenter@oracle.com>, Greg KH <gregkh@linuxfoundation.org> Cc: kernel-janitors@vger.kernel.org, linux-aspeed@lists.ozlabs.org, alistair@popple.id.au, linux-kernel@vger.kernel.org, linux-fsi@lists.ozlabs.org, linux-arm-kernel@lists.infradead.org Subject: Re: [PATCH] fsi: Aspeed: Fix a potential double free Date: Thu, 6 Jan 2022 19:35:56 +0100 [thread overview] Message-ID: <edd19014-3b99-fa0b-912b-e058c14401d8@wanadoo.fr> (raw) In-Reply-To: <f2ba50fd-5c6b-e905-17ed-541dcc98c6c1@roeck-us.net> Le 06/01/2022 à 18:25, Guenter Roeck a écrit : > On 1/6/22 12:14 AM, Dan Carpenter wrote: >> On Mon, Dec 27, 2021 at 07:29:07AM +0100, Greg KH wrote: >>> On Sun, Dec 26, 2021 at 05:56:02PM +0100, Christophe JAILLET wrote: >>>> 'aspeed' is a devm_alloc'ed, so there is no need to free it >>>> explicitly or >>>> there will be a double free(). >>> >>> A struct device can never be devm_alloced for obvious reasons. Perhaps >>> that is the real problem here? >>> >> >> I don't understand how "aspeed" is a struct device. >> > > -static void aspeed_master_release(struct device *dev) > -{ > - struct fsi_master_aspeed *aspeed = > - to_fsi_master_aspeed(dev_to_fsi_master(dev)); > - > - kfree(aspeed); > -} > > So "dev" is embedded in struct fsi_master, and struct fsi_master is > embedded > in struct fsi_master_aspeed. Since "struct device" is embedded, the data > structure embedding it must be released with the release function, as is > done > here. The problem is indeed that the data structure is allocated with > devm_kzalloc(), which as Greg points out must not be devm_ allocated > (because its lifetime does not match the lifetime of devm_ allocated > memory). Thanks a lot for the detailed explanation. Crystal clear for me now. Do you want me to send a patch to remove the devm_ or will you? CJ > >> I've been working on understanding device managed memory recently for >> Smatch. It's really complicated. There are a bunch of rules/heuristics >> that I'm slowly creating to generate new warnings but I'm a long way >> from understanding it well myself. >> > > A data structure embedding struct device must not be devm_ allocated, > and it must be released with the release callback. Maybe there is > a means to flag that somehow ? > > Guenter >
WARNING: multiple messages have this Message-ID (diff)
From: Christophe JAILLET <christophe.jaillet@wanadoo.fr> To: Guenter Roeck <linux@roeck-us.net>, Dan Carpenter <dan.carpenter@oracle.com>, Greg KH <gregkh@linuxfoundation.org> Cc: kernel-janitors@vger.kernel.org, linux-aspeed@lists.ozlabs.org, alistair@popple.id.au, linux-kernel@vger.kernel.org, linux-fsi@lists.ozlabs.org, linux-arm-kernel@lists.infradead.org Subject: Re: [PATCH] fsi: Aspeed: Fix a potential double free Date: Thu, 6 Jan 2022 19:35:56 +0100 [thread overview] Message-ID: <edd19014-3b99-fa0b-912b-e058c14401d8@wanadoo.fr> (raw) In-Reply-To: <f2ba50fd-5c6b-e905-17ed-541dcc98c6c1@roeck-us.net> Le 06/01/2022 à 18:25, Guenter Roeck a écrit : > On 1/6/22 12:14 AM, Dan Carpenter wrote: >> On Mon, Dec 27, 2021 at 07:29:07AM +0100, Greg KH wrote: >>> On Sun, Dec 26, 2021 at 05:56:02PM +0100, Christophe JAILLET wrote: >>>> 'aspeed' is a devm_alloc'ed, so there is no need to free it >>>> explicitly or >>>> there will be a double free(). >>> >>> A struct device can never be devm_alloced for obvious reasons. Perhaps >>> that is the real problem here? >>> >> >> I don't understand how "aspeed" is a struct device. >> > > -static void aspeed_master_release(struct device *dev) > -{ > - struct fsi_master_aspeed *aspeed = > - to_fsi_master_aspeed(dev_to_fsi_master(dev)); > - > - kfree(aspeed); > -} > > So "dev" is embedded in struct fsi_master, and struct fsi_master is > embedded > in struct fsi_master_aspeed. Since "struct device" is embedded, the data > structure embedding it must be released with the release function, as is > done > here. The problem is indeed that the data structure is allocated with > devm_kzalloc(), which as Greg points out must not be devm_ allocated > (because its lifetime does not match the lifetime of devm_ allocated > memory). Thanks a lot for the detailed explanation. Crystal clear for me now. Do you want me to send a patch to remove the devm_ or will you? CJ > >> I've been working on understanding device managed memory recently for >> Smatch. It's really complicated. There are a bunch of rules/heuristics >> that I'm slowly creating to generate new warnings but I'm a long way >> from understanding it well myself. >> > > A data structure embedding struct device must not be devm_ allocated, > and it must be released with the release callback. Maybe there is > a means to flag that somehow ? > > Guenter > _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
next prev parent reply other threads:[~2022-01-06 18:36 UTC|newest] Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-12-26 16:56 [PATCH] fsi: Aspeed: Fix a potential double free Christophe JAILLET 2021-12-26 16:56 ` Christophe JAILLET 2021-12-27 6:29 ` Greg KH 2021-12-27 6:29 ` Greg KH 2021-12-27 7:48 ` Christophe JAILLET 2021-12-27 7:48 ` Christophe JAILLET 2022-01-06 8:14 ` Dan Carpenter 2022-01-06 8:14 ` Dan Carpenter 2022-01-06 17:25 ` Guenter Roeck 2022-01-06 17:25 ` Guenter Roeck 2022-01-06 18:35 ` Christophe JAILLET [this message] 2022-01-06 18:35 ` Christophe JAILLET 2022-01-07 1:35 ` Guenter Roeck 2022-01-07 1:35 ` Guenter Roeck
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=edd19014-3b99-fa0b-912b-e058c14401d8@wanadoo.fr \ --to=christophe.jaillet@wanadoo.fr \ --cc=alistair@popple.id.au \ --cc=dan.carpenter@oracle.com \ --cc=gregkh@linuxfoundation.org \ --cc=kernel-janitors@vger.kernel.org \ --cc=linux-arm-kernel@lists.infradead.org \ --cc=linux-aspeed@lists.ozlabs.org \ --cc=linux-fsi@lists.ozlabs.org \ --cc=linux-kernel@vger.kernel.org \ --cc=linux@roeck-us.net \ /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: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.