From: Joe Lawrence <joe.lawrence@redhat.com>
To: Dan Carpenter <dan.carpenter@oracle.com>
Cc: pmladek@suse.com, live-patching@vger.kernel.org
Subject: Re: [bug report] livepatch: Initialize shadow variables safely by a custom callback
Date: Tue, 7 Jan 2020 10:43:11 -0500 [thread overview]
Message-ID: <5fca242f-132a-db4a-cc0d-a51a61d72041@redhat.com> (raw)
In-Reply-To: <20200107152337.GB27042@kadam>
On 1/7/20 10:23 AM, Dan Carpenter wrote:
> On Tue, Jan 07, 2020 at 10:06:21AM -0500, Joe Lawrence wrote:
>> On 1/7/20 8:29 AM, Dan Carpenter wrote:
>>> Hello Petr Mladek,
>>>
>>> The patch e91c2518a5d2: "livepatch: Initialize shadow variables
>>> safely by a custom callback" from Apr 16, 2018, leads to the
>>> following static checker warning:
>>>
>>> samples/livepatch/livepatch-shadow-fix1.c:86 livepatch_fix1_dummy_alloc()
>>> error: 'klp_shadow_alloc()' 'leak' too small (4 vs 8)
>>>
>>> samples/livepatch/livepatch-shadow-fix1.c
>>> 53 static int shadow_leak_ctor(void *obj, void *shadow_data, void *ctor_data)
>>> 54 {
>>> 55 void **shadow_leak = shadow_data;
>>> 56 void *leak = ctor_data;
>>> 57
>>> 58 *shadow_leak = leak;
>>> 59 return 0;
>>> 60 }
>>> 61
>>> 62 static struct dummy *livepatch_fix1_dummy_alloc(void)
>>> 63 {
>>> 64 struct dummy *d;
>>> 65 void *leak;
>>> 66
>>> 67 d = kzalloc(sizeof(*d), GFP_KERNEL);
>>> 68 if (!d)
>>> 69 return NULL;
>>> 70
>>> 71 d->jiffies_expire = jiffies +
>>> 72 msecs_to_jiffies(1000 * EXPIRE_PERIOD);
>>> 73
>>> 74 /*
>>> 75 * Patch: save the extra memory location into a SV_LEAK shadow
>>> 76 * variable. A patched dummy_free routine can later fetch this
>>> 77 * pointer to handle resource release.
>>> 78 */
>>> 79 leak = kzalloc(sizeof(int), GFP_KERNEL);
>>> 80 if (!leak) {
>>> 81 kfree(d);
>>> 82 return NULL;
>>> 83 }
>>> 84
>>> 85 klp_shadow_alloc(d, SV_LEAK, sizeof(leak), GFP_KERNEL,
>>> ^^^^^^^^^^^^
>>> This doesn't seem right at all? Leak is a pointer. Why is leak a void
>>> pointer instead of an int pointer?
>>>
>>
>> Hi Dan,
>>
>> If I remember this code correctly, the shadow variable is tracking the
>> pointer value itself and not its contents, so sizeof(leak) should be correct
>> for the shadow variable data size.
>>
>> (For kernel/livepatch/shadow.c :: __klp_shadow_get_or_alloc() creates new
>> struct klp_shadow with .data[size] to accommodate its meta-data plus the
>> desired data).
>>
>> Why isn't leak an int pointer? I don't remember why, according to git
>> history it's been that way since the beginning. I think it was coded to
>> say, "Give me some storage, any size an int will do. I'm not going to touch
>> it, but I want to demonstrate a memory leak".
>>
>> Would modifying the pointer type satisfy the static code complaint?
>>
>> Since the warning is about a size mismatch, what are the parameters that it
>> is keying on? [ ... snip ... ]
>
> It looks at places which call klp_shadow_alloc() and says that sometimes
> the third argument is the size of the last argument. Then it complains
> when a caller doesn't match.
>
> I could just make klp_shadow_alloc() an exception.
>
Ah, I see. It must also be checking that the last two arguments (ctor,
ctor_data) are non-null, as that's the simplified calling sequence.
/me runs cscope to find an example ...
Well that's interesting, there really aren't any other
klp_shadow_alloc() callers aside from
lib/livepatch/test_klp_shadow_vars.c, which hides it in a wrapper
routine that probably dodges the static code check.
For a typical out-of-tree example [1], we do a lot of this:
newpid = klp_shadow_get_or_alloc(p, 0, sizeof(*newpid),
GFP_KERNEL, NULL, NULL);
if (newpid)
*newpid = ctr++;
as we don't always need the constructor / destructor callbacks for
simple shadow variables.
Since the ctor/dtor callback part of the API was provided by Petr, I'll
let him chime in what the code checker should be warning about here. I
think it may be more complicated than it's worth, but maybe he has
another idea.
Regards,
-- Joe
[1]
https://github.com/dynup/kpatch/blob/master/test/integration/centos-7/shadow-newpid.patch
next prev parent reply other threads:[~2020-01-07 15:43 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-01-07 13:29 [bug report] livepatch: Initialize shadow variables safely by a custom callback Dan Carpenter
2020-01-07 15:06 ` Joe Lawrence
2020-01-07 15:23 ` Dan Carpenter
2020-01-07 15:43 ` Joe Lawrence [this message]
2020-01-09 9:29 ` Petr Mladek
2020-01-13 15:11 ` Joe Lawrence
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=5fca242f-132a-db4a-cc0d-a51a61d72041@redhat.com \
--to=joe.lawrence@redhat.com \
--cc=dan.carpenter@oracle.com \
--cc=live-patching@vger.kernel.org \
--cc=pmladek@suse.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;
as well as URLs for NNTP newsgroup(s).