Re: [PATCH 1/6] ERR_PTR: if errno value is known at compile time,make sure it's valid

From: Marcin Slusarz
Date: Thu May 22 2008 - 12:04:32 EST


On Mon, May 19, 2008 at 10:38:52AM +0400, Alexey Dobriyan wrote:
> On Sun, May 18, 2008 at 11:56:53PM +0200, Marcin Slusarz wrote:
> > ERR_PTR is easy to call with wrong argument (positive errno),
> > and this error lead to catastrophic event - oops or kernel panic
> > (dereference of invalid pointer).
> >
> > As most of error handling code paths are rarely tested, this kind of
> > bug can be hidden for years.
> >
> > (Currently there are > 1400 calls of ERR_PTR with constant argument.)
>
> > --- a/include/linux/err.h
> > +++ b/include/linux/err.h
> > @@ -19,11 +19,13 @@
> >
> > #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
> >
> > -static inline void *ERR_PTR(long error)
> > +static inline void *__ERR_PTR(long error)
> > {
> > return (void *) error;
> > }
> >
> > +#define ERR_PTR(error) (BUILD_BUG_ON(__builtin_constant_p(error) && !IS_ERR_VALUE(error)), __ERR_PTR(error))
>
> This needs comment that this construct must be a macro, otherwise gcc
> does something stupid and doesn't break the build (at least in the case
> below).
Thanks, added to new version.

> BTW, I used grep(1) to find it.

http://lkml.org/lkml/2008/5/11/159

>
> [PATCH] xprtrdma: fix ERR_PTR(E typo
>
> Signed-off-by: Alexey Dobriyan <adobriyan@xxxxxxxxx>
> ---
>
> net/sunrpc/xprtrdma/svc_rdma_transport.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> --- a/net/sunrpc/xprtrdma/svc_rdma_transport.c
> +++ b/net/sunrpc/xprtrdma/svc_rdma_transport.c
> @@ -661,7 +661,7 @@ static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
>
> cma_xprt = rdma_create_xprt(serv, 1);
> if (!cma_xprt)
> - return ERR_PTR(ENOMEM);
> + return ERR_PTR(-ENOMEM);
> xprt = &cma_xprt->sc_xprt;
>
> listen_id = rdma_create_id(rdma_listen_handler, cma_xprt, RDMA_PS_TCP);
>
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/