Re: [PATCH] jfs: Add missing NULL pointer check in __get_metapage

Juerg Haefliger <[email protected]>
Newsgroups gmane.comp.file-systems.jfs.general
Message-ID <50f61041-7507-6410-ddf5-36892759be8b__46894.0925821245$1509630250$gmane$org@canonical.com>

On 11/02/2017 02:15 PM, Dave Kleikamp wrote:
> On 11/02/2017 01:59 AM, Juerg Haefliger wrote:
>>
>>
>> On 10/30/2017 11:13 PM, Dave Kleikamp wrote:
>>> On 10/25/2017 02:50 AM, Juerg Haefliger wrote:
>>>> Is this a patch you might consider?
>>>
>>> Sorry it's taken me so long to respond.
>>>
>>> I don't think this is the right fix. A failed allocation will still
>>> result in a null pointer dereference by the caller, __get_metapage(). I
>>> think the check needs to be put there. Like this:
>>>
>>> --- a/fs/jfs/jfs_metapage.c
>>> +++ b/fs/jfs/jfs_metapage.c
>>> @@ -663,6 +663,8 @@ struct metapage *__get_metapage(struct inode *inode,
>>> unsigned long lblock,
>>>  	} else {
>>>  		INCREMENT(mpStat.pagealloc);
>>>  		mp = alloc_metapage(GFP_NOFS);
>>> +		if (!mp)
>>> +			goto unlock;
>>>  		mp->page = page;
>>>  		mp->sb = inode->i_sb;
>>>  		mp->flag = 0;
>>
>> I don't understand. This is part of the patch that I sent.
> 
> Doh! How'd I miss that?

:-)


>>
>>
>>>
>>> Furthermore, it looks like all the callers of __get_metapage() check for
>>> a null return, so I'm not sure we need to handle the error at this
>>> point. I might have to look a bit harder at that, since there are many
>>> callers.
>>
>> I don't understand this either :-) Yes, the callers do check for a null
>> pointer but things blow up (in __get_metapage) before that check without
>> the above fix.
> 
> Yeah, the fix to __get_metapage() is necessary. I'm not convinced the
> first part of the patch, to alloc_metapage(), is necessary.

It's not. I just thought it'd be nice to get some sort of notification
in the log when the alloc fails. But if the callers log it then that's fine.

...Juerg


>>
>> ...Juerg
>>
>>
>>>
>>> Thanks,
>>> Shaggy
>>>
>>>>
>>>> Thanks
>>>> ...Juerg
>>>>
>>>>
>>>> On 10/04/2017 10:24 AM, Juerg Haefliger wrote:
>>>>> alloc_metapage can return a NULL pointer so check for that. And also emit
>>>>> an error message if that happens.
>>>>>
>>>>> Signed-off-by: Juerg Haefliger <[email protected]>
>>>>> ---
>>>>>  fs/jfs/jfs_metapage.c | 20 +++++++++++++-------
>>>>>  1 file changed, 13 insertions(+), 7 deletions(-)
>>>>>
>>>>> diff --git a/fs/jfs/jfs_metapage.c b/fs/jfs/jfs_metapage.c
>>>>> index 1c4b9ad4d7ab..00f21af66872 100644
>>>>> --- a/fs/jfs/jfs_metapage.c
>>>>> +++ b/fs/jfs/jfs_metapage.c
>>>>> @@ -187,14 +187,18 @@ static inline struct metapage *alloc_metapage(gfp_t gfp_mask)
>>>>>  {
>>>>>  	struct metapage *mp = mempool_alloc(metapage_mempool, gfp_mask);
>>>>>  
>>>>> -	if (mp) {
>>>>> -		mp->lid = 0;
>>>>> -		mp->lsn = 0;
>>>>> -		mp->data = NULL;
>>>>> -		mp->clsn = 0;
>>>>> -		mp->log = NULL;
>>>>> -		init_waitqueue_head(&mp->wait);
>>>>> +	if (!mp) {
>>>>> +		jfs_err("mempool_alloc failed!\n");
>>>>> +		return NULL;
>>>>>  	}
>>>>> +
>>>>> +	mp->lid = 0;
>>>>> +	mp->lsn = 0;
>>>>> +	mp->data = NULL;
>>>>> +	mp->clsn = 0;
>>>>> +	mp->log = NULL;
>>>>> +	init_waitqueue_head(&mp->wait);
>>>>> +
>>>>>  	return mp;
>>>>>  }
>>>>>  
>>>>> @@ -663,6 +667,8 @@ struct metapage *__get_metapage(struct inode *inode, unsigned long lblock,
>>>>>  	} else {
>>>>>  		INCREMENT(mpStat.pagealloc);
>>>>>  		mp = alloc_metapage(GFP_NOFS);
>>>>> +		if (!mp)
>>>>> +			goto unlock;
>>>>>  		mp->page = page;
>>>>>  		mp->sb = inode->i_sb;
>>>>>  		mp->flag = 0;
>>>>>
>>

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

_______________________________________________
Jfs-discussion mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jfs-discussion
signature.asc (application/pgp-signature, 845 B)
-----BEGIN PGP SIGNATURE-----

iQI7BAEBCAAlBQJZ+yEOHhxqdWVyZy5oYWVmbGlnZXJAY2Fub25pY2FsLmNvbQAK
CRB1TDqW+fi0jJWPD/4n7WszNth+vevocQmgpzUBD9uNrSstSZda/Dx0YPYB5Wve
c1kX8IuDhmuKJ7268woewKIs8mziOxrcjIcYTGzWPISVhU6ZXChF2q/405qrbidp
ukVlNhy9IfOP8xrw1DIFQtLvSbEbuHyYJJ3LmMMvwmTOmGcSNH1Ct2RPcIUu1Biq
xPzQIteztdBupw9Xw51tjI8vEdZTon2TE1nFOTZJ2YoSVaSo0rE+xi182e/620kZ
1hqIKhtx+VtmDZiBh2OFSnsdqHtzmwy4Nqd76ZAAN9oCIJYUbCIgSWxGii1LeMdr
Gjw0O3LgUGo6YgIYhAE9o8Xk6KInznG7UZBodXVVV+jM5/i8Z5n5p3ogv/Dj8l3l
KeeAiBOpYBXOjyT2J+l8t34Bgoh7etAhWNR77d1M4em31RltyDcvMWRYQ8ojgruI
Df/XZxWECWsiN4/HKZDaLmjNDPHM+Jq/HVE3dRYBFZBoBlBMOog2WM18++g59/R9
MkSNBF+NDHzrBejztv/jcCLe1aDAff4nKt9gV0PDT+ymukGylEhkAKlyotoWzqe4
vvR3OkJU0jiS+LJ7EKCRPpLO90z6N46ZKuJi7CcCesSomX38woaAOMmn9iR7KkNo
cO5C6gVptjKtA3F9ulGvFPFZyKhnamXWaLBANWvHQ9cKQ/iQnAnyR240s04ldg==
=MP7x
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.