From nobody Sat Feb 7 18:16:02 2026 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 8E555C7EE24 for ; Mon, 5 Jun 2023 17:06:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235303AbjFERGQ (ORCPT ); Mon, 5 Jun 2023 13:06:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235187AbjFERGB (ORCPT ); Mon, 5 Jun 2023 13:06:01 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 515FDED; Mon, 5 Jun 2023 10:06:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685984760; x=1717520760; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=KBLjfFtbsCdNjxRprw9OiUI+007+Q0Bv6edzE8t+Abo=; b=fLlLmNLk7EN2fFWVHDienboKYrMJTdfYjtWSnZW81E3ulMkfI/1ldwdU TTuZ5+cADGB7YgIPt6DoRtQV+vHDew6Y1xbYhM4YA709JHONoiNVKIYva hBMZDtgjKx2+ANcFZFjot6biQxwz5F41tAdiQKFNUWUichxnfkWCcLcJN 0uxj/BKh/IkjcZ7kAQgkPwJI6rFPS3ZEeGHfglAhaw3m4psLIWyebc2gA YpC030bVDDBHsfaC3h/1LeL0VGCwVZ+W/N86p5kPWq8lNb+w+rlRAOf4c sSQKc3MxRT1/ntgpdjcVl5OviCBlzT+h3h5ihTa3eBWBOR7dTIqvnaSxu g==; X-IronPort-AV: E=McAfee;i="6600,9927,10732"; a="356431401" X-IronPort-AV: E=Sophos;i="6.00,218,1681196400"; d="scan'208";a="356431401" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Jun 2023 10:05:52 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10732"; a="773807508" X-IronPort-AV: E=Sophos;i="6.00,218,1681196400"; d="scan'208";a="773807508" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga008.fm.intel.com with ESMTP; 05 Jun 2023 10:05:50 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id 93C65FD; Mon, 5 Jun 2023 20:05:56 +0300 (EEST) From: Andy Shevchenko To: Kees Cook , Greg Kroah-Hartman , Andy Shevchenko , Cezary Rojewski , linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org Cc: "Theodore Ts'o" , Jan Kara , Andy Shevchenko , "Rafael J. Wysocki" , Jan Kara Subject: [PATCH v3 1/3] jbd2: Avoid printing outside the boundary of the buffer Date: Mon, 5 Jun 2023 20:05:51 +0300 Message-Id: <20230605170553.7835-2-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.40.0.1.gaa8946217a0b In-Reply-To: <20230605170553.7835-1-andriy.shevchenko@linux.intel.com> References: <20230605170553.7835-1-andriy.shevchenko@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Theoretically possible that "%pg" will take all room for the j_devname and hence the "-%lu" will go outside the boundary due to unconditional sprintf() in use. To make this code more robust, replace two sequential s*printf():s by a single call and then replace forbidden character. It's possible to do this way, because '/' won't ever be in the result of "-%lu". Reviewed-by: Jan Kara Signed-off-by: Andy Shevchenko --- fs/jbd2/journal.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/fs/jbd2/journal.c b/fs/jbd2/journal.c index 8ae419152ff6..6e17f8f94dfd 100644 --- a/fs/jbd2/journal.c +++ b/fs/jbd2/journal.c @@ -1491,7 +1491,6 @@ journal_t *jbd2_journal_init_inode(struct inode *inod= e) { journal_t *journal; sector_t blocknr; - char *p; int err =3D 0; =20 blocknr =3D 0; @@ -1515,9 +1514,8 @@ journal_t *jbd2_journal_init_inode(struct inode *inod= e) =20 journal->j_inode =3D inode; snprintf(journal->j_devname, sizeof(journal->j_devname), - "%pg", journal->j_dev); - p =3D strreplace(journal->j_devname, '/', '!'); - sprintf(p, "-%lu", journal->j_inode->i_ino); + "%pg-%lu", journal->j_dev, journal->j_inode->i_ino); + strreplace(journal->j_devname, '/', '!'); jbd2_stats_proc_init(journal); =20 return journal; --=20 2.40.0.1.gaa8946217a0b From nobody Sat Feb 7 18:16:02 2026 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 9AAF0C7EE24 for ; Mon, 5 Jun 2023 17:06:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235266AbjFERGH (ORCPT ); Mon, 5 Jun 2023 13:06:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235107AbjFERGA (ORCPT ); Mon, 5 Jun 2023 13:06:00 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 615D09C; Mon, 5 Jun 2023 10:05:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685984758; x=1717520758; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gFiVCBXWyj8bmgbMxtEqGr730r7w2iTq5mksoPWBe7A=; b=WWqlzGxd7lWQv+bSr1iQeZlDLji0adz9uhaWwby8h4ZqhsyW1TQQ1SLr v+loH2puNZ2ZiG4ZLui0gxAV2WCYy/WsMlqzHCNk+w11pyT81LQsTNst/ xm+R5YKJb3ymTsQiK0wNNkIM0AeeEkcG+J58QXGwKPCuFhJpEawsoZ0M8 2oMxVuxLNUn0LC+xKBTzG0b2/Q0HUgaBlZAKjUhhYFCY5sT7GWSBXKXXS m8m+pvdjbgvpTpdtOLal8+esIG0+cyYlW3nvWijSYl6L7oZJU0ADm8UaC BV3RU3l+uBOzvKC/gHDNvTAMdmCQ5aNXe5q3UZQXkwgGMC009ATtqlN1A A==; X-IronPort-AV: E=McAfee;i="6600,9927,10732"; a="356431386" X-IronPort-AV: E=Sophos;i="6.00,218,1681196400"; d="scan'208";a="356431386" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Jun 2023 10:05:52 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10732"; a="773807503" X-IronPort-AV: E=Sophos;i="6.00,218,1681196400"; d="scan'208";a="773807503" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga008.fm.intel.com with ESMTP; 05 Jun 2023 10:05:49 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id 9E28B34F; Mon, 5 Jun 2023 20:05:56 +0300 (EEST) From: Andy Shevchenko To: Kees Cook , Greg Kroah-Hartman , Andy Shevchenko , Cezary Rojewski , linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org Cc: "Theodore Ts'o" , Jan Kara , Andy Shevchenko , "Rafael J. Wysocki" Subject: [PATCH v3 2/3] lib/string_helpers: Change returned value of the strreplace() Date: Mon, 5 Jun 2023 20:05:52 +0300 Message-Id: <20230605170553.7835-3-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.40.0.1.gaa8946217a0b In-Reply-To: <20230605170553.7835-1-andriy.shevchenko@linux.intel.com> References: <20230605170553.7835-1-andriy.shevchenko@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" It's more useful to return the pointer to the string itself with strreplace(), so it may be used like attr->name =3D strreplace(name, '/', '_'); While at it, amend the kernel documentation. Signed-off-by: Andy Shevchenko --- include/linux/string.h | 2 +- lib/string_helpers.c | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/include/linux/string.h b/include/linux/string.h index c062c581a98b..dbfc66400050 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -169,7 +169,7 @@ static inline void memcpy_flushcache(void *dst, const v= oid *src, size_t cnt) #endif =20 void *memchr_inv(const void *s, int c, size_t n); -char *strreplace(char *s, char old, char new); +char *strreplace(char *str, char old, char new); =20 extern void kfree_const(const void *x); =20 diff --git a/lib/string_helpers.c b/lib/string_helpers.c index 230020a2e076..d3b1dd718daf 100644 --- a/lib/string_helpers.c +++ b/lib/string_helpers.c @@ -979,18 +979,22 @@ EXPORT_SYMBOL(__sysfs_match_string); =20 /** * strreplace - Replace all occurrences of character in string. - * @s: The string to operate on. + * @str: The string to operate on. * @old: The character being replaced. * @new: The character @old is replaced with. * - * Returns pointer to the nul byte at the end of @s. + * Replaces the each @old character with a @new one in the given string @s= tr. + * + * Return: pointer to the string @str itself. */ -char *strreplace(char *s, char old, char new) +char *strreplace(char *str, char old, char new) { + char *s =3D str; + for (; *s; ++s) if (*s =3D=3D old) *s =3D new; - return s; + return str; } EXPORT_SYMBOL(strreplace); =20 --=20 2.40.0.1.gaa8946217a0b From nobody Sat Feb 7 18:16:02 2026 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 D327EC7EE2E for ; Mon, 5 Jun 2023 17:06:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235151AbjFERGB (ORCPT ); Mon, 5 Jun 2023 13:06:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232289AbjFERF6 (ORCPT ); Mon, 5 Jun 2023 13:05:58 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90D9EF1; Mon, 5 Jun 2023 10:05:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685984753; x=1717520753; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Uqba7KW+LSTrR6KDuN+F/tgiTmPx6AhRcJexGJYfzL0=; b=gZnt7i7S5RAKnCKhWoOseINM9UweFOim683+c0tM4l+IvhnrTiEsvNsw JAbAU1C6zGh/Jgx/2ys2TRPKu32shLHlX47KJdo9lEMBGzeqdPFOutF6E sXVfRKyebWO6uAl1SSnvYo2BJLsEGI2CyZNgM23CdLIWozuWyhe7pHjfY l9Nxg6UnQShrJF8Q7HpHEZB+EtoN5++Oz4mwgOXik1k7dVyxUHOZIqEDC eU/oQPvR4mY32FA+j5qW6AJJtHJwXa5CPKCnY9K/cT2jPhSqrirF88/rR s3YfBWRqAuPdYQ4yjKUwKZbtbhqMVxwOqK5YTIKOdCtUO9LfCAyauBLRi Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10732"; a="356431378" X-IronPort-AV: E=Sophos;i="6.00,218,1681196400"; d="scan'208";a="356431378" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Jun 2023 10:05:52 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10732"; a="773807505" X-IronPort-AV: E=Sophos;i="6.00,218,1681196400"; d="scan'208";a="773807505" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga008.fm.intel.com with ESMTP; 05 Jun 2023 10:05:49 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id AE297379; Mon, 5 Jun 2023 20:05:56 +0300 (EEST) From: Andy Shevchenko To: Kees Cook , Greg Kroah-Hartman , Andy Shevchenko , Cezary Rojewski , linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org Cc: "Theodore Ts'o" , Jan Kara , Andy Shevchenko , "Rafael J. Wysocki" Subject: [PATCH v3 3/3] kobject: Use return value of strreplace() Date: Mon, 5 Jun 2023 20:05:53 +0300 Message-Id: <20230605170553.7835-4-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.40.0.1.gaa8946217a0b In-Reply-To: <20230605170553.7835-1-andriy.shevchenko@linux.intel.com> References: <20230605170553.7835-1-andriy.shevchenko@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Since strreplace() returns the pointer to the string itself, we may use it directly in the code. Signed-off-by: Andy Shevchenko --- lib/kobject.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/kobject.c b/lib/kobject.c index f79a434e1231..16d530f9c174 100644 --- a/lib/kobject.c +++ b/lib/kobject.c @@ -281,8 +281,7 @@ int kobject_set_name_vargs(struct kobject *kobj, const = char *fmt, kfree_const(s); if (!t) return -ENOMEM; - strreplace(t, '/', '!'); - s =3D t; + s =3D strreplace(t, '/', '!'); } kfree_const(kobj->name); kobj->name =3D s; --=20 2.40.0.1.gaa8946217a0b