From patchwork Thu Mar 2 22:49:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 63628 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp112403wrd; Thu, 2 Mar 2023 15:02:42 -0800 (PST) X-Google-Smtp-Source: AK7set+LRbqEMdcwlh2SQWisU6igfig8dv+y09gbUSKJKC3a91I4OfsXib7vDkOtkPmehYwcz8GG X-Received: by 2002:a17:907:3f9e:b0:8aa:33c4:87d5 with SMTP id hr30-20020a1709073f9e00b008aa33c487d5mr15289054ejc.10.1677798162793; Thu, 02 Mar 2023 15:02:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677798162; cv=none; d=google.com; s=arc-20160816; b=oFkcHsgrb+XHDbigB7oMjmqUzMVikezUqMbma8uoaaUPyR9iL+cDcpE65KS9va3lrS xNefE8bqdbhQMVEGqbbtCZ5nSEOcHFKyR+rloFAlGyD/aR+tWLJdINmBnI+O2s2o0E3/ JwS1iZmbskzcVPQn6XCfYIKRS8zH32O/m2QUz/MwN2Qh3diL7TVsmpvUlnwpIO0xwgpy sxkngZ0zHLdVPhXwdV/ZQQyFe2ss+nAd18VTiSvIPdSwBiKuCsTX5A3BjZ7QqG5WpCqz RityErsWY5D3qr/cz6avY2V1eyjlefa+lDePgiKgAH0Pih+GzgE86jQ3fnFlx4Z6JDnZ 7uww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=87r3ryuddMp2n2OJgEocLi5hCbxjRwWfqUz8IuAFVl0=; b=lXbUAC7IHotkOZ3MwJWfgB2Of79jj47XKbJ4NODy7+YZQXPHxC47UnheJxEfxdr8Mv rHXOio9AIjil8a+6tgXH4h8a8qApeg4mUQ9MunAXm9KVuyBdqvgQWTJLLk12hGh4EGvk v1h1U0/1g/7e4TVRfWn62canORnCuCrhYefxqCY5aMKsKzgGlyFwRqDR++d+mQNtcnjG yXs8SaqzXAI38EpkNLKxRU2I/I3dht29CUAHRUdhxr5VcmieZ7ZiSJAvtXA0mYURxcHH fx/BAyYYQs5fni90cKb5GPsU2S+8IVs2G9dGZ8gkhIRhOfA6Dwb0rzoxJzxw4xIpF6Eh XAFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=KJYLa2os; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z22-20020aa7c656000000b004c0da1cf02dsi879290edr.105.2023.03.02.15.02.10; Thu, 02 Mar 2023 15:02:42 -0800 (PST) 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=KJYLa2os; 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 S230203AbjCBWt5 (ORCPT + 99 others); Thu, 2 Mar 2023 17:49:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230145AbjCBWtz (ORCPT ); Thu, 2 Mar 2023 17:49:55 -0500 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C77F23650 for ; Thu, 2 Mar 2023 14:49:53 -0800 (PST) Received: by mail-pl1-x632.google.com with SMTP id z2so823373plf.12 for ; Thu, 02 Mar 2023 14:49:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1677797393; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=87r3ryuddMp2n2OJgEocLi5hCbxjRwWfqUz8IuAFVl0=; b=KJYLa2os+yE0S+tQquXclEo5UjbB3+Ix9L7t+KQV3eWYy49UoX7lkm4pdsI0DCG2xP 1Zb5VEgb0JZwWKs5XjvOkNJmi2QFtZJxCjvVNdN4NIi6tT4yT1bRDmW6x7vkkY/W0SBp grxfEqQ+YXXSh+Uj6LgzHidfiHyt2I/MWu8XU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677797393; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=87r3ryuddMp2n2OJgEocLi5hCbxjRwWfqUz8IuAFVl0=; b=mEdO/xBlxVFCGnBGCBZcv/iwNqrUKpRg+3Vyv97mDLPnFO3aY/OS9GFvfqRi0JemX9 wItl8w2xdUKPNKU5W7cEVyclH4+PJ3XwPSxPcUYuB4lzPfUDPLP7uZIRs9N8SPtxw7Q/ hFQeqdytnyVtDfwZZw9uDRtt4iC6n2u6p2+oeUk59SVPdKXChdMJoim3zgA15Cq08Reu Q+++9Ywr/hzf4bLZTOT63RYhfpnaSpBbGdlK9JgwdzefPaU6/PYSqsSGxdqYqOFk3FgZ gEHUbqmzuSsBzC8SzlUKd5jlzyvsnBJK7F9hiaKqEOnjvu+/+SXczlh9EdDKZlYwU/Kh 0JpQ== X-Gm-Message-State: AO0yUKUSkd+Biq93L+fAZmXLoLSeJE0n4LFnmaMayeZ4TOBVf6jUe5E3 h6CJkrqPjwDlra+E69z1gFU4Lg== X-Received: by 2002:a05:6a20:160c:b0:cd:1cce:d892 with SMTP id l12-20020a056a20160c00b000cd1cced892mr139958pzj.9.1677797393110; Thu, 02 Mar 2023 14:49:53 -0800 (PST) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id e19-20020a62aa13000000b005b6f63c6cf4sm217194pff.30.2023.03.02.14.49.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Mar 2023 14:49:52 -0800 (PST) From: Kees Cook To: Joel Fernandes Cc: Kees Cook , Jakub Kicinski , stable@vger.kernel.org, Masahiro Yamada , Greg Kroah-Hartman , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH] kheaders: Use array declaration instead of char Date: Thu, 2 Mar 2023 14:49:50 -0800 Message-Id: <20230302224946.never.243-kees@kernel.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2075; h=from:subject:message-id; bh=eLj/PflKjatP3hFY0i0HHeuMwWpjFzg2cBxv81U9QkM=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBkASgOFlZ1ftATS5I9PtciOtLxZpppkpTfsbekhvi0 AiYPKIuJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZAEoDgAKCRCJcvTf3G3AJkeqD/ 0UKRxmCYQN06lvmilRYvIyKjBpP1mKZf3n4bjCclIFAOJ/ZLkeIiUPLMpOo34kP1jZOaaJapJsc3th WwaEdtGNaVCu8UyDipj4c1YBMdSYL1xekEJtkPnelZnDwEutxQNxEy38mpAgoCb541wlTw9w0YbPAi yyJa+NJI5l/x2IxhPwKD3F19cxe9JlWTBqC82IpvTUY7l9jxv1XcnM4ZJA0D5mlyDzOovN10GhQdVj aEPKTTjm8ifvUsuiODnrsguQPwwvoadopjqjwgGLlmxM9G6IfAD8F1zIWFo6XFsPfrxhRaQvO/bjcm GLXRBa3GPb0I05K21/lSkajRvk5xOfs+FSpF9S+PztoqgHOnI4wDRrL6zhdp7JzxZQ6IzVsukq1Xqe UUuvWc4OfJN2Q8j3Jkb4aq3NPpQPMnvOip9hlQdEpgRR4foO95+kX2XpQ3nK1uODIiDxCb3y+ScURs iI+/TAUPcYLIV6i59xNWz9yNv2kfisiAXVg5AQdf9jcJbJn6LhGEaW0GlpQc6ocYRRliSxzE91OD9l UKkvUONcTKTMx7q7/bUY+E+2qmFHnoN1HUb//TPtpO1Km4DypVSVz7flMS9OWbF8y6sX/eP2Bk1ceM c8dEuWg6AeUgSV0SwOuqvYletGKA8FxVGpvNRif+xcgiCXHeBQE6QmbVeyjw== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 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 autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759298886274934297?= X-GMAIL-MSGID: =?utf-8?q?1759298886274934297?= Under CONFIG_FORTIFY_SOURCE, memcpy() will check the size of destination and source buffers. Defining kernel_headers_data as "char" would trip this check. Since these addresses are treated as byte arrays, define them as arrays (as done everywhere else). This was seen with: $ cat /sys/kernel/kheaders.tar.xz >> /dev/null detected buffer overflow in memcpy kernel BUG at lib/string_helpers.c:1027! ... RIP: 0010:fortify_panic+0xf/0x20 [...] Call Trace: ikheaders_read+0x45/0x50 [kheaders] kernfs_fop_read_iter+0x1a4/0x2f0 ... Reported-by: Jakub Kicinski Link: https://lore.kernel.org/bpf/20230302112130.6e402a98@kernel.org/ Tested-by: Jakub Kicinski Fixes: 43d8ce9d65a5 ("Provide in-kernel headers to make extending kernel easier") Cc: Joel Fernandes (Google) Cc: stable@vger.kernel.org Signed-off-by: Kees Cook Acked-by: Joel Fernandes (Google) Reviewed-by: Alexander Lobakin --- kernel/kheaders.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/kernel/kheaders.c b/kernel/kheaders.c index 8f69772af77b..42163c9e94e5 100644 --- a/kernel/kheaders.c +++ b/kernel/kheaders.c @@ -26,15 +26,15 @@ asm ( " .popsection \n" ); -extern char kernel_headers_data; -extern char kernel_headers_data_end; +extern char kernel_headers_data[]; +extern char kernel_headers_data_end[]; static ssize_t ikheaders_read(struct file *file, struct kobject *kobj, struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len) { - memcpy(buf, &kernel_headers_data + off, len); + memcpy(buf, &kernel_headers_data[off], len); return len; } @@ -48,8 +48,8 @@ static struct bin_attribute kheaders_attr __ro_after_init = { static int __init ikheaders_init(void) { - kheaders_attr.size = (&kernel_headers_data_end - - &kernel_headers_data); + kheaders_attr.size = (kernel_headers_data_end - + kernel_headers_data); return sysfs_create_bin_file(kernel_kobj, &kheaders_attr); }