2017-04-05 15:29:16

by Kees Cook

[permalink] [raw]
Subject: [PATCH v2] NFS: Avoid cross-structure casting

When the call to nfs_devname() fails, the error path attempts to retain
the error via the mnt variable, but this requires a cast across very
different types (char * to struct vfsmount *), which the upcoming
structure layout randomization plugin flags as being potentially
dangerous in the face of randomization. This is a false positive, but
what this code actually wants to do is retain the error value, so this
patch explicitly sets it, instead of using what seems to be an
unexpected cast.

Signed-off-by: Kees Cook <[email protected]>
---
v2: duh, use ERR_CAST. thanks neilb!
---
fs/nfs/namespace.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/fs/nfs/namespace.c b/fs/nfs/namespace.c
index 786f17580582..8ca5d147124d 100644
--- a/fs/nfs/namespace.c
+++ b/fs/nfs/namespace.c
@@ -259,9 +259,10 @@ struct vfsmount *nfs_do_submount(struct dentry *dentry, struct nfs_fh *fh,
if (page == NULL)
goto out;
devname = nfs_devname(dentry, page, PAGE_SIZE);
- mnt = (struct vfsmount *)devname;
- if (IS_ERR(devname))
+ if (IS_ERR(devname)) {
+ mnt = ERR_CAST(devname);
goto free_page;
+ }
mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
free_page:
free_page((unsigned long)page);
--
2.7.4


--
Kees Cook
Pixel Security


2017-04-12 18:59:16

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH v2] NFS: Avoid cross-structure casting

On Wed, Apr 5, 2017 at 8:29 AM, Kees Cook <[email protected]> wrote:
> When the call to nfs_devname() fails, the error path attempts to retain
> the error via the mnt variable, but this requires a cast across very
> different types (char * to struct vfsmount *), which the upcoming
> structure layout randomization plugin flags as being potentially
> dangerous in the face of randomization. This is a false positive, but
> what this code actually wants to do is retain the error value, so this
> patch explicitly sets it, instead of using what seems to be an
> unexpected cast.
>
> Signed-off-by: Kees Cook <[email protected]>

If I can get an Acked-by on this, I could push it via the gcc-plugin tree.

Thanks!

-Kees

> ---
> v2: duh, use ERR_CAST. thanks neilb!
> ---
> fs/nfs/namespace.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/fs/nfs/namespace.c b/fs/nfs/namespace.c
> index 786f17580582..8ca5d147124d 100644
> --- a/fs/nfs/namespace.c
> +++ b/fs/nfs/namespace.c
> @@ -259,9 +259,10 @@ struct vfsmount *nfs_do_submount(struct dentry *dentry, struct nfs_fh *fh,
> if (page == NULL)
> goto out;
> devname = nfs_devname(dentry, page, PAGE_SIZE);
> - mnt = (struct vfsmount *)devname;
> - if (IS_ERR(devname))
> + if (IS_ERR(devname)) {
> + mnt = ERR_CAST(devname);
> goto free_page;
> + }
> mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
> free_page:
> free_page((unsigned long)page);
> --
> 2.7.4
>
>
> --
> Kees Cook
> Pixel Security



--
Kees Cook
Pixel Security

2017-04-20 18:04:31

by Trond Myklebust

[permalink] [raw]
Subject: Re: [PATCH v2] NFS: Avoid cross-structure casting

T24gV2VkLCAyMDE3LTA0LTEyIGF0IDExOjU5IC0wNzAwLCBLZWVzIENvb2sgd3JvdGU6DQo+IE9u
IFdlZCwgQXByIDUsIDIwMTcgYXQgODoyOSBBTSwgS2VlcyBDb29rIDxrZWVzY29va0BjaHJvbWl1
bS5vcmc+DQo+IHdyb3RlOg0KPiA+IFdoZW4gdGhlIGNhbGwgdG8gbmZzX2Rldm5hbWUoKSBmYWls
cywgdGhlIGVycm9yIHBhdGggYXR0ZW1wdHMgdG8NCj4gPiByZXRhaW4NCj4gPiB0aGUgZXJyb3Ig
dmlhIHRoZSBtbnQgdmFyaWFibGUsIGJ1dCB0aGlzIHJlcXVpcmVzIGEgY2FzdCBhY3Jvc3MNCj4g
PiB2ZXJ5DQo+ID4gZGlmZmVyZW50IHR5cGVzIChjaGFyICogdG8gc3RydWN0IHZmc21vdW50ICop
LCB3aGljaCB0aGUgdXBjb21pbmcNCj4gPiBzdHJ1Y3R1cmUgbGF5b3V0IHJhbmRvbWl6YXRpb24g
cGx1Z2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