Received: by 2002:a05:6358:701b:b0:131:369:b2a3 with SMTP id 27csp1688824rwo; Sat, 22 Jul 2023 21:04:20 -0700 (PDT) X-Google-Smtp-Source: APBJJlEdt3g99KkAeQ+mMx8i1RxKwY43+PSj67DC1VXseeN4DQ1XIiOXOzHP8Ts+D21GkXtl5URo X-Received: by 2002:a05:6402:1397:b0:51e:52bd:68c8 with SMTP id b23-20020a056402139700b0051e52bd68c8mr5607463edv.14.1690085060478; Sat, 22 Jul 2023 21:04:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690085060; cv=none; d=google.com; s=arc-20160816; b=EsAMjh9ws6TJahd8eaI6ZwMXoP++M1Szxhe7PRFf4cE2VIkbBgDsORHaXd9y23EkhY JGcuwtCKK1koFP9+5VL3SEOTFlIiZoyN5JAbZniKOpp6CTgtMou32QCE3Hh9Vr3PJWP5 D8sD2eBJewojZCnyndiDxRkgt7Hr9svB03WBFrTPpmyqjmLzcMTQ/EDhed7VQuowEtFB 3SxZUcYEDYsSHbO5sI35S/8drT4AZ4Q8UFUGwapYaXsLaS9PGbq6Lps1bQ/gKffoDQBd gr/i2025+n/jSY8prxGclZLbc+RzEFj/CnD58BleN84RMoIo63ltZvzigeZHFyky1jdK A8ng== 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=CxXqztj5OtaboYjZJG2RhBj3enmq0GMAVkQwjxk3WaM=; fh=XNfWOKP9ollpwu3QCmLQSzE417bgPMoW1xJl4PoPjJU=; b=XENlB+ZnQjvsRwH7YgaWwgNeDh+5KRG+uPnWDnh/e74NPgtdme8fClHVKiQ41mgixV ogzWhBpy0GW6F/xwCvGelJUKZFVNKjvQfV1JJSUU8ztlodD0pzM2rC2OLpJRmFv0lnrL 5l+d/0Lt1suQG28N5ucYN/1QKcKCwUbuY0ZqiREGTQWAp/+RqlIXQKvbnphdlPkwy2ec SNP9XgYSqJ5brN7GoShksqNrGwNJvaFAVO3mJydDxRjD5O7DveNpvuq2oM2gRJRM2KXV z8C2+Iu2nKlNlnDBDTztvndSiQZFlUS6jHSIsjHkddQZffuoN0ohw9nUunNBoDubazFl l5/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=KzmI0GVD; 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 o11-20020aa7d3cb000000b0051a4adabcb5si4761829edr.618.2023.07.22.21.03.56; Sat, 22 Jul 2023 21:04:20 -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=KzmI0GVD; 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 S229750AbjGWCCp (ORCPT + 99 others); Sat, 22 Jul 2023 22:02:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229476AbjGWCCn (ORCPT ); Sat, 22 Jul 2023 22:02:43 -0400 Received: from mail-lj1-x22b.google.com (mail-lj1-x22b.google.com [IPv6:2a00:1450:4864:20::22b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9E868119; Sat, 22 Jul 2023 19:02:42 -0700 (PDT) Received: by mail-lj1-x22b.google.com with SMTP id 38308e7fff4ca-2b961822512so46494171fa.2; Sat, 22 Jul 2023 19:02:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690077761; x=1690682561; 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=CxXqztj5OtaboYjZJG2RhBj3enmq0GMAVkQwjxk3WaM=; b=KzmI0GVDBAVsb+2SsO5TF0mSZnnJB9lI55c9iArQ7WqfJtwhG0Y53aJp6aYCqQrenY 7w6K1l3hUJpYBUI2na591nMv5ukdZXBX0wTQR69L75a1MlemSZ1PJN78gM9xg+A4BneO SDa5q1spEGrwRCVFhwb6ENzdriiMJltHJpGu896p0Zcl+7N2bn8owTQEousVkhZrVqTT VcqgH/qkI+XqZ5NPkcaGYf39SaOXHN/WOFZfvkA5O1Qs8LamYoSvN/dAYZMwAIeKXweM seN4KjzldN/0lrnL51NOV4KA6AoZtm4p1v21gx9yEd6eLrRvwA9q6V5D6+PtijPH668i pfgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690077761; x=1690682561; 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=CxXqztj5OtaboYjZJG2RhBj3enmq0GMAVkQwjxk3WaM=; b=gh8Vd3MMLua80cBe3N7cjYU8Q1S1h7YnZ+r4Z1uqFomU3IdbXR1ov6yLZLkgob0wLR lmdgiK7JbrPraBWJePkuexlPbL5J43j0cB10yD7NTSMTJjCN1Zlw4vzOze2k22u2EIdu /btQS3fZg/FP5peVnY5utNnkxuiOl11miyY4BErqSS6FthikruE7VrbPUXxusbwFCXKx EUYWmQvXI9GlH34D+cs3VQcXiG9eDRRmONeD5e7voA2eXi7fzW83oAJjushGzfBeGz5r WJ4KeEOrYwD1jwIro67h06583UhZxrYpwgyjKSraIXiJCOIAwCUOnvhmeJkvHZ+7Tnb8 oezg== X-Gm-Message-State: ABy/qLbtg+tLj5hUbbveud3IunbnVIFKe0dp9YnTa1vVQQvaCWTSXmFo i9ljCsxwj237UFGpOZkNfwt5xKoQp2S1OY6mXhw= X-Received: by 2002:a2e:9283:0:b0:2b6:eeb3:da94 with SMTP id d3-20020a2e9283000000b002b6eeb3da94mr3695037ljh.22.1690077760516; Sat, 22 Jul 2023 19:02:40 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Alexei Starovoitov Date: Sat, 22 Jul 2023 19:02:29 -0700 Message-ID: Subject: Re: bpf: bpf_probe_read_user_str() returns 0 for empty strings To: Bagas Sanjaya Cc: Ingo Molnar , Masami Hiramatsu , "Steven Rostedt (Google)" , Max Froehling , Linux Kernel Mailing List , Linux BPF , Linux Memory Management List 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 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 Sat, Jul 22, 2023 at 6:53=E2=80=AFPM Bagas Sanjaya wrote: > > Hi, > > I notice a bug report on Bugzilla [1]. Quoting from it: > > > Overview: > > > > From within eBPF, calling the helper function bpf_probe_read_user_str(v= oid *dst, __u32 size, const void *unsafe_ptr returns 0 when the source stri= ng (void *unsafe_ptr) consists of a string containing only a single null-by= te. > > > > This violates various functions documentations (the helper and various = internal kernel functions), which all state: Sounds like the bugzilla author believes it's a documentation issue. If so, please encourage the author to send the patch to fix the doc. > > > >> On success, the strictly positive length of the output string, > >> including the trailing NUL character. On error, a negative value. > > > > To me, this states that the function should return 1 for char myString[= ] =3D ""; However, this is not the case. The function returns 0 instead. > > > > For non-empty strings, it works as expected. For example, char myString= [] =3D "abc"; returns 4. > > > > Steps to Reproduce: > > * Write an eBPF program that calls bpf_probe_read_user_str(), using a u= serspace pointer pointing to an empty string. > > * Store the result value of that function > > * Do the same thing, but try out bpf_probe_read_kernel_str(), like this= : > > char empty[] =3D ""; > > char copy[5]; > > long ret =3D bpf_probe_read_kernel_str(copy, 5, empty); > > * Compare the return value of bpf_probe_read_user_str() and bpf_probe_r= ead_kernel_str() > > > > Expected Result: > > > > Both functions return 1 (because of the single NULL byte). > > > > Actual Result: > > > > bpf_probe_read_user_str() returns 0, while bpf_probe_read_kernel_str() = returns 1. > > > > Additional Information: > > > > I believe I can see the bug on the current Linux kernel master branch. > > > > In the file/function mm/maccess.c::strncpy_from_user_nofault() the help= er implementation calls strncpy_from_user(), which returns the length witho= ut trailing 0. Hence this function returns 0 for an empty string. > > > > However, in line 192 (as of commit fdf0eaf11452d72945af31804e2a1048ee1b= 574c) there is a check that only increments ret, if it is > 0. This appears= to be the logic that adds the trailing null byte. Since the check only doe= s this for a ret > 0, a ret of 0 remains at 0. > > > > This is a possible off-by-one error that might cause the behavior. > > See Bugzilla for the full thread. > > FYI, the culprit line is introduced by commit 3d7081822f7f9e ("uaccess: A= dd > non-pagefault user-space read functions"). I Cc: culprit SoB so that they > can look into this bug. > > Thanks. > > [1]: https://bugzilla.kernel.org/show_bug.cgi?id=3D217679 > > -- > An old man doll... just what I always wanted! - Clara >