Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1649058ybt; Mon, 15 Jun 2020 06:06:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzbSoHh2tONqBYgGA5/ZVZ4kxkvYidXvxZh+EtSrOWTd6l+kKi5TpkwLdOv1htHXhb9a0rm X-Received: by 2002:a17:906:7e19:: with SMTP id e25mr25004942ejr.319.1592226364214; Mon, 15 Jun 2020 06:06:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592226364; cv=none; d=google.com; s=arc-20160816; b=jHs0TXbLE76VjUN2NKKGFIhc0H/KXooiZzTF/uVvrGg7cid+gpL1kNjEBAOV+YEeOa ubdTipfmYR7UBQsWI+RSRBag17PJgh4tAMB3cjWZbHXvrRPOV2dTK3B/qFdYw45VVLWu Z9b4bE1HrrouISWAIxXGy+wkOru8MFuNNyLrn0j0Yb3pVV/YxzZm8nVihLugmLhy/Vtz /6cpm/lYzwYR8/eM5UdQXE3U5jICPP4cS7nrdz0g25zChD7OVMW1Eb8E9FKygdC1ZBKC q9R0F0rcRheZZQQK7c/pDhJDrktE6Htd/rmGdhnUyeCQk5TMkmoYCAjAmTrkWp2YFVr3 cBvQ== 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=/9kSSTxyPh85kVvOo6maTc8rLC2WWVwxqEzTRiJwKx0=; b=EbLoF/3wKgvTMdzbEOacbj7xBw3rgTpYIcLZDwPoGgngM+N/OLl4+VjhYbVFvpRgEu 3LPv37ESK9NXdYUP8xcft4NzECnmnTTnmuIKVVmhH8eSsxJeMSnJn0C8vO6Rt/nWtjHe Gg+B6oN/Vgsv8kHtYCWszsc4xbuglNGEiA3/BN+/BhVLeth56E82MSmCGFrcZ7cjJmUP GXVkKYX4P8CNh+z/CKzOpE2iJ0XjZP7T/vAzOSgb1FhTWjrDEtG63a6ewQECFCE+CK7i rGqUz3hibmcni/scn6co0e6uCznad2pdykxytaqNz/cXjy+h5E2ft6sEX8vbB1pbMzYr f5zA== 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 r15si8475769ejs.733.2020.06.15.06.05.41; Mon, 15 Jun 2020 06:06:04 -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 S1730449AbgFONCq (ORCPT + 99 others); Mon, 15 Jun 2020 09:02:46 -0400 Received: from mail-lj1-f195.google.com ([209.85.208.195]:42122 "EHLO mail-lj1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730098AbgFONCm (ORCPT ); Mon, 15 Jun 2020 09:02:42 -0400 Received: by mail-lj1-f195.google.com with SMTP id y11so19087488ljm.9 for ; Mon, 15 Jun 2020 06:02:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=/9kSSTxyPh85kVvOo6maTc8rLC2WWVwxqEzTRiJwKx0=; b=PoFceHT/f9sNkVkc0UL4UWU82b9WcwNGKvajyCHV5fhPMcQ9AuxVU3auF6WR5QhEVs DwsbuFgiGXkgPZfgnMNyyh3722wXPBecoRejz9zYOHqtSK1NyatbZiZTsVcWa67Uc3w9 fIuD9hP4wZaVzUaVsrpJqXKBYiHyLc4sFMI0/dTchhC1vId4+5IgRj7VLBoh+yUHukg0 eee1rKhNQe0ma16FNOXWBsBPTrS3MLoY+gJOBeJ9xn8TjYpOOXmmarq4ASJuIE/pl6su YxKg/fyn6uyu+SHu9tW+c9YeU3enN4w4q2gBaKqsg8H5w83pWI7/13ytVE1EVAeXQy7a JOCA== X-Gm-Message-State: AOAM533ApKr19Z43gZLZ8D2NQrLReOXj/4heguBDdrFuzIQhmkAQ1aLR +i/Rvwag3L92TKxMkUrHlnY= X-Received: by 2002:a2e:8043:: with SMTP id p3mr11906174ljg.217.1592226159211; Mon, 15 Jun 2020 06:02:39 -0700 (PDT) Received: from localhost.localdomain (broadband-37-110-38-130.ip.moscow.rt.ru. [37.110.38.130]) by smtp.googlemail.com with ESMTPSA id f19sm4548072lfk.24.2020.06.15.06.02.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jun 2020 06:02:38 -0700 (PDT) From: Denis Efremov To: Julia Lawall Cc: Denis Efremov , linux-kernel@vger.kernel.org, cocci@systeme.lip6.fr Subject: [PATCH] coccinelle: api: add device_attr_show script Date: Mon, 15 Jun 2020 16:02:42 +0300 Message-Id: <20200615130242.11825-1-efremov@linux.com> X-Mailer: git-send-email 2.26.2 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 According to the documentation[1] show() methods of device attributes should return the number of bytes printed into the buffer. This is the return value of scnprintf(). show() must not use snprintf() when formatting the value to be returned to user space. snprintf() returns the length the resulting string would be, assuming it all fit into the destination array[2]. scnprintf() return the length of the string actually created in buf. If one can guarantee that an overflow will never happen sprintf() can be used otherwise scnprintf(). [1] Documentation/filesystems/sysfs.txt [2] "snprintf() confusion" https://lwn.net/Articles/69419/ Signed-off-by: Denis Efremov --- scripts/coccinelle/api/device_attr_show.cocci | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 scripts/coccinelle/api/device_attr_show.cocci diff --git a/scripts/coccinelle/api/device_attr_show.cocci b/scripts/coccinelle/api/device_attr_show.cocci new file mode 100644 index 000000000000..d8ec4bb8ac41 --- /dev/null +++ b/scripts/coccinelle/api/device_attr_show.cocci @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: GPL-2.0-only +/// +/// From Documentation/filesystems/sysfs.txt: +/// show() must not use snprintf() when formatting the value to be +/// returned to user space. If you can guarantee that an overflow +/// will never happen you can use sprintf() otherwise you must use +/// scnprintf(). +/// +// Confidence: High +// Copyright: (C) 2020 Denis Efremov ISPRAS +// Options: --no-includes --include-headers +// + +virtual report +virtual org +virtual context +virtual patch + +@r depends on !patch@ +identifier show, dev, attr, buf; +position p; +@@ + +ssize_t show(struct device *dev, struct device_attribute *attr, char *buf) +{ + <... +* return snprintf@p(...); + ...> +} + +@rp depends on patch@ +identifier show, dev, attr, buf; +@@ + +ssize_t show(struct device *dev, struct device_attribute *attr, char *buf) +{ + <... + return +- snprintf ++ scnprintf + (...); + ...> +} + +@script: python depends on report@ +p << r.p; +@@ + +coccilib.report.print_report(p[0], "WARNING: use scnprintf or sprintf") + +@script: python depends on org@ +p << r.p; +@@ + +coccilib.org.print_todo(p[0], "WARNING: use scnprintf or sprintf") -- 2.26.2