Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp1649146rwb; Wed, 26 Jul 2023 16:27:07 -0700 (PDT) X-Google-Smtp-Source: APBJJlEulJO1MWD+1OC5Opfm9VEgaCZKcs2ORz0SXVYifT9fB35oDRLAag9r47HczvFMlUacf/gM X-Received: by 2002:a05:6512:3ca4:b0:4fb:ca59:42d7 with SMTP id h36-20020a0565123ca400b004fbca5942d7mr512186lfv.33.1690414027350; Wed, 26 Jul 2023 16:27:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690414027; cv=none; d=google.com; s=arc-20160816; b=WK0TefT0O0drMRyAfw+2iWLEIW3WEXqgTanY5c7K4gcN69C+LRNLw2cNl+RguHjoOa 6fnO50KgaUk6Wp0hm6S8XFCTN0Kqi5jgbapOd3DagOWZkY0rjZBtBef4DMAydyCq//03 J+eT9RUDrU6a9qewQzu/TV/u8/5eLHr3gvIF9vNa2v+Z9IYHwP13N/IBxniCWg3pTcb/ CD8kaiLIRs07O/cieeSV+1rv3AUQHr4rpMmuS7ECv2UlkZrmQoTVGbcxsaDThW5Z6Xuu QD3dgT2N6tL6xhE6RIyQmjOvlbdC67+IO/uJa11asLzUgARw3GMZidhVQ5tMyuhT8gJL lpdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=MaG/2UBeePq8inaENT+NwKjpjU5MiahEk7iN9th9S0Y=; fh=Hz4yl7TPwARxGDakZxZkzY/t2vfiA27YYF3A3qy8Ptw=; b=FpKCHy9DZjopP7wCvgYSAvsvq3s5ARcFPuxAy6y1Yhn1uh8KgcjwdIWc4C59/+g4mX cGtzMJufD/0jBKnQAojbVA4AIhrGUzNqTNaEx5YHN2BB7jhQtHclylX1M5QXrsxVuFwM 7ytKclkze4ndi5+vnMt9YfPgHuOT+v9FgGyargzdxyC7cMQgg1Ie6ZmopRx0/O2rtcHO bbXT+S+8F/BWF+DnchmxLGkWZ5Q5HVANf2kR2gSE/cJiuv9Hsrqo6xNekXYS+lGsNWZY vPFBjTYDboKRo4t/ZcUNkyxEFbu1aF0pYzbVkw9Q1QmNgI7fl7nKDi68o000yRE4frJH zd+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="ko8WGR+/"; 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=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a19-20020aa7cf13000000b005223ebc697dsi3735355edy.585.2023.07.26.16.26.41; Wed, 26 Jul 2023 16:27:07 -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=@chromium.org header.s=google header.b="ko8WGR+/"; 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=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230221AbjGZWnV (ORCPT + 99 others); Wed, 26 Jul 2023 18:43:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38554 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229866AbjGZWnU (ORCPT ); Wed, 26 Jul 2023 18:43:20 -0400 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28084270F for ; Wed, 26 Jul 2023 15:43:19 -0700 (PDT) Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1bbd2761f1bso2468965ad.2 for ; Wed, 26 Jul 2023 15:43:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1690411398; x=1691016198; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=MaG/2UBeePq8inaENT+NwKjpjU5MiahEk7iN9th9S0Y=; b=ko8WGR+/taNUp8J6OqypMcS4PobhQCefoY9ZnAMyqN+swRHLtYzA3J3I6EFZHH5JLf 2ScAKum79L0DXHoNKzy90xTh8rpFYEVm3NZAzExvQsBq6s4uVgOCVHJVgTmMPpxSLI4p 6/R7YD7r11qvwTQRy55tAwQB/oLhjaO8JkmQA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690411398; x=1691016198; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=MaG/2UBeePq8inaENT+NwKjpjU5MiahEk7iN9th9S0Y=; b=mIfB9KnDc9vJQ6q+UcUfmkEe5YwsGHpqScGsfFm7Pm4NU8BN1SM6wBmp2AUI5hF7xN /u/KZHg+aicYFGuZpHIqcHT/rj0VdqS0BSinTeFqmU3/02TEaJ8v1Zr3y/R/nUtlvhHx WG0/d332Jrel/EkDTGN/P8+JnrqALEjUFRUCpvjbWsM2TPYRAWDrJJ4f1PLt2Z0160Dw Iycorq1MhO3ArTZbgOyYrC5r0Fv8I8FqFJ5988Bpp+xzUzvTHFv3K0HbNTvPFKQgHsuM vU93JQTcoTz8wA00eXB2sN9ewcj0TM1zzmmvTeDktFMuJjSgCSVCA/0wKrVn/YHuxhzF sbNA== X-Gm-Message-State: ABy/qLbwwRy8kFnCz9I1cRHmsmaKtaz/e2jIxNpsSIfFqOTq8lqgaFVl Li+1qUztVFzveBLycAJJBuiR1Q== X-Received: by 2002:a17:903:442:b0:1bb:98de:251e with SMTP id iw2-20020a170903044200b001bb98de251emr3120883plb.56.1690411398639; Wed, 26 Jul 2023 15:43:18 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id n15-20020a170902d2cf00b001b54a88e6adsm39153plc.309.2023.07.26.15.43.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jul 2023 15:43:18 -0700 (PDT) Date: Wed, 26 Jul 2023 15:43:17 -0700 From: Kees Cook To: Arnd Bergmann Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Steven Rostedt , Masami Hiramatsu , Luis Chamberlain , Arnd Bergmann , John Fastabend , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mark Rutland , Petr Mladek , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-modules@vger.kernel.org Subject: Re: [PATCH] [v3] kallsyms: rework symbol lookup return codes Message-ID: <202307261536.797610DC@keescook> References: <20230726141333.3992790-1-arnd@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230726141333.3992790-1-arnd@kernel.org> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 Wed, Jul 26, 2023 at 04:12:23PM +0200, Arnd Bergmann wrote: > From: Arnd Bergmann > > Building with W=1 in some configurations produces a false positive > warning for kallsyms: > > kernel/kallsyms.c: In function '__sprint_symbol.isra': > kernel/kallsyms.c:503:17: error: 'strcpy' source argument is the same as destination [-Werror=restrict] > 503 | strcpy(buffer, name); > | ^~~~~~~~~~~~~~~~~~~~ > > This originally showed up while building with -O3, but later started > happening in other configurations as well, depending on inlining > decisions. The underlying issue is that the local 'name' variable is > always initialized to the be the same as 'buffer' in the called functions > that fill the buffer, which gcc notices while inlining, though it could > see that the address check always skips the copy. > > The calling conventions here are rather unusual, as all of the internal > lookup functions (bpf_address_lookup, ftrace_mod_address_lookup, > ftrace_func_address_lookup, module_address_lookup and > kallsyms_lookup_buildid) already use the provided buffer and either return > the address of that buffer to indicate success, or NULL for failure, > but the callers are written to also expect an arbitrary other buffer > to be returned. > > Rework the calling conventions to return the length of the filled buffer > instead of its address, which is simpler and easier to follow as well > as avoiding the warning. Leave only the kallsyms_lookup() calling conventions > unchanged, since that is called from 16 different functions and > adapting this would be a much bigger change. > > Link: https://lore.kernel.org/all/20200107214042.855757-1-arnd@arndb.de/ > Reviewed-by: Luis Chamberlain > Signed-off-by: Arnd Bergmann > --- > v3: use strscpy() instead of strlcpy() Thank you! :) (Though see my notes below...) > [...] > @@ -344,13 +345,12 @@ const char *module_address_lookup(unsigned long addr, > #endif > } > > - ret = find_kallsyms_symbol(mod, addr, size, offset); > - } > - /* Make a copy in here where it's safe */ > - if (ret) { > - strncpy(namebuf, ret, KSYM_NAME_LEN - 1); This -1 was to keep the buffer NUL-terminated. > - ret = namebuf; > + sym = find_kallsyms_symbol(mod, addr, size, offset); > + > + if (sym) > + ret = strscpy(namebuf, sym, KSYM_NAME_LEN - 1); This strscpy should use KSYM_NAME_LEN without the "- 1". > } > + > preempt_enable(); > -Kees -- Kees Cook