Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp1398623pxb; Fri, 13 Nov 2020 11:36:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJzE0kXeP+wX0GWQpkES/Zh7Dbdyvl9G2J5yEvEIk5o5B0FS+9Im+HhSjN2sJUPfvihi6ueZ X-Received: by 2002:aa7:c2d7:: with SMTP id m23mr4128810edp.230.1605296160337; Fri, 13 Nov 2020 11:36:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605296160; cv=none; d=google.com; s=arc-20160816; b=t/nAuYa3AZUdnJ3fxELpAswcnAGMR3eK7I5bmZ3zy6Ux1OUUid45O6U121jB3QU9A1 0EC8jDQTmBdCSg0lE4GnhYPBrtvV7ZZTkaRXbom7iREAfg+kVTl+wuF4H48ZJ3+810f9 0vsSRZ8yFWTV0qP3EQ/pye3Lzx3/JIokGr4kFwBUTyhY1AFWbBQJRj87jtRXeYPDmLfY Kkr+5frSah4EqGGCSvu4t6gvWPrtLlBM52tqdO84FiNcrEExRTiLLkK8wOSWc/TEnSzi Dz4zFkG8cMLOfaoOqaS0/+QlxiROMW7qLEZTk5jn+sTGE1wJR7D5GeEPAe9gEAmP22yR cM5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=kSxNzIRXq2hQf++iAIuMzFXpOxmVx5Tx4jehTl6QJNI=; b=jj9CtXa3fUpeOsi38GeifPtnMUrIME1k7QFplHumhIQ0miz680EcqKLVG7sVzkepmP 81EhxOSjUFS8r8Z+zSbFLZlgbOnYsaFahVWm8Mlb12XBzFCIROOxOkYGZTwLS4WahiTA CiTAzje2XN4zXFipzlwb+7pfGRjaAnXiTbu/o2Apx9GvIok7NaIJkge1o4Avb2frF5Me roMMMRIJKsAsFk+lljt3I7jZsIhiv3ftcLbnK2TQCJhbHlUAySo6CyRtQLy3bewrLz4K w9iwhUwq37+q/J8sA5VWrxOkzO7Oc5HcuO25zLuEud/rY8lu5Chvqz+fBzH2cPPzo6VO BQWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=ZQII+Ulq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k12si6862607eds.191.2020.11.13.11.35.35; Fri, 13 Nov 2020 11:36:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=ZQII+Ulq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726156AbgKMTaI (ORCPT + 99 others); Fri, 13 Nov 2020 14:30:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726003AbgKMTaI (ORCPT ); Fri, 13 Nov 2020 14:30:08 -0500 Received: from mail-lj1-x243.google.com (mail-lj1-x243.google.com [IPv6:2a00:1450:4864:20::243]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 934A4C0617A6 for ; Fri, 13 Nov 2020 11:30:06 -0800 (PST) Received: by mail-lj1-x243.google.com with SMTP id v20so12119608ljk.8 for ; Fri, 13 Nov 2020 11:30:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=kSxNzIRXq2hQf++iAIuMzFXpOxmVx5Tx4jehTl6QJNI=; b=ZQII+Ulq7va1iJz0uKz+Z7hnZH8qARd99V8Do3e4eotny4nigLldBALMq6FH4lN4Qn kL5Ndq56lqCg+PeXvwgLb5ngyslCFgjPXecUcCCmuHrlQTPYkg3DoLeoBR3C56mr3Dny tDGvFHyGHz+YvFUfBrXkbhprIdhyLZ97ngD3M= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=kSxNzIRXq2hQf++iAIuMzFXpOxmVx5Tx4jehTl6QJNI=; b=kNKd/ZFY5YLjRoiJ6Ijwe5LvqUj1Jl+9A2+kYCqrpJKnszRwhyRM7C1OwFTl0wMoGG 2RXLskFJ7lkiPNZglzolcy0Jnfke3i7Cb1Wxp51A054uIk3UybykbnvT4YKT69bsk/0u Rv1Z2uJ5sZT+Qy5VEta2dCW/lqswvBIYVi6KvcTvlHIs7fhTgJNrZD85hqaVbPSW73rf nCTSt/E3ZuRlYu680hZbblTc9sqJ2W13iNmhSOGSUmqCh9b/RUoJ7qR9Su8DkmLtBPuL yDCx9bBPBTp7HoygUkaq21OQ4sA9k8AHwLxFJ6gHDK/BuhKSZ0VuqFUeTL6kFQyYWJPF X/zg== X-Gm-Message-State: AOAM532feWOw6SX8//4db8KuvQuOpY+xmgFQEwZHiy9N4XaQcCNKGQpC Xpz9lvjWxpuw7ChhtG3muytfCDJZ2KlL4Q== X-Received: by 2002:a2e:92d5:: with SMTP id k21mr1626176ljh.244.1605295804577; Fri, 13 Nov 2020 11:30:04 -0800 (PST) Received: from mail-lf1-f53.google.com (mail-lf1-f53.google.com. [209.85.167.53]) by smtp.gmail.com with ESMTPSA id e9sm1729985ljp.119.2020.11.13.11.30.03 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 13 Nov 2020 11:30:03 -0800 (PST) Received: by mail-lf1-f53.google.com with SMTP id a9so14611949lfh.2 for ; Fri, 13 Nov 2020 11:30:03 -0800 (PST) X-Received: by 2002:a19:f243:: with SMTP id d3mr1336702lfk.534.1605295802640; Fri, 13 Nov 2020 11:30:02 -0800 (PST) MIME-Version: 1.0 References: <20201113170338.3uxdgb4yl55dgto5@ast-mbp> <20201113191751.rwgv2gyw5dblhe3j@ast-mbp> In-Reply-To: <20201113191751.rwgv2gyw5dblhe3j@ast-mbp> From: Linus Torvalds Date: Fri, 13 Nov 2020 11:29:46 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH bpf v5 1/2] lib/strncpy_from_user.c: Don't overcopy bytes after NUL terminator To: Alexei Starovoitov Cc: Daniel Xu , bpf , Linux Kernel Mailing List , Alexei Starovoitov , Daniel Borkmann , kernel-team@fb.com Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Nov 13, 2020 at 11:17 AM Alexei Starovoitov wrote: > > You misunderstood. > BPF side does not depend on zero padding. > The destination buffer was already initialized with zeros before the call. > What BPF didn't expect is strncpy_from_user() copying extra garbage after NUL byte. BPF made the wrong expectation. Those bytes are not defined, and it's faster the way it is written. Nobody else cares. BPF needs to fix it's usage. It really is that simple. strncpy_from_user() is one of the hottest functions in the whole kernel (under certain not very uncommon loads), and it's been optimized for performance. You told it that the destination buffer was some amount of bytes, and strncpy_from_user() will use up to that maximum number of bytes. That's the only guarantee you have - it won't write _past_ the buffer you gave it. The fact that you then use the string not as a string, but as something else, that's why *you* need to change your code. Linus