Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp26236rwd; Tue, 30 May 2023 15:36:10 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5QsVFkMpCfGodrzHUFmgE9NIdfp0ZJ4S2aKlC3brxJFGC7lsolQRgtjo3SmQKHF3ocseYo X-Received: by 2002:a17:90a:c982:b0:256:6b1c:f5e9 with SMTP id w2-20020a17090ac98200b002566b1cf5e9mr3472702pjt.42.1685486170445; Tue, 30 May 2023 15:36:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685486170; cv=none; d=google.com; s=arc-20160816; b=Gv/BbLxbB04RqsXPkSo2DKzvlR7xVL0OHyJOPuu8sPNJERzfVRhBC17BJfO8UAipsu 3fgwuQrSqouoQt/XSttuFgJQQMPao8laPLLpud7PbBMjMbPP4MM+KFO1ABP5XoXn5sX6 WeVQ3FXh2KoiEvPjpZcxqg4L7ukNwHfl5AAIeaTPbFv7B9X/4SJfKArb9teEGupZ2hCY nlwX5xaGlmInjtaSJvf7SVcJS1VNgAV24l0t/m73OSJEU51ars/qXnjojlXNSSBsJVkr vXUrqstg1OJQhLCt5R5hIxSQ3RXrUvoIsXqgAgwbrNLaZ83MaNUTg7aA5EkQETV7PY9b jE7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:references:in-reply-to:subject :cc:to:from:mime-version:content-transfer-encoding:dkim-signature :dkim-signature; bh=18j5tvDaSV7ASsBeBkyrZ2rlsJmZg7DSojVFhv89dpY=; b=YGy+jrI3t+372Az27P2ri2J8Qjg1pELMVQWkle30aYR1A9E3gA9AphAnXIyDMyYTMr aHry0s9UMuLmV9balRLRo9R79rdQ2LETNczNvboH/5R9Q+5lHkgaBVpEqGVxPMJCVH/s WR9J6as/1D/MfZvd70yubvGdn8l+RkTWWttVo6a4doovlMyVLucFM3ZkO7tr5sxBqATY YkeRVD5WSuweUJ7OvOG6pnVIXLCIQG5RmVXsEPmQ20wqBWHyubVC9Hk8VUmfkZ6Sc3LI 796+5XdyWzEtjrbdKvzemyO1LWOU/WdQJfMuETTI/DvOReR80EhCW4TVwWqF+sEYaW2V IV1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=wehyXkwR; dkim=neutral (no key) header.i=@suse.de; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 9-20020a17090a004900b00250c48910bdsi8384583pjb.70.2023.05.30.15.35.53; Tue, 30 May 2023 15:36:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=wehyXkwR; dkim=neutral (no key) header.i=@suse.de; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233725AbjE3W2L (ORCPT + 99 others); Tue, 30 May 2023 18:28:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233745AbjE3W2I (ORCPT ); Tue, 30 May 2023 18:28:08 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7FA1FF7; Tue, 30 May 2023 15:27:52 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id ED0192187B; Tue, 30 May 2023 22:27:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1685485670; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=18j5tvDaSV7ASsBeBkyrZ2rlsJmZg7DSojVFhv89dpY=; b=wehyXkwRpGMJYML5r49f7V+U/SzLmqJ/KAVFN7Xq+OjsdqBPJize1K2t44Nzy5DRxVpg6O 20iLwewLOp05gvuvmILokAfctIoWmf+H5Xykz1w3ePQz6ACKcSI/ZJXfYaCg7OmQ8iwNa8 idvYVycOqq67drFpxBnTwUbBTGRMyyg= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1685485670; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=18j5tvDaSV7ASsBeBkyrZ2rlsJmZg7DSojVFhv89dpY=; b=FZ8v9KB0G9DsDm6MV0OJE9QBBCpcRZhcMVQCwb6Xx9X9YL78StXG1gCuT+xmaCDl5pKl9S TIh4+NpA6NzIJqBQ== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 7D50E13478; Tue, 30 May 2023 22:27:47 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id PgUxDGN4dmS5VgAAMHmgww (envelope-from ); Tue, 30 May 2023 22:27:47 +0000 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 From: "NeilBrown" To: "Dan Carpenter" Cc: "Stanislav Kinsbursky" , "Chuck Lever" , "Jeff Layton" , "Trond Myklebust" , "Anna Schumaker" , "J. Bruce Fields" , linux-nfs@vger.kernel.org, netdev@vger.kernel.org, kernel-janitors@vger.kernel.org Subject: Re: [PATCH] nfsd: fix double fget() bug in __write_ports_addfd() In-reply-to: <9c90e813-c7fb-4c90-b52b-131481640a78@kili.mountain> References: <9c90e813-c7fb-4c90-b52b-131481640a78@kili.mountain> Date: Wed, 31 May 2023 08:27:43 +1000 Message-id: <168548566376.23533.14778348024215909777@noble.neil.brown.name> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org On Mon, 29 May 2023, Dan Carpenter wrote: > The bug here is that you cannot rely on getting the same socket > from multiple calls to fget() because userspace can influence > that. This is a kind of double fetch bug. >=20 > The fix is to delete the svc_alien_sock() function and insted do > the checking inside the svc_addsock() function. Hi, I definitely agree with the change to pass the 'net' into svc_addsock(), and check the the fd has the correct net. I'm not sure I agree with the removal of the svc_alien_sock() test. It is best to perform sanity tests before allocation things, and nfsd_create_serv() can create a new 'serv' - though most often it just incs the refcount. Maybe instead svc_alien_sock() could return the struct socket (if successful), and it could be passed to svc_addsock()??? I would probably then change the name of svc_alien_sock() Thanks, NeilBrown >=20 > Fixes: 3064639423c4 ("nfsd: check passed socket's net matches NFSd superblo= ck's one") > Signed-off-by: Dan Carpenter > --- > Based on static analysis and untested. This goes through the NFS tree.=20 > Inspired by CVE-2023-1838. >=20 > include/linux/sunrpc/svcsock.h | 7 +++---- > fs/nfsd/nfsctl.c | 7 +------ > net/sunrpc/svcsock.c | 23 +++++------------------ > 3 files changed, 9 insertions(+), 28 deletions(-) >=20 > diff --git a/include/linux/sunrpc/svcsock.h b/include/linux/sunrpc/svcsock.h > index d16ae621782c..a7116048a4d4 100644 > --- a/include/linux/sunrpc/svcsock.h > +++ b/include/linux/sunrpc/svcsock.h > @@ -61,10 +61,9 @@ int svc_recv(struct svc_rqst *, long); > void svc_send(struct svc_rqst *rqstp); > void svc_drop(struct svc_rqst *); > void svc_sock_update_bufs(struct svc_serv *serv); > -bool svc_alien_sock(struct net *net, int fd); > -int svc_addsock(struct svc_serv *serv, const int fd, > - char *name_return, const size_t len, > - const struct cred *cred); > +int svc_addsock(struct svc_serv *serv, struct net *net, > + const int fd, char *name_return, const size_t len, > + const struct cred *cred); > void svc_init_xprt_sock(void); > void svc_cleanup_xprt_sock(void); > struct svc_xprt *svc_sock_create(struct svc_serv *serv, int prot); > diff --git a/fs/nfsd/nfsctl.c b/fs/nfsd/nfsctl.c > index e0e98b40a6e5..1489e0b703b4 100644 > --- a/fs/nfsd/nfsctl.c > +++ b/fs/nfsd/nfsctl.c > @@ -698,16 +698,11 @@ static ssize_t __write_ports_addfd(char *buf, struct = net *net, const struct cred > return -EINVAL; > trace_nfsd_ctl_ports_addfd(net, fd); > =20 > - if (svc_alien_sock(net, fd)) { > - printk(KERN_ERR "%s: socket net is different to NFSd's one\n", __func__); > - return -EINVAL; > - } > - > err =3D nfsd_create_serv(net); > if (err !=3D 0) > return err; > =20 > - err =3D svc_addsock(nn->nfsd_serv, fd, buf, SIMPLE_TRANSACTION_LIMIT, cre= d); > + err =3D svc_addsock(nn->nfsd_serv, net, fd, buf, SIMPLE_TRANSACTION_LIMIT= , cred); > =20 > if (err >=3D 0 && > !nn->nfsd_serv->sv_nrthreads && !xchg(&nn->keep_active, 1)) > diff --git a/net/sunrpc/svcsock.c b/net/sunrpc/svcsock.c > index 46845cb6465d..e4184e40793c 100644 > --- a/net/sunrpc/svcsock.c > +++ b/net/sunrpc/svcsock.c > @@ -1474,22 +1474,6 @@ static struct svc_sock *svc_setup_socket(struct svc_= serv *serv, > return svsk; > } > =20 > -bool svc_alien_sock(struct net *net, int fd) > -{ > - int err; > - struct socket *sock =3D sockfd_lookup(fd, &err); > - bool ret =3D false; > - > - if (!sock) > - goto out; > - if (sock_net(sock->sk) !=3D net) > - ret =3D true; > - sockfd_put(sock); > -out: > - return ret; > -} > -EXPORT_SYMBOL_GPL(svc_alien_sock); > - > /** > * svc_addsock - add a listener socket to an RPC service > * @serv: pointer to RPC service to which to add a new listener > @@ -1502,8 +1486,8 @@ EXPORT_SYMBOL_GPL(svc_alien_sock); > * Name is terminated with '\n'. On error, returns a negative errno > * value. > */ > -int svc_addsock(struct svc_serv *serv, const int fd, char *name_return, > - const size_t len, const struct cred *cred) > +int svc_addsock(struct svc_serv *serv, struct net *net, const int fd, > + char *name_return, const size_t len, const struct cred *cred) > { > int err =3D 0; > struct socket *so =3D sockfd_lookup(fd, &err); > @@ -1514,6 +1498,9 @@ int svc_addsock(struct svc_serv *serv, const int fd, = char *name_return, > =20 > if (!so) > return err; > + err =3D -EINVAL; > + if (sock_net(so->sk) !=3D net) > + goto out; > err =3D -EAFNOSUPPORT; > if ((so->sk->sk_family !=3D PF_INET) && (so->sk->sk_family !=3D PF_INET6)) > goto out; > --=20 > 2.39.2 >=20 >=20