Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp3277626rwd; Sat, 3 Jun 2023 02:06:30 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6TSvzowvXGAohA/VMAiwm+50UOOXfv+0xc6bLOSdGM21CTmZXkLBOD4VDUd18Gh+imD/MS X-Received: by 2002:a17:902:9344:b0:1b0:3a03:50d0 with SMTP id g4-20020a170902934400b001b03a0350d0mr2265084plp.26.1685783190049; Sat, 03 Jun 2023 02:06:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685783190; cv=none; d=google.com; s=arc-20160816; b=nvxEQFXrSmKE2MctXIURHizipVa/UXN4SEaCN4WZHImIsIAb+chycUT97BUZquwUzH mIAJ/BPjpKe4bkHDZzTrev2hefWYzr7SOSVOKc7E6Vb/2B0c5mpO9kZR8p9UJNXL+fWY PpU87irvpblSJdyS/4b8YkZX/J6VVkNVGnsfOeHcjjPdmqhMNAr8RJbrHW67Nu6kciwt OSytmDzbZs58NVbmC0i0FlJnjtqLA4tmy9BURidL8Teb9KbQAi1E/dslx7UFSRtCEO+H MsxeTltZoxfPIpnr0RFTWPKhrcsug59mSSFUTyHuOAYE/0SWtP7lvWU9S+0yhp2VT3a8 svsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=j8g2hnOGOg2sxpTnLUbSGzAiSvvgwyqIgGFTiGWjGU0=; b=lgdWB6ExsOufVwZseWG/9TmMCTi2JCcDKPjalp6YtflzLEp1Nq1meAfJXs0SSV1YyC dw/3H2se5htb9z3sr8ISxeQzVokoL8Q8SikW6+MLQpIVkBYbS6wszKl8yhlxm5JcUr1w Vy8+hpRX8yBejvSdn40Tm3bQJBDiBmIrDoh4Zo4YiFW8XBVw8eJUBhOJbQNn73lokynV 03rSvSUY3nb69jpSgdjuCD3itoE/OOMVHy9xTfy9fxHbOEdBO8We6DWyCU70cHjQ99zn hMNUlC37EAUQAgRZvimkwvp093W60sJchE/VQb/k0DBKMqWP0doQw5Wsx4NhLOQz/75o suyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=rk5jL5mS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j15-20020a170903024f00b001aaf91ae3acsi2268266plh.485.2023.06.03.02.06.17; Sat, 03 Jun 2023 02:06:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=@gmail.com header.s=20221208 header.b=rk5jL5mS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234858AbjFCIWb (ORCPT + 99 others); Sat, 3 Jun 2023 04:22:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60184 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229831AbjFCIWa (ORCPT ); Sat, 3 Jun 2023 04:22:30 -0400 Received: from mail-ua1-x92c.google.com (mail-ua1-x92c.google.com [IPv6:2607:f8b0:4864:20::92c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF81299; Sat, 3 Jun 2023 01:22:28 -0700 (PDT) Received: by mail-ua1-x92c.google.com with SMTP id a1e0cc1a2514c-7841f18f9f7so750588241.0; Sat, 03 Jun 2023 01:22:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685780548; x=1688372548; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=j8g2hnOGOg2sxpTnLUbSGzAiSvvgwyqIgGFTiGWjGU0=; b=rk5jL5mSq6M2gxVyXs4AUO7B65MmZArJzj3wGRXaSqEsGRuwpHAWF2E9/hMFbKk3c8 e4J5pa9SUHMps06XsQ+RxYXPuzuni8NHeYDSYI2xO/UQ49l2A5gS6/4jG3OFwBMYmE81 y79noa98hW4pqR2rjBCeCl8nPDi012PX9gVFQNQGk1UXVr4m4XmDvNUqYULKwOFf6Xar 2DBh/N6PETCeNBD9+r0VjTjxasPJFUVqVKsrvMFAPkd/Fs7Sx8Pn6rEs/ZTuD7nZ0HIY mfIQvZWI3I+JzOEx6t22cO64rgX3pncWQqcG2P/ECJzu0HB3GMWlTtGYLcMlUX/oO4Mc Bpnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685780548; x=1688372548; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=j8g2hnOGOg2sxpTnLUbSGzAiSvvgwyqIgGFTiGWjGU0=; b=RRKZ4/6zYB4JdsufrgTPGIPqUlUFPeNDjmjItfl49H8xQA1i+mH98Mduh/ZBj8t/57 ef2sAfzd/23oZpPZps+FTR9F3RlhbMAT5IfAcxc6KDJTu8GmlrWtVFOyZwvNzBqzejZ7 xer1a5jEdEvK0hW8+RDRNagCYmLSP+bgL/rWMkfwBbWLkUvi7aHRqgCnGm5l6o6EoIcV nAhdWCQQPiH1Ph1I6SRKrSekVPJ/BCVggfnmOPDDjvkjfRMmP4DXtZ0UR8Uie17Uueag BXIrHR0kwWGZAvpsZHuvEXgqOqjT2XZeLV+28av0/4bNmc5tp5fRDyNN8f0Gx8Ep+qfP OiQQ== X-Gm-Message-State: AC+VfDwwaAQ8NRkA7U1br8JHDNtz3ovjgFSmQWrBbbyH7LQUxK+6G7CK 1m3wwduG2mWBj7y5FRxCYe2Ny4A1Qm0EmcjklE8= X-Received: by 2002:a1f:6017:0:b0:461:479d:745d with SMTP id u23-20020a1f6017000000b00461479d745dmr985295vkb.8.1685780547644; Sat, 03 Jun 2023 01:22:27 -0700 (PDT) MIME-Version: 1.0 References: <20230602163044.1820619-1-leitao@debian.org> In-Reply-To: <20230602163044.1820619-1-leitao@debian.org> From: Willem de Bruijn Date: Sat, 3 Jun 2023 10:21:50 +0200 Message-ID: Subject: Re: [PATCH net-next v5] net: ioctl: Use kernel memory on protocol ioctl callbacks To: Breno Leitao Cc: Remi Denis-Courmont , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Alexander Aring , Stefan Schmidt , Miquel Raynal , David Ahern , Matthieu Baerts , Mat Martineau , Marcelo Ricardo Leitner , Xin Long , axboe@kernel.dk, asml.silence@gmail.com, leit@fb.com, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, dccp@vger.kernel.org, linux-wpan@vger.kernel.org, mptcp@lists.linux.dev, linux-sctp@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,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-kernel@vger.kernel.org On Fri, Jun 2, 2023 at 6:31=E2=80=AFPM Breno Leitao wro= te: > > Most of the ioctls to net protocols operates directly on userspace > argument (arg). Usually doing get_user()/put_user() directly in the > ioctl callback. This is not flexible, because it is hard to reuse these > functions without passing userspace buffers. > > Change the "struct proto" ioctls to avoid touching userspace memory and > operate on kernel buffers, i.e., all protocol's ioctl callbacks is > adapted to operate on a kernel memory other than on userspace (so, no > more {put,get}_user() and friends being called in the ioctl callback). > > This changes the "struct proto" ioctl format in the following way: > > int (*ioctl)(struct sock *sk, int cmd, > - unsigned long arg); > + int *karg); > > So, the "karg" argument, which is passed to the ioctl callback, is a > pointer allocated to kernel space memory (inside a function wrapper). > This buffer (karg) may contain input argument (copied from userspace in > a prep function) and it might return a value/buffer, which is copied > back to userspace if necessary. There is not one-size-fits-all format > (that is I am using 'may' above), but basically, there are three type of > ioctls: > > 1) Do not read from userspace, returns a result to userspace > 2) Read an input parameter from userspace, and does not return anything > to userspace > 3) Read an input from userspace, and return a buffer to userspace. > > The default case (1) (where no input parameter is given, and an "int" is > returned to userspace) encompasses more than 90% of the cases, but there > are two other exceptions. Here is a list of exceptions: > > * Protocol RAW: > * cmd =3D SIOCGETVIFCNT: > * input and output =3D struct sioc_vif_req > * cmd =3D SIOCGETSGCNT > * input and output =3D struct sioc_sg_req > * Explanation: for the SIOCGETVIFCNT case, userspace passes the input > argument, which is struct sioc_vif_req. Then the callback populates > the struct, which is copied back to userspace. > > * Protocol RAW6: > * cmd =3D SIOCGETMIFCNT_IN6 > * input and output =3D struct sioc_mif_req6 > * cmd =3D SIOCGETSGCNT_IN6 > * input and output =3D struct sioc_sg_req6 > > * Protocol PHONET: > * cmd =3D=3D SIOCPNADDRESOURCE | SIOCPNDELRESOURCE > * input int (4 bytes) > * Nothing is copied back to userspace. > > For the exception cases, functions sock_sk_ioctl_inout() will > copy the userspace input, and copy it back to kernel space. > > The wrapper that prepare the buffer and put the buffer back to user is > sk_ioctl(), so, instead of calling sk->sk_prot->ioctl(), the callee now > calls sk_ioctl(), which will handle all cases. > > Signed-off-by: Breno Leitao Please check the checkpatch output https://patchwork.hopto.org/static/nipa/753609/13265673/checkpatch/stdout > +static inline int phonet_sk_ioctl(struct sock *sk, unsigned int cmd, voi= d __user *arg) > +{ > + int karg; > + > + switch (cmd) { > + case SIOCPNADDRESOURCE: > + case SIOCPNDELRESOURCE: > + if (get_user(karg, (int __user *)arg)) > + return -EFAULT; > + > + return sk->sk_prot->ioctl(sk, cmd, &karg); > + } > + /* A positive return value means that the ioctl was not processed= */ > + return 1; > +} > #endif > +/* A wrapper around sock ioctls, which copies the data from userspace > + * (depending on the protocol/ioctl), and copies back the result to user= space. > + * The main motivation for this function is to pass kernel memory to the > + * protocol ioctl callbacks, instead of userspace memory. > + */ > +int sk_ioctl(struct sock *sk, unsigned int cmd, void __user *arg) > +{ > + int rc =3D 1; > + > + if (sk_is_ipmr(sk)) > + rc =3D ipmr_sk_ioctl(sk, cmd, arg); > + else if (sk_is_icmpv6(sk)) > + rc =3D ip6mr_sk_ioctl(sk, cmd, arg); > + else if (sk_is_phonet(sk)) > + rc =3D phonet_sk_ioctl(sk, cmd, arg); Does this handle all phonet ioctl cases correctly? Notably pn_socket_ioctl has a SIOCPNGETOBJECT that reads and writes a u16.