lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <b8348f42ef7e78e391619b198ee9a92eb74524e8.camel@mediatek.com>
Date: Thu, 22 Aug 2024 03:28:00 +0000
From: Tze-nan Wu (吳澤南) <Tze-nan.Wu@...iatek.com>
To: "yonghong.song@...ux.dev" <yonghong.song@...ux.dev>
CC: "sdf@...ichev.me" <sdf@...ichev.me>, "linux-mediatek@...ts.infradead.org"
	<linux-mediatek@...ts.infradead.org>, "linux-kernel@...r.kernel.org"
	<linux-kernel@...r.kernel.org>, "kuniyu@...zon.com" <kuniyu@...zon.com>,
	"ast@...nel.org" <ast@...nel.org>, "daniel@...earbox.net"
	<daniel@...earbox.net>, Cheng-Jui Wang (王正睿)
	<Cheng-Jui.Wang@...iatek.com>, wsd_upstream <wsd_upstream@...iatek.com>,
	"andrii@...nel.org" <andrii@...nel.org>,
	Bobule Chang (張弘義) <bobule.chang@...iatek.com>,
	"jolsa@...nel.org" <jolsa@...nel.org>, "john.fastabend@...il.com"
	<john.fastabend@...il.com>, "song@...nel.org" <song@...nel.org>,
	"kuba@...nel.org" <kuba@...nel.org>, "bpf@...r.kernel.org"
	<bpf@...r.kernel.org>, "kpsingh@...nel.org" <kpsingh@...nel.org>,
	"edumazet@...gle.com" <edumazet@...gle.com>,
	Yanghui Li (李阳辉) <Yanghui.Li@...iatek.com>,
	"martin.lau@...ux.dev" <martin.lau@...ux.dev>,
	"linux-arm-kernel@...ts.infradead.org"
	<linux-arm-kernel@...ts.infradead.org>, "eddyz87@...il.com"
	<eddyz87@...il.com>, "pabeni@...hat.com" <pabeni@...hat.com>,
	"netdev@...r.kernel.org" <netdev@...r.kernel.org>, "davem@...emloft.net"
	<davem@...emloft.net>, "haoluo@...gle.com" <haoluo@...gle.com>,
	"angelogioacchino.delregno@...labora.com"
	<angelogioacchino.delregno@...labora.com>, "matthias.bgg@...il.com"
	<matthias.bgg@...il.com>
Subject: Re: [PATCH net v4] bpf, net: Check cgroup_bpf_enabled() only once in
 do_sock_getsockopt()

On Wed, 2024-08-21 at 11:44 -0700, Yonghong Song wrote:
>  	 
>  
> On 8/21/24 2:30 AM, Tze-nan Wu wrote:
> > The return value from `cgroup_bpf_enabled(CGROUP_GETSOCKOPT)` can
> change
> > between the invocations of `BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN` and
> > `BPF_CGROUP_RUN_PROG_GETSOCKOPT`.
> >
> > If `cgroup_bpf_enabled(CGROUP_GETSOCKOPT)` changes from "false" to
> > "true" between the invocations of
> `BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN` and
> > `BPF_CGROUP_RUN_PROG_GETSOCKOPT`, `BPF_CGROUP_RUN_PROG_GETSOCKOPT`
> will
> > receive an -EFAULT from
> `__cgroup_bpf_run_filter_getsockopt(max_optlen=0)`
> > due to `get_user()` was not reached in
> `BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN`.
> >
> > Scenario shown as below:
> >
> >             `process A`                      `process B`
> >             -----------                      ------------
> >    BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN
> >                                              enable
> CGROUP_GETSOCKOPT
> >    BPF_CGROUP_RUN_PROG_GETSOCKOPT (-EFAULT)
> >
> > To prevent this, invoke `cgroup_bpf_enabled()` only once and cache
> the
> > result in a newly added local variable `enabled`.
> > Both `BPF_CGROUP_*` macros in `do_sock_getsockopt` will then check
> their
> > condition using the same `enabled` variable as the condition
> variable,
> > instead of using the return values from `cgroup_bpf_enabled` called
> by
> > themselves as the condition variable(which could yield different
> results).
> > This ensures that either both `BPF_CGROUP_*` macros pass the
> condition
> > or neither does.
> >
> > Fixes: 0d01da6afc54 ("bpf: implement getsockopt and setsockopt
> hooks")
> > Co-developed-by: Yanghui Li <yanghui.li@...iatek.com>
> > Signed-off-by: Yanghui Li <yanghui.li@...iatek.com>
> > Co-developed-by: Cheng-Jui Wang <cheng-jui.wang@...iatek.com>
> > Signed-off-by: Cheng-Jui Wang <cheng-jui.wang@...iatek.com>
> > Signed-off-by: Tze-nan Wu <Tze-nan.Wu@...iatek.com>
> > ---
> >
> > Chagnes from v1 to v2: 
> https://lore.kernel.org/all/20240819082513.27176-1-Tze-nan.Wu@mediatek.com/
> >    Instead of using cgroup_lock in the fastpath, invoke
> cgroup_bpf_enabled
> >    only once and cache the value in the newly added variable
> `enabled`.
> >    `BPF_CGROUP_*` macros in do_sock_getsockopt can then both check
> their
> >    condition with the new variable `enable`, ensuring that either
> they both
> >    passing the condition or both do not.
> >
> > Chagnes from v2 to v3: 
> https://lore.kernel.org/all/20240819155627.1367-1-Tze-nan.Wu@mediatek.com/
> >    Hide cgroup_bpf_enabled in the macro, and some modifications to
> adapt
> >    the coding style.
> >
> > Chagnes from v3 to v4: 
> https://lore.kernel.org/all/20240820092942.16654-1-Tze-nan.Wu@mediatek.com/
> >    Add bpf tag to subject, and Fixes tag in body.
> >
> > ---
> >   include/linux/bpf-cgroup.h | 15 ++++++++-------
> >   net/socket.c               |  5 +++--
> >   2 files changed, 11 insertions(+), 9 deletions(-)
> >
> > diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-
> cgroup.h
> > index fb3c3e7181e6..5afa2ac76aae 100644
> > --- a/include/linux/bpf-cgroup.h
> > +++ b/include/linux/bpf-cgroup.h
> > @@ -390,20 +390,20 @@ static inline bool
> cgroup_bpf_sock_enabled(struct sock *sk,
> >   __ret;       \
> >   })
> >   
> > -#define BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen)       \
> > +#define BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen, enabled)       \
> >   ({       \
> >   int __ret = 0;       \
> > -if (cgroup_bpf_enabled(CGROUP_GETSOCKOPT))       \
> > +enabled = cgroup_bpf_enabled(CGROUP_GETSOCKOPT);       \
> > +if (enabled)       \
> >   copy_from_sockptr(&__ret, optlen, sizeof(int));       \
> >   __ret;       \
> >   })
> >   
> >   #define BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock, level, optname,
> optval, optlen,   \
> > -       max_optlen, retval)       \
> > +       max_optlen, retval, enabled)       \
> >   ({       \
> >   int __ret = retval;       \
> > -if (cgroup_bpf_enabled(CGROUP_GETSOCKOPT) &&       \
> > -    cgroup_bpf_sock_enabled(sock, CGROUP_GETSOCKOPT))       \
> > +if (enabled && cgroup_bpf_sock_enabled(sock,
> CGROUP_GETSOCKOPT))       \
> >   if (!(sock)->sk_prot->bpf_bypass_getsockopt ||       \
> >       !INDIRECT_CALL_INET_1((sock)->sk_prot->bpf_bypass_getsockopt, 
> \
> >   tcp_bpf_bypass_getsockopt,       \
> > @@ -518,9 +518,10 @@ static inline int
> bpf_percpu_cgroup_storage_update(struct bpf_map *map,
> >   #define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) ({ 0; })
> >   #define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(atype, major, minor,
> access) ({ 0; })
> >   #define
> BPF_CGROUP_RUN_PROG_SYSCTL(head,table,write,buf,count,pos) ({ 0; })
> > -#define BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen) ({ 0; })
> > +#define BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen, enabled) ({ 0; })
> >   #define BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock, level, optname,
> optval, \
> > -       optlen, max_optlen, retval) ({ retval; })
> > +       optlen, max_optlen, retval, \
> > +       enabled) ({ retval; })
> >   #define BPF_CGROUP_RUN_PROG_GETSOCKOPT_KERN(sock, level, optname,
> optval, \
> >       optlen, retval) ({ retval; })
> >   #define BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock, level, optname,
> optval, optlen, \
> > diff --git a/net/socket.c b/net/socket.c
> > index fcbdd5bc47ac..0b465dc8a789 100644
> > --- a/net/socket.c
> > +++ b/net/socket.c
> > @@ -2363,6 +2363,7 @@ int do_sock_getsockopt(struct socket *sock,
> bool compat, int level,
> >          int optname, sockptr_t optval, sockptr_t optlen)
> >   {
> >   int max_optlen __maybe_unused;
> > +bool enabled __maybe_unused;
> >   const struct proto_ops *ops;
> >   int err;
> >   
> > @@ -2371,7 +2372,7 @@ int do_sock_getsockopt(struct socket *sock,
> bool compat, int level,
> >   return err;
> >   
> >   if (!compat)
> > -max_optlen = BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen);
> > +max_optlen = BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen, enabled);
> 
> Here, 'enabled' is actually assigned with a value in the macro. I am
> not sure
> whether this is a common practice or not. At least from macro, it is
> not clear
> about this.
> 
> Maybe we can do
> max_optlen = BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen, &enabled);
> 
> The &enabled signals that its value could change. And indeed
> the macro will store the proper value to &enabled properly.
> 
> Just my 2 cents.
> 
Thanks for the suggestion.
Will take the suggestion in v5 if this patch is truely needed,
looks like this patch could possibly lead to regression issue.


> >   
> >   ops = READ_ONCE(sock->ops);
> >   if (level == SOL_SOCKET) {
> > @@ -2390,7 +2391,7 @@ int do_sock_getsockopt(struct socket *sock,
> bool compat, int level,
> >   if (!compat)
> >   err = BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock->sk, level, optname,
> >        optval, optlen, max_optlen,
> > -     err);
> > +     err, enabled);
> >   
> >   return err;
> >   }

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ