Received: by 2002:a05:6a10:6006:0:0:0:0 with SMTP id w6csp1537792pxa; Fri, 28 Aug 2020 15:53:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyyN9mcDjHaaxqEl72NOt4/byt+108KT6HMIoNv5ZuirjG2jwiWgpA6xNi6JJBVjZI12om/ X-Received: by 2002:a17:906:2a49:: with SMTP id k9mr1008322eje.117.1598655199562; Fri, 28 Aug 2020 15:53:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598655199; cv=none; d=google.com; s=arc-20160816; b=B2nXfYWcdprh18Ae/sVgL71uOdkFDktI6Ij9VlGCB2KCnAJ6nU2IFlRI2ZepeqeWvT 0qFXBXzODGMfTW6kJhgNlcb92x/uCaKL2lSiIsiqCwlxaCCoPCSi5Ad7exkf34YUUHC9 6nODl1XYyYFPOQ5ymjt4sMRFsGEB9tUq2Y/ShkPC7CYKMru+sJ569DJ0A2SRR8925L74 tN5TS3F/KkP6mXZDAjtv57igDllo1ghOHi9rRkxd2PnMdebPPc7oei41WVBlhxPAyNrp jAoBXqJ2r0yGJV9BZU5ZiFuoi1HA81RouNFNNHfxevn5qE+shpUWNQqUVIYeiLJBUgU/ VoBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=ib7U1oTPj17AEoxU6W01MLmbiN7jVMo/shFZp/3MO04=; b=01TZG+7oM8pbV7GAz5ffS9L/BYAWkV8QS2SYXIu+rBSoz42MS8I8Z4TzdFAbsDAXgO LBWpSjrIAoXdZX2flMLauzfznTsOnASBK9k+8FJcpKW49v6rQUBIr5sdeD532Pop3lwh V0ECWPOf3hV5b4NzEoZ4wj1NBB12k2tbjgNZgVM2bnnihyvdArqB3D5PMr5XuPoyHLSs EMHWkcPFA7Nry5OeYCCMZfnUFXf2Xv84QOA1OndQI5Sxrm0z2SEwCqGvSRJHYG6+D9p2 rA9q3jQ/T71Yddx+hJakWkKKcKuPkjfwr0hiMqW3hh7EngmJArdyFs7I2bnD1a5kceAd 2t8w== ARC-Authentication-Results: i=1; mx.google.com; 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 dk11si623339ejb.450.2020.08.28.15.52.56; Fri, 28 Aug 2020 15:53:19 -0700 (PDT) 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; 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 S1726775AbgH1WwT (ORCPT + 99 others); Fri, 28 Aug 2020 18:52:19 -0400 Received: from smtprelay0173.hostedemail.com ([216.40.44.173]:45790 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726550AbgH1WwS (ORCPT ); Fri, 28 Aug 2020 18:52:18 -0400 Received: from filter.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay03.hostedemail.com (Postfix) with ESMTP id E7F71837F24A; Fri, 28 Aug 2020 22:52:16 +0000 (UTC) X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,,RULES_HIT:41:355:379:541:800:960:973:988:989:1260:1311:1314:1345:1437:1515:1534:1542:1711:1730:1747:1777:1792:1981:2194:2199:2393:2559:2562:2914:3138:3139:3140:3141:3142:3353:3865:3866:3867:3868:3870:3871:3872:3874:4321:4605:5007:6261:7875:8603:10004:10848:11026:11473:11658:11914:12043:12291:12296:12297:12438:12555:12683:12895:12986:13894:14096:14110:14181:14394:14721:21080:21451:21627:21740:21990:30034:30054,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:1,LUA_SUMMARY:none X-HE-Tag: shock81_260b9bc27079 X-Filterd-Recvd-Size: 3243 Received: from joe-laptop.perches.com (cpe-72-134-242-36.natsow.res.rr.com [72.134.242.36]) (Authenticated sender: joe@perches.com) by omf10.hostedemail.com (Postfix) with ESMTPA; Fri, 28 Aug 2020 22:52:15 +0000 (UTC) From: Joe Perches To: Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: Kees Cook , "Gustavo A . R . Silva" , Denis Efremov , Julia Lawall , Alex Dewar , linux-kernel@vger.kernel.org Subject: [PATCH] sysfs: Add sysfs_emit to replace sprintf to PAGE_SIZE buffers. Date: Fri, 28 Aug 2020 15:52:13 -0700 Message-Id: X-Mailer: git-send-email 2.26.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org sprintf does not know the PAGE_SIZE maximum of the temporary buffer used for outputting sysfs content requests and it's possible to overrun the buffer length. Add a generic sysfs_emit mechanism that knows that the size of the temporary buffer and ensures that no overrun is done. Signed-off-by: Joe Perches --- fs/sysfs/file.c | 30 ++++++++++++++++++++++++++++++ include/linux/sysfs.h | 8 ++++++++ 2 files changed, 38 insertions(+) diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c index eb6897ab78e7..06a13bbd7080 100644 --- a/fs/sysfs/file.c +++ b/fs/sysfs/file.c @@ -707,3 +707,33 @@ int sysfs_change_owner(struct kobject *kobj, kuid_t kuid, kgid_t kgid) return 0; } EXPORT_SYMBOL_GPL(sysfs_change_owner); + +/** + * sysfs_emit - scnprintf equivalent, aware of PAGE_SIZE buffer. + * @buf: start of PAGE_SIZE buffer. + * @pos: current position in buffer + * (pos - buf) must always be < PAGE_SIZE + * @fmt: format + * @...: arguments to format + * + * + * Returns number of characters written at pos. + */ +int sysfs_emit(char *buf, char *pos, const char *fmt, ...) +{ + va_list args; + bool bad_pos = pos < buf; + bool bad_len = (pos - buf) >= PAGE_SIZE; + int len; + + if (WARN(bad_pos || bad_len, "(pos < buf):%d (pos >= PAGE_SIZE):%d\n", + bad_pos, bad_len)) + return 0; + + va_start(args, fmt); + len = vscnprintf(pos, PAGE_SIZE - (pos - buf), fmt, args); + va_end(args); + + return len; +} +EXPORT_SYMBOL_GPL(sysfs_emit); diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h index 34e84122f635..5a21d3d30016 100644 --- a/include/linux/sysfs.h +++ b/include/linux/sysfs.h @@ -329,6 +329,8 @@ int sysfs_groups_change_owner(struct kobject *kobj, int sysfs_group_change_owner(struct kobject *kobj, const struct attribute_group *groups, kuid_t kuid, kgid_t kgid); +__printf(3, 4) +int sysfs_emit(char *buf, char *pos, const char *fmt, ...); #else /* CONFIG_SYSFS */ @@ -576,6 +578,12 @@ static inline int sysfs_group_change_owner(struct kobject *kobj, return 0; } +__printf(3, 4) +static inline int sysfs_emit(char *buf, char *pos, const char *fmt, ...) +{ + return 0; +} + #endif /* CONFIG_SYSFS */ static inline int __must_check sysfs_create_file(struct kobject *kobj, -- 2.26.0