• Daniel Borkmann's avatar
    net: fib: fib6_add: fix potential NULL pointer dereference · ae7b4e1f
    Daniel Borkmann authored
    When the kernel is compiled with CONFIG_IPV6_SUBTREES, and we return
    with an error in fn = fib6_add_1(), then error codes are encoded into
    the return pointer e.g. ERR_PTR(-ENOENT). In such an error case, we
    write the error code into err and jump to out, hence enter the if(err)
    condition. Now, if CONFIG_IPV6_SUBTREES is enabled, we check for:
    
      if (pn != fn && pn->leaf == rt)
        ...
      if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO))
        ...
    
    Since pn is NULL and fn is f.e. ERR_PTR(-ENOENT), then pn != fn
    evaluates to true and causes a NULL-pointer dereference on further
    checks on pn. Fix it, by setting both NULL in error case, so that
    pn != fn already evaluates to false and no further dereference
    takes place.
    
    This was first correctly implemented in 4a287eba ("IPv6 routing,
    NLM_F_* flag support: REPLACE and EXCL flags support, warn about
    missing CREATE flag"), but the bug got later on introduced by
    188c517a ("ipv6: return errno pointers consistently for fib6_add_1()").
    Signed-off-by: default avatarDaniel Borkmann <dborkman@redhat.com>
    Cc: Lin Ming <mlin@ss.pku.edu.cn>
    Cc: Matti Vaittinen <matti.vaittinen@nsn.com>
    Cc: Hannes Frederic Sowa <hannes@stressinduktion.org>
    Acked-by: default avatarHannes Frederic Sowa <hannes@stressinduktion.org>
    Acked-by: default avatarMatti Vaittinen <matti.vaittinen@nsn.com>
    Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
    ae7b4e1f
ip6_fib.c 37.8 KB