From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8D08FFA373D for ; Thu, 27 Oct 2022 23:46:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235691AbiJ0Xqs (ORCPT ); Thu, 27 Oct 2022 19:46:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235686AbiJ0Xqr (ORCPT ); Thu, 27 Oct 2022 19:46:47 -0400 Received: from out2.migadu.com (out2.migadu.com [IPv6:2001:41d0:2:aacc::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D450E22BD4 for ; Thu, 27 Oct 2022 16:46:46 -0700 (PDT) Message-ID: <4dbbbdf1-0253-9101-1fc7-a4685970f4d7@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1666914405; h=from:from:reply-to:subject:subject: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=wFxsJeRTl6qyUpqbWNEYpaZl8EK/rivJAX6WfMjG7aA=; b=rYPBn9BWXqlaALMtBB7BsanzArxjtQRqB6gfBhqBZt1jShRy90SGKBxkC6nqfyi6r9pddF YhZ/G0Cns0vAVJkXTsBfYDeMizSZ4OL5Ok9spF6ZysJRx6dDLqqkmt0NQgNqGJFbvRB1/0 f+ixPQ2K++87N6CCTOH8Ax/re9gxgy0= Date: Thu, 27 Oct 2022 16:46:42 -0700 MIME-Version: 1.0 Subject: Re: [Question]: BPF_CGROUP_{GET,SET}SOCKOPT handling when optlen > PAGE_SIZE Content-Language: en-US To: Andrii Nakryiko Cc: Stanislav Fomichev , bpf References: <5c8b7d59-1f28-2284-f7b9-49d946f2e982@linux.dev> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Martin KaFai Lau In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org On 10/27/22 1:46 PM, Andrii Nakryiko wrote: > On Wed, Oct 26, 2022 at 6:17 PM Martin KaFai Lau wrote: >> >> The cgroup-bpf {get,set}sockopt prog is useful to change the optname behavior. >> The bpf prog usually just handles a few specific optnames and ignores most >> others. For the optnames that it ignores, it usually does not need to change >> the optlen. The exception is when optlen > PAGE_SIZE (or optval_end - optval). >> The bpf prog needs to set the optlen to 0 for this case or else the kernel will >> return -EFAULT to the userspace. It is usually not what the bpf prog wants >> because the bpf prog only expects error returning to userspace when it has >> explicitly 'return 0;' or used bpf_set_retval(). If a bpf prog always changes >> optlen for optnames that it does not care to 0, it may risk if the latter bpf >> prog in the same cgroup may want to change/look-at it. >> >> Would like to explore if there is an easier way for the bpf prog to handle it. >> eg. does it make sense to track if the bpf prog has changed the ctx->optlen >> before returning -EFAULT to the user space when ctx.optlen > max_optlen? > > Maybe optlen + optval/optval_end could be replaced with dynptr? If we > do a new type of dynptr (DYNPTR_CTXBUF or something like that), we can > implement tracking of whether it was ever modified through > bpf_dynptr_write() or if direct memory access was ever used (was > bpf_dynptr_data() called). Not sure how you'd know if > bpf_dynptr_data() was used to modify data, though (this is where > bpf_dynptr_data_rdonly() vs bpf_dynptr_data() would be helpful, > perhaps). But just a seed of an idea, maybe you guys can somehow fit > it here? Yep, dynptr can be used here. May be one dynptr specifically for sockptr_t. The dynptr will have the __user pointer first to avoid a big (and potentially bogus) kernel buffer pre allocation. Then only read and write it through the bpf_dynptr_read() and write(). Since it is __user pointer, it won't be directly accessible through data slice with bpf_dynptr_data(). It should not be a perf issue, most of the common optval is an integer. The worst common case is the 16 bytes tcp-cc name. The bpf prog has to be sleepable first though which I think will be a useful thing to have.