From patchwork Mon Nov 8 08:38:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12608049 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 46B93C433F5 for ; Mon, 8 Nov 2021 08:39:31 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DE84161284 for ; Mon, 8 Nov 2021 08:39:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org DE84161284 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 8654B6B0072; Mon, 8 Nov 2021 03:39:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 815736B0073; Mon, 8 Nov 2021 03:39:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6DD536B0078; Mon, 8 Nov 2021 03:39:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0078.hostedemail.com [216.40.44.78]) by kanga.kvack.org (Postfix) with ESMTP id 5F7586B0072 for ; Mon, 8 Nov 2021 03:39:30 -0500 (EST) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 121D6788A7 for ; Mon, 8 Nov 2021 08:39:30 +0000 (UTC) X-FDA: 78785114100.02.5D575DD Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf18.hostedemail.com (Postfix) with ESMTP id A9DA94001E8D for ; Mon, 8 Nov 2021 08:39:29 +0000 (UTC) Received: by mail-pg1-f172.google.com with SMTP id q126so10581146pgq.13 for ; Mon, 08 Nov 2021 00:39:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=iq4zrNVdlh/5wG73Ug/p6d/NCH5T/gwaZPmqV+SFNKo=; b=maKAqki5wXCbTQ/fH6Jk+nVp6iuVd+6TxHryAX3qBLaIGjY1EUHFKRg0BjhtS+b7rc ND7BY8wMUexofSUrich+OavZEjxkGN9IOjTM5O40/ayuUMiV5kecxpwqtXCYkfIJVu6H oznEBwko1QaKnz0HAcguBI5BmVxp06eRNUyWWexz0STcPyytRZ0Omq4KNJ5bJuzt6i/5 EtZUBdkMm+70IRETGjQClhAj954jzO/TTj9updGyFBOB/X5/lTJzit3bjO60zgRSD0W7 ZFEDUlm9fglKynTukFdl9B6yw8Qsd8x2YhBrw9xzmyh7OJsleQY8oJn9KwBJ9nO6+lc5 piyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iq4zrNVdlh/5wG73Ug/p6d/NCH5T/gwaZPmqV+SFNKo=; b=urXIH3u7VIn0NfqNcvSGahLEaV87WExlOgFW4dJTek/+jppnzkBxmLZ1tf/7cqFRiL fiVC8zEc/XcvOcEuPmEnwJsmiBGwUONsGjh8+fU4GQ+diugZjQrGkeJxz8D+Hr6+DYqP FjUNkI9h6DDwYkv45f5s2x7FRyQ3xJhx3bfLYiklt7bdKMIMup06IX41xn3Sv2t+B39O qYmhPcI/mPQtT4w9i+N8xjdp3GVsGpy4BzB2x2N/kz/WXS0DZprqLBO5Qi39QjvyVxNa uBsH2PgKI0JzGP0gLVftsxVoapZSNaVDaDw3ZtfejoC6LXZJeR6I6oEe9dCBizJjJLwZ fFpw== X-Gm-Message-State: AOAM531EHArVzMMJIrdaeKaB6jRFLUeat6T+4vXUtJLpW+aywnfXFF6f +77awvg0tejKxztuc9+epgp17BWoldYS6aZ9SMs= X-Google-Smtp-Source: ABdhPJz5rnIMMev+bztAzoBsTj6gT08dyrO7wwf++qNFeLHZzz0+q7nORNR0ZylFcShM0uAssI/+SA== X-Received: by 2002:a05:6a00:1686:b0:44d:50e:de9e with SMTP id k6-20020a056a00168600b0044d050ede9emr80979907pfc.4.1636360768710; Mon, 08 Nov 2021 00:39:28 -0800 (PST) Received: from localhost.localdomain ([45.63.124.202]) by smtp.gmail.com with ESMTPSA id w3sm12253206pfd.195.2021.11.08.00.39.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Nov 2021 00:39:28 -0800 (PST) From: Yafang Shao To: akpm@linux-foundation.org Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao , Kees Cook , Mathieu Desnoyers , Arnaldo Carvalho de Melo , Alexei Starovoitov , Andrii Nakryiko , Michal Miroslaw , Peter Zijlstra , Steven Rostedt , Matthew Wilcox , David Hildenbrand , Al Viro , Petr Mladek Subject: [PATCH 1/7] fs/exec: make __set_task_comm always set a nul terminated string Date: Mon, 8 Nov 2021 08:38:34 +0000 Message-Id: <20211108083840.4627-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211108083840.4627-1-laoar.shao@gmail.com> References: <20211108083840.4627-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: A9DA94001E8D X-Stat-Signature: muycob4qfhphn8e3ay1jiwzaeb37indo Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=maKAqki5; spf=pass (imf18.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-HE-Tag: 1636360769-678407 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Make sure the string set to task comm is always nul terminated. Signed-off-by: Yafang Shao Reviewed-by: Kees Cook Cc: Mathieu Desnoyers Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Michal Miroslaw Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Matthew Wilcox Cc: David Hildenbrand Cc: Al Viro Cc: Kees Cook Cc: Petr Mladek Reviewed-by: David Hildenbrand --- fs/exec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/exec.c b/fs/exec.c index a098c133d8d7..404156b5b314 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1224,7 +1224,7 @@ void __set_task_comm(struct task_struct *tsk, const char *buf, bool exec) { task_lock(tsk); trace_task_rename(tsk, buf); - strlcpy(tsk->comm, buf, sizeof(tsk->comm)); + strscpy_pad(tsk->comm, buf, sizeof(tsk->comm)); task_unlock(tsk); perf_event_comm(tsk, exec); } From patchwork Mon Nov 8 08:38:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12608051 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 50C2FC433FE for ; Mon, 8 Nov 2021 08:39:35 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 052AD61163 for ; Mon, 8 Nov 2021 08:39:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 052AD61163 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 9C4B26B0073; Mon, 8 Nov 2021 03:39:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 974D46B0078; Mon, 8 Nov 2021 03:39:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 83CBF6B007D; Mon, 8 Nov 2021 03:39:34 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0040.hostedemail.com [216.40.44.40]) by kanga.kvack.org (Postfix) with ESMTP id 758B16B0073 for ; Mon, 8 Nov 2021 03:39:34 -0500 (EST) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 285D92D00F for ; Mon, 8 Nov 2021 08:39:34 +0000 (UTC) X-FDA: 78785114226.27.36C2BEC Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) by imf25.hostedemail.com (Postfix) with ESMTP id 40B22B0002AD for ; Mon, 8 Nov 2021 08:39:23 +0000 (UTC) Received: by mail-pg1-f170.google.com with SMTP id p17so14533888pgj.2 for ; Mon, 08 Nov 2021 00:39:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pMvCN74ycpQtuiyiIco6Y7Jz+XXe+RFDsv6G89oMXm4=; b=c4A9tarfW3eVsp46QAt4AR1nyCmnNvt5dVVK234LNS0uX8r6BsOgCAPOiCqxs3LbUI /xWgGIqLh2OFP7JQsuQo+ZLqMNeVgfvziBtdZX5JhzsuGdtdqaaxCxHM7qKw1C3YfZkC R6YX/9r1i0bRp1jS95bazWORKKTyRrBUaW+OYJ6diyZOpiEb1XzRVjFpt0iSBloqkEj7 +9O7AK5eq6iCFNB1e/+QF6pB/lqRQMh6JiqbE3SJ+0zLZXu53alxkrc643rYYLBfiR7o K0qCPuYXgyr+5VXUb+tYHHviFwCxfChPpGq41G69PJ3BPQy6cyCeHN+MTGDK9BlCtGSB Jk9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pMvCN74ycpQtuiyiIco6Y7Jz+XXe+RFDsv6G89oMXm4=; b=z8eJDRwLrr3dkq3cFyWGeA0gNWrWzG113Q8iioGauxC+bWWDSn2nNttT3GG9rFE5Fc 2eQTfiXF7+AJMxeGrMkiXTO7s+XnW6LN1ion3uGMSbWBhAqqeduISYDRFlda69XDP5fw B2fa1VoMhOPjZUHSm9reIz36eb+/MwbGOQxqexTQ/opTREAfJEssmvg8cAeBhPE4qmJk 7V6UoOKRrjOFNXsibWBMn5a+X5W+v7mylmgqYWmMsGakGdnx6a4tgBLn2m8Os9rcYQl3 I73fI63yHQsbAvC20DXMCuqFWUf17esmdUQFFGvYO5yyYmMscTl3WJPYPgGeRPfu0NHF S2iw== X-Gm-Message-State: AOAM533RH0O6EWQxwiLNZ5WRUXBAWlvns1A5ruHv/ioeGYosQZe/24UL 7J8C3R1+vhl7+nsFSl87GDw= X-Google-Smtp-Source: ABdhPJwHHU5s/XJqHJ5mA3bH36SqqIoONZM5F5Gk6ynW4tqq/51O2I2aSuIIWykfZ34KJrCRSlkf8g== X-Received: by 2002:a05:6a00:216f:b0:49f:dcb7:2bf2 with SMTP id r15-20020a056a00216f00b0049fdcb72bf2mr4101842pff.19.1636360773086; Mon, 08 Nov 2021 00:39:33 -0800 (PST) Received: from localhost.localdomain ([45.63.124.202]) by smtp.gmail.com with ESMTPSA id w3sm12253206pfd.195.2021.11.08.00.39.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Nov 2021 00:39:32 -0800 (PST) From: Yafang Shao To: akpm@linux-foundation.org Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao , Kees Cook , Steven Rostedt , Mathieu Desnoyers , Arnaldo Carvalho de Melo , Alexei Starovoitov , Andrii Nakryiko , Michal Miroslaw , Peter Zijlstra , Matthew Wilcox , David Hildenbrand , Al Viro , Petr Mladek Subject: [PATCH 2/7] fs/exec: make __get_task_comm always get a nul terminated string Date: Mon, 8 Nov 2021 08:38:35 +0000 Message-Id: <20211108083840.4627-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211108083840.4627-1-laoar.shao@gmail.com> References: <20211108083840.4627-1-laoar.shao@gmail.com> MIME-Version: 1.0 Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=c4A9tarf; spf=pass (imf25.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.170 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 40B22B0002AD X-Stat-Signature: 7qwggfouqt1j6i7qnj6hb9iowgsshkqq X-HE-Tag: 1636360763-199656 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: If the dest buffer size is smaller than sizeof(tsk->comm), the buffer will be without null ternimator, that may cause problem. Using strscpy_pad() instead of strncpy() in __get_task_comm() can make the string always nul ternimated. Suggested-by: Kees Cook Suggested-by: Steven Rostedt Signed-off-by: Yafang Shao Reviewed-by: Kees Cook Cc: Mathieu Desnoyers Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Michal Miroslaw Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Matthew Wilcox Cc: David Hildenbrand Cc: Al Viro Cc: Kees Cook Cc: Petr Mladek Reviewed-by: David Hildenbrand --- fs/exec.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fs/exec.c b/fs/exec.c index 404156b5b314..013b707d995d 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1209,7 +1209,8 @@ static int unshare_sighand(struct task_struct *me) char *__get_task_comm(char *buf, size_t buf_size, struct task_struct *tsk) { task_lock(tsk); - strncpy(buf, tsk->comm, buf_size); + /* Always NUL terminated and zero-padded */ + strscpy_pad(buf, tsk->comm, buf_size); task_unlock(tsk); return buf; } From patchwork Mon Nov 8 08:38:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12608053 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D40B0C433EF for ; Mon, 8 Nov 2021 08:39:39 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8D0176124D for ; Mon, 8 Nov 2021 08:39:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 8D0176124D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 2C0786B0078; Mon, 8 Nov 2021 03:39:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 270236B007D; Mon, 8 Nov 2021 03:39:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 138026B007E; Mon, 8 Nov 2021 03:39:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0115.hostedemail.com [216.40.44.115]) by kanga.kvack.org (Postfix) with ESMTP id 06C6C6B0078 for ; Mon, 8 Nov 2021 03:39:39 -0500 (EST) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id AFD1D78B1B for ; Mon, 8 Nov 2021 08:39:38 +0000 (UTC) X-FDA: 78785114436.15.F41AE7E Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by imf20.hostedemail.com (Postfix) with ESMTP id 33CC7D000678 for ; Mon, 8 Nov 2021 08:39:28 +0000 (UTC) Received: by mail-pl1-f173.google.com with SMTP id s24so15361544plp.0 for ; Mon, 08 Nov 2021 00:39:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jSlXXIvd0FywhZ4leAmyX2SHSUDRm+0RxBzeZkFzbpI=; b=Im28rp9XZJVVYG76ls5CqFtvjq2gljvzbc9rZU3ARBdC5+xQuecNb+Sms+SQDRvOKh YSHUapNu+KdWfRyPfBzBFTke/XN8qiPCPenebYiJ2qIGSXPB0GJ42DaNwNFW6LkNI42w JEu18GjmyoDXk/SnKYtXdySTbIxRTz0smS5nVNxyziUmQYZY7lpZzO/N64YUP5Kfu9yJ AMbvhSghgNadSnz4R7k6XWGiUYvn+r5cFGyuSZ4AHP9W1Yf6fbtDDBLcS1bnDzpJSbgH lDlRNItLb1Nvp9GtPa6e0FcpM90k459DZ79L/7Tb9ypPMa0FtgqX4nKb4vfg53LCWHyV +PQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jSlXXIvd0FywhZ4leAmyX2SHSUDRm+0RxBzeZkFzbpI=; b=E/KbSuSw3vhnhcrtgBnhzaeOxYwak5GVjmvZyeZNu1YEu3bfPaCKCGwow1SFFJlHVo GiOpsc4j4n53KZojDHCNtw/fhP24v7vQN5bzCsnjyv8VibJOgaZ5dGUDVEnrMi6fIMML kDygKycfX8VXgOM8ZVkepdZmfs5YQIRB0DB/9sZQUZnv/vTPDDFDf11odnDDyLx8w/VT fnSOBwhDvtHJDAfXF/7dghUKA3YmVrslR/Q0FxE1ic3v2JbBKtZjVhpCXprIFH9dKTj6 lc8IsEXIwPyVO2SWCcF3cLTU7jMNlMG40r6S1WNAA//MsvzC8XHsjcTZBj5p47z3WaJS 6ljA== X-Gm-Message-State: AOAM530vtRNp+gQg/bN/5gxwTQ4EUpEwIBSzivnd3vAhNH0VNybktifg TajKnlpkxI9F+1XahAhUFGg= X-Google-Smtp-Source: ABdhPJwHdtguH7BTx2jF0cGrpR7hI4JYLc1SvyMBQXEVmZZVFrQPprVDTIGCrHOR5Gsk913+KDh2SQ== X-Received: by 2002:a17:90a:6b0a:: with SMTP id v10mr49960820pjj.130.1636360777605; Mon, 08 Nov 2021 00:39:37 -0800 (PST) Received: from localhost.localdomain ([45.63.124.202]) by smtp.gmail.com with ESMTPSA id w3sm12253206pfd.195.2021.11.08.00.39.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Nov 2021 00:39:37 -0800 (PST) From: Yafang Shao To: akpm@linux-foundation.org Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao , Dennis Dalessandro , Mathieu Desnoyers , Arnaldo Carvalho de Melo , Alexei Starovoitov , Andrii Nakryiko , Michal Miroslaw , Peter Zijlstra , Steven Rostedt , Matthew Wilcox , David Hildenbrand , Al Viro , Kees Cook , Petr Mladek Subject: [PATCH 3/7] drivers/infiniband: use get_task_comm instead of open-coded string copy Date: Mon, 8 Nov 2021 08:38:36 +0000 Message-Id: <20211108083840.4627-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211108083840.4627-1-laoar.shao@gmail.com> References: <20211108083840.4627-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 33CC7D000678 X-Stat-Signature: haebsn9jy1febdwhow5dey4g6b4qgrkf Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Im28rp9X; spf=pass (imf20.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-HE-Tag: 1636360768-313685 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Use get_task_comm() instead of open-coded strlcpy() to make the comm always nul terminated. As the comment above the hard-coded 16, we can replace it with TASK_COMM_LEN, then it will adopt to the comm size change. Signed-off-by: Yafang Shao Acked-by: Dennis Dalessandro Cc: Mathieu Desnoyers Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Michal Miroslaw Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Matthew Wilcox Cc: David Hildenbrand Cc: Al Viro Cc: Kees Cook Cc: Petr Mladek Reviewed-by: David Hildenbrand --- drivers/infiniband/hw/qib/qib.h | 2 +- drivers/infiniband/hw/qib/qib_file_ops.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/infiniband/hw/qib/qib.h b/drivers/infiniband/hw/qib/qib.h index 9363bccfc6e7..a8e1c30c370f 100644 --- a/drivers/infiniband/hw/qib/qib.h +++ b/drivers/infiniband/hw/qib/qib.h @@ -196,7 +196,7 @@ struct qib_ctxtdata { pid_t pid; pid_t subpid[QLOGIC_IB_MAX_SUBCTXT]; /* same size as task_struct .comm[], command that opened context */ - char comm[16]; + char comm[TASK_COMM_LEN]; /* pkeys set by this use of this ctxt */ u16 pkeys[4]; /* so file ops can get at unit */ diff --git a/drivers/infiniband/hw/qib/qib_file_ops.c b/drivers/infiniband/hw/qib/qib_file_ops.c index 63854f4b6524..aa290928cf96 100644 --- a/drivers/infiniband/hw/qib/qib_file_ops.c +++ b/drivers/infiniband/hw/qib/qib_file_ops.c @@ -1321,7 +1321,7 @@ static int setup_ctxt(struct qib_pportdata *ppd, int ctxt, rcd->tid_pg_list = ptmp; rcd->pid = current->pid; init_waitqueue_head(&dd->rcd[ctxt]->wait); - strlcpy(rcd->comm, current->comm, sizeof(rcd->comm)); + get_task_comm(rcd->comm, current); ctxt_fp(fp) = rcd; qib_stats.sps_ctxts++; dd->freectxts--; From patchwork Mon Nov 8 08:38:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12608055 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4BD59C433F5 for ; Mon, 8 Nov 2021 08:39:44 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id F3C9C6125F for ; Mon, 8 Nov 2021 08:39:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org F3C9C6125F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 8DAAB6B007D; Mon, 8 Nov 2021 03:39:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 88A6B6B007E; Mon, 8 Nov 2021 03:39:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 72BC56B0080; Mon, 8 Nov 2021 03:39:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0064.hostedemail.com [216.40.44.64]) by kanga.kvack.org (Postfix) with ESMTP id 6524C6B007D for ; Mon, 8 Nov 2021 03:39:43 -0500 (EST) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 0AA10181D2FC2 for ; Mon, 8 Nov 2021 08:39:43 +0000 (UTC) X-FDA: 78785114646.03.B0996B1 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by imf18.hostedemail.com (Postfix) with ESMTP id A28034001E84 for ; Mon, 8 Nov 2021 08:39:42 +0000 (UTC) Received: by mail-pl1-f177.google.com with SMTP id b13so15323688plg.2 for ; Mon, 08 Nov 2021 00:39:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=F4COnTTRSC+CwVYNV8z9S/6Jkdf+sp3tFqGz0D2qNdM=; b=U86xcKBeIe8C88277Vsloks1Il0hC3nKHvJ/vLLriTNMrA4feGBNC9/5t3C0NY+mi9 k0apksnAx8GSlr8XnRgNkC8844s/oe6uT6IeRT8NMnfS5P9gWmzIciQcWKweRk337tMM R3L91x8yeBo2/haXhaqtndWPD6n8XuzekdRzEsz8nNn7HRSWliVlH0LPwgXImKNIjwQ2 xoCSgy0W1Vs7L0FngeHa0k78vc5RyDzj70ZshBdBDQgi/63wTJb6dzkAPPAtavkIcUhF cHf1P8FNWZhJm4reblZI3cL2DEbGnpDfkzUJN82a3bqfg9jjPHX+Tkv91UClqAX7MNq+ CKQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=F4COnTTRSC+CwVYNV8z9S/6Jkdf+sp3tFqGz0D2qNdM=; b=Mz0UeKrMhSvZMC9auRox9QoSFK57W0JVSEa67ttYHz0CD2CWmw2DSBs6kPU9GEcZqf 0FnrgXygIb67HsMDIkJzc/se4gBtSFHX4qmbdHjFkRH/g32ZpCYCg36/HJeWQN+O2ABU 3Lxk8+q2BANmQZsUlyKcdy/vUbKZJGaygCQODfFwIiD/qg8QdaKCWORUCOJwsE5JuV/M V74P3fNdVfCtpNrlL+9VoM7yydRAgWDblIvkdz2qAGRY+6jo38W5FsmojbX7zOL7xG2V HlUnHGdN4pAcZrmG0m6KIg2CAgnRfpO6Dp0IzaQq51cUKnoPI4qA0D2wWeD9Yl8j9Jru +6ng== X-Gm-Message-State: AOAM5301RAr3i12QpvSwp08YnMC8TGs1ySZ3u7IEgiYIpKrebFgWrF03 chtYlF9mlcBNrHrliTYLOW4ZFRTJMvUEz7Epvj4= X-Google-Smtp-Source: ABdhPJz6TyXtOl9PVb+CV7JTpxefSEYXhcK6oHfHEwQaGhLu5wThf9piHTh4Fia/b/8GTiLgxUtiQw== X-Received: by 2002:a17:902:e849:b0:142:c85:4d3d with SMTP id t9-20020a170902e84900b001420c854d3dmr40721527plg.75.1636360781841; Mon, 08 Nov 2021 00:39:41 -0800 (PST) Received: from localhost.localdomain ([45.63.124.202]) by smtp.gmail.com with ESMTPSA id w3sm12253206pfd.195.2021.11.08.00.39.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Nov 2021 00:39:41 -0800 (PST) From: Yafang Shao To: akpm@linux-foundation.org Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao , Kees Cook , Mathieu Desnoyers , Arnaldo Carvalho de Melo , Andrii Nakryiko , Michal Miroslaw , Peter Zijlstra , Steven Rostedt , Matthew Wilcox , David Hildenbrand , Al Viro , Petr Mladek Subject: [PATCH 4/7] fs/binfmt_elf: use get_task_comm instead of open-coded string copy Date: Mon, 8 Nov 2021 08:38:37 +0000 Message-Id: <20211108083840.4627-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211108083840.4627-1-laoar.shao@gmail.com> References: <20211108083840.4627-1-laoar.shao@gmail.com> MIME-Version: 1.0 Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=U86xcKBe; spf=pass (imf18.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: A28034001E84 X-Stat-Signature: 7gjebmddiss8z9rqgbijiwazo39ktutt X-HE-Tag: 1636360782-216229 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: It is better to use get_task_comm() instead of the open coded string copy as we do in other places. struct elf_prpsinfo is used to dump the task information in userspace coredump or kernel vmcore. Below is the verfication of vmcore, crash> ps PID PPID CPU TASK ST %MEM VSZ RSS COMM 0 0 0 ffffffff9d21a940 RU 0.0 0 0 [swapper/0] > 0 0 1 ffffa09e40f85e80 RU 0.0 0 0 [swapper/1] > 0 0 2 ffffa09e40f81f80 RU 0.0 0 0 [swapper/2] > 0 0 3 ffffa09e40f83f00 RU 0.0 0 0 [swapper/3] > 0 0 4 ffffa09e40f80000 RU 0.0 0 0 [swapper/4] > 0 0 5 ffffa09e40f89f80 RU 0.0 0 0 [swapper/5] 0 0 6 ffffa09e40f8bf00 RU 0.0 0 0 [swapper/6] > 0 0 7 ffffa09e40f88000 RU 0.0 0 0 [swapper/7] > 0 0 8 ffffa09e40f8de80 RU 0.0 0 0 [swapper/8] > 0 0 9 ffffa09e40f95e80 RU 0.0 0 0 [swapper/9] > 0 0 10 ffffa09e40f91f80 RU 0.0 0 0 [swapper/10] > 0 0 11 ffffa09e40f93f00 RU 0.0 0 0 [swapper/11] > 0 0 12 ffffa09e40f90000 RU 0.0 0 0 [swapper/12] > 0 0 13 ffffa09e40f9bf00 RU 0.0 0 0 [swapper/13] > 0 0 14 ffffa09e40f98000 RU 0.0 0 0 [swapper/14] > 0 0 15 ffffa09e40f9de80 RU 0.0 0 0 [swapper/15] It works well as expected. Suggested-by: Kees Cook Signed-off-by: Yafang Shao Cc: Mathieu Desnoyers Cc: Arnaldo Carvalho de Melo Cc: Andrii Nakryiko Cc: Michal Miroslaw Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Matthew Wilcox Cc: David Hildenbrand Cc: Al Viro Cc: Kees Cook Cc: Petr Mladek Reviewed-by: David Hildenbrand --- fs/binfmt_elf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index a813b70f594e..138956fd4a88 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -1572,7 +1572,7 @@ static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p, SET_UID(psinfo->pr_uid, from_kuid_munged(cred->user_ns, cred->uid)); SET_GID(psinfo->pr_gid, from_kgid_munged(cred->user_ns, cred->gid)); rcu_read_unlock(); - strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname)); + get_task_comm(psinfo->pr_fname, p); return 0; } From patchwork Mon Nov 8 08:38:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12608057 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 87E44C433F5 for ; Mon, 8 Nov 2021 08:39:48 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3C88C61288 for ; Mon, 8 Nov 2021 08:39:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 3C88C61288 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id D49826B0080; Mon, 8 Nov 2021 03:39:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CF7AB6B0081; Mon, 8 Nov 2021 03:39:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BBEE26B0082; Mon, 8 Nov 2021 03:39:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0222.hostedemail.com [216.40.44.222]) by kanga.kvack.org (Postfix) with ESMTP id ACE1D6B0080 for ; Mon, 8 Nov 2021 03:39:47 -0500 (EST) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 6A5E6182012B6 for ; Mon, 8 Nov 2021 08:39:47 +0000 (UTC) X-FDA: 78785114814.18.6410515 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by imf13.hostedemail.com (Postfix) with ESMTP id E7CC7104F341 for ; Mon, 8 Nov 2021 08:39:36 +0000 (UTC) Received: by mail-pf1-f177.google.com with SMTP id 127so15309903pfu.1 for ; Mon, 08 Nov 2021 00:39:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jQqHvCs2MTZ23pzaDipnjVEuJT5n+/mnDeqlR4J5X1s=; b=FBNJQvD8VIJ6uUEzs0CTS/FbDDoXwoxs2R7wK/wQUA0G2/b5AyxLoDfdGh03PKDWoh kirsgwnEtgFvH6YXH4cHpV8TPoKMeouBoBiC9HBeYFy6mXZeKNYDruFrgxtOtagUaCHp laFxG90tvGykBeri7hkDoLFRwQRQskQEs6ahxhAKpWhI94I8pbssT34fAVk2dErfHPdn Xf0SmxbL7jOttaxJa1IyBdg3ONVLHFeSxEz8eeUXOFQyOYVn+s2HEuNodd0VQ6V5JfsJ qYDap4v3ZeSpmUjay38ZhsC6COJgO0aa9XaUvqaVMeLsiWjO63ipYlCWpCk6Sng6QqLT XycA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jQqHvCs2MTZ23pzaDipnjVEuJT5n+/mnDeqlR4J5X1s=; b=nJaY2rAYVUne9LOdvoRl9xlInXyft+jf95z73KipcEj+g6A8RCsP72KywHMbNVmpRe J07LDtANWXg8/fajECIUiEk6UUkZJGpPNeJn+jB3xRxOcj0FK47kqZvqxMm12rCwshEB 4nMzs6LaV14goYXW38h+U4gNdMa3tO9W2tqNvV7wzMtbfcw7gaqJS/PpVe5QV8DA27CJ 8MnyACc7qGcN1hbWCN5W/aSCzP+TizpWu+dU+yvIsG5cOh8GkegWPSpO3Tco2JLul3g4 UH+M5Neb5++p1HaLf+95ZS2/hqpwi/iRI3KzKYcl580dUFoBQNkznh1LkXD4qO+FOmKl brWQ== X-Gm-Message-State: AOAM532d2V2Z4L/sb/Sv3lrQszHD7CJjJw6U2LCaE65FzD0F28LuKIPp SML3L3ckUENR4S/5M4T4jG4= X-Google-Smtp-Source: ABdhPJzC5TIvVeUxW/sa1XCq0pzyQv39+eD9OWLIs5cika5zyfhxlNpP9FYB8kkiEly2ft4BYkpcVA== X-Received: by 2002:aa7:96ba:0:b0:49f:c35f:83f8 with SMTP id g26-20020aa796ba000000b0049fc35f83f8mr10317791pfk.47.1636360786296; Mon, 08 Nov 2021 00:39:46 -0800 (PST) Received: from localhost.localdomain ([45.63.124.202]) by smtp.gmail.com with ESMTPSA id w3sm12253206pfd.195.2021.11.08.00.39.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Nov 2021 00:39:45 -0800 (PST) From: Yafang Shao To: akpm@linux-foundation.org Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao , Kees Cook , Mathieu Desnoyers , Arnaldo Carvalho de Melo , Alexei Starovoitov , Andrii Nakryiko , Michal Miroslaw , Peter Zijlstra , Steven Rostedt , Matthew Wilcox , David Hildenbrand , Al Viro , Petr Mladek Subject: [PATCH 5/7] samples/bpf/test_overhead_kprobe_kern: make it adopt to task comm size change Date: Mon, 8 Nov 2021 08:38:38 +0000 Message-Id: <20211108083840.4627-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211108083840.4627-1-laoar.shao@gmail.com> References: <20211108083840.4627-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: E7CC7104F341 X-Stat-Signature: s36w3j69dbgwtxhfacqn3jhd3xyzkcqr Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=FBNJQvD8; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf13.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com X-HE-Tag: 1636360776-728770 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: bpf_probe_read_kernel_str() will add a nul terminator to the dst, then we don't care about if the dst size is big enough. This patch also replaces the hard-coded 16 with TASK_COMM_LEN to make it adopt to task comm size change. Signed-off-by: Yafang Shao Reviewed-by: Kees Cook Cc: Mathieu Desnoyers Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Michal Miroslaw Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Matthew Wilcox Cc: David Hildenbrand Cc: Al Viro Cc: Kees Cook Cc: Petr Mladek Acked-by: Andrii Nakryiko Reviewed-by: David Hildenbrand --- samples/bpf/offwaketime_kern.c | 4 ++-- samples/bpf/test_overhead_kprobe_kern.c | 11 ++++++----- samples/bpf/test_overhead_tp_kern.c | 5 +++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/samples/bpf/offwaketime_kern.c b/samples/bpf/offwaketime_kern.c index 4866afd054da..eb4d94742e6b 100644 --- a/samples/bpf/offwaketime_kern.c +++ b/samples/bpf/offwaketime_kern.c @@ -113,11 +113,11 @@ static inline int update_counts(void *ctx, u32 pid, u64 delta) /* taken from /sys/kernel/debug/tracing/events/sched/sched_switch/format */ struct sched_switch_args { unsigned long long pad; - char prev_comm[16]; + char prev_comm[TASK_COMM_LEN]; int prev_pid; int prev_prio; long long prev_state; - char next_comm[16]; + char next_comm[TASK_COMM_LEN]; int next_pid; int next_prio; }; diff --git a/samples/bpf/test_overhead_kprobe_kern.c b/samples/bpf/test_overhead_kprobe_kern.c index f6d593e47037..8fdd2c9c56b2 100644 --- a/samples/bpf/test_overhead_kprobe_kern.c +++ b/samples/bpf/test_overhead_kprobe_kern.c @@ -6,6 +6,7 @@ */ #include #include +#include #include #include #include @@ -22,17 +23,17 @@ int prog(struct pt_regs *ctx) { struct signal_struct *signal; struct task_struct *tsk; - char oldcomm[16] = {}; - char newcomm[16] = {}; + char oldcomm[TASK_COMM_LEN] = {}; + char newcomm[TASK_COMM_LEN] = {}; u16 oom_score_adj; u32 pid; tsk = (void *)PT_REGS_PARM1(ctx); pid = _(tsk->pid); - bpf_probe_read_kernel(oldcomm, sizeof(oldcomm), &tsk->comm); - bpf_probe_read_kernel(newcomm, sizeof(newcomm), - (void *)PT_REGS_PARM2(ctx)); + bpf_probe_read_kernel_str(oldcomm, sizeof(oldcomm), &tsk->comm); + bpf_probe_read_kernel_str(newcomm, sizeof(newcomm), + (void *)PT_REGS_PARM2(ctx)); signal = _(tsk->signal); oom_score_adj = _(signal->oom_score_adj); return 0; diff --git a/samples/bpf/test_overhead_tp_kern.c b/samples/bpf/test_overhead_tp_kern.c index eaa32693f8fc..80edadacb692 100644 --- a/samples/bpf/test_overhead_tp_kern.c +++ b/samples/bpf/test_overhead_tp_kern.c @@ -4,6 +4,7 @@ * modify it under the terms of version 2 of the GNU General Public * License as published by the Free Software Foundation. */ +#include #include #include @@ -11,8 +12,8 @@ struct task_rename { __u64 pad; __u32 pid; - char oldcomm[16]; - char newcomm[16]; + char oldcomm[TASK_COMM_LEN]; + char newcomm[TASK_COMM_LEN]; __u16 oom_score_adj; }; SEC("tracepoint/task/task_rename") From patchwork Mon Nov 8 08:38:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12608059 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0A1C5C433EF for ; Mon, 8 Nov 2021 08:39:53 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B179B6124D for ; Mon, 8 Nov 2021 08:39:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org B179B6124D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 530CA6B0082; Mon, 8 Nov 2021 03:39:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4E15D6B0083; Mon, 8 Nov 2021 03:39:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3D0056B0085; Mon, 8 Nov 2021 03:39:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0175.hostedemail.com [216.40.44.175]) by kanga.kvack.org (Postfix) with ESMTP id 2D38C6B0082 for ; Mon, 8 Nov 2021 03:39:52 -0500 (EST) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id E97A98249980 for ; Mon, 8 Nov 2021 08:39:51 +0000 (UTC) X-FDA: 78785114982.04.4EAB23C Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf22.hostedemail.com (Postfix) with ESMTP id 97860193F for ; Mon, 8 Nov 2021 08:39:51 +0000 (UTC) Received: by mail-pf1-f179.google.com with SMTP id b68so4649761pfg.11 for ; Mon, 08 Nov 2021 00:39:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Vo2MttItjwsBsGEUYM0AHAekU+2EW8dsU5b9ohsZ6Ts=; b=FwceL46eexAcZCPqGKjKQWeRmziWQbb8WStX5KoHEXQ0HRFmEoxTbDvAd2WtYlt/Iq RV1aZIohd0k1nzuLtqYyDQsHVCTdIuYJugDZ0V9WYpWbfcprziMckAwkMF3P72cn28p/ Fk9qKo8Sey/QfeZj9QuPTurT1T6LmzlyfbLYpV3Yf0aYdurW1SfmpoGa49NZ17w5WvBu oU5R53qvv6cuDYKDcLFhs1MnsgLoJhAQ543YqxZQxNycMHUyCbwdADEUaUIs146ujYSW 6Opr6p4VaBbmaq2iTzrR8/qlo48gQy3i8VH1hHSoNO8IinNuyi03EHFENln+fZCVri+R 9lJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Vo2MttItjwsBsGEUYM0AHAekU+2EW8dsU5b9ohsZ6Ts=; b=Qsr1VOuNhHbR6n8yqjqzqahLr/6ZeCCPd8KkX2lRyWRzDU2uxHoSR7UXw1skmpf7BJ VYdnZGbKMZNutAMlucGfZEObURK9vBFXMGJl+p96506VpkC3y0sQjeOJsN6iLEw6tP6S rIzVwnf1i8CglmuyL/Dt1dbAsNxi3PmVMD1gyUF21G84kl+M1s4lGpgfWRDIZTh3SI3C ku6VAftXxz4RGB2bSNfDu2QiF84xXfDV9F3VOqIrRh9LnHN3m8fTIfpslUOoA9egHFkG 1IpRaA8WezouTiyTEu+zklRCkmteKsHmk6lWQ8piuteqQAeq2FHEiVZ9JI2+e1Y3HvIZ 4VLQ== X-Gm-Message-State: AOAM533jhnoZjigu6GoxI1pJbPAwezpdJMrU3vHCJekyv1h12kILFnJI mB84lpvOvz/1xsoiEE+VwOw= X-Google-Smtp-Source: ABdhPJwgo4zI7DiDbY+RIIxuCe+WtrN4O4psiCBih6u5/j6TLIYm8NMWW7enVf1avJoGZz+hPCeR4w== X-Received: by 2002:a63:556:: with SMTP id 83mr48600913pgf.222.1636360790838; Mon, 08 Nov 2021 00:39:50 -0800 (PST) Received: from localhost.localdomain ([45.63.124.202]) by smtp.gmail.com with ESMTPSA id w3sm12253206pfd.195.2021.11.08.00.39.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Nov 2021 00:39:50 -0800 (PST) From: Yafang Shao To: akpm@linux-foundation.org Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao , Andrii Nakryiko , Mathieu Desnoyers , Arnaldo Carvalho de Melo , Alexei Starovoitov , Andrii Nakryiko , Michal Miroslaw , Peter Zijlstra , Steven Rostedt , Matthew Wilcox , David Hildenbrand , Al Viro , Kees Cook , Petr Mladek Subject: [PATCH 6/7] tools/bpf/bpftool/skeleton: use bpf_probe_read_kernel_str to get task comm Date: Mon, 8 Nov 2021 08:38:39 +0000 Message-Id: <20211108083840.4627-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211108083840.4627-1-laoar.shao@gmail.com> References: <20211108083840.4627-1-laoar.shao@gmail.com> MIME-Version: 1.0 Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=FwceL46e; spf=pass (imf22.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 97860193F X-Stat-Signature: 69s4re9nnija87ijbozp6pku64npf7n5 X-HE-Tag: 1636360791-469002 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: bpf_probe_read_kernel_str() will add a nul terminator to the dst, then we don't care about if the dst size is big enough. Signed-off-by: Yafang Shao Acked-by: Andrii Nakryiko Cc: Mathieu Desnoyers Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Michal Miroslaw Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Matthew Wilcox Cc: David Hildenbrand Cc: Al Viro Cc: Kees Cook Cc: Petr Mladek Reviewed-by: David Hildenbrand --- tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/bpf/bpftool/skeleton/pid_iter.bpf.c b/tools/bpf/bpftool/skeleton/pid_iter.bpf.c index d9b420972934..f70702fcb224 100644 --- a/tools/bpf/bpftool/skeleton/pid_iter.bpf.c +++ b/tools/bpf/bpftool/skeleton/pid_iter.bpf.c @@ -71,8 +71,8 @@ int iter(struct bpf_iter__task_file *ctx) e.pid = task->tgid; e.id = get_obj_id(file->private_data, obj_type); - bpf_probe_read_kernel(&e.comm, sizeof(e.comm), - task->group_leader->comm); + bpf_probe_read_kernel_str(&e.comm, sizeof(e.comm), + task->group_leader->comm); bpf_seq_write(ctx->meta->seq, &e, sizeof(e)); return 0; From patchwork Mon Nov 8 08:38:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12608061 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 56A12C4332F for ; Mon, 8 Nov 2021 08:39:57 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 169416124D for ; Mon, 8 Nov 2021 08:39:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 169416124D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id A61DB6B0085; Mon, 8 Nov 2021 03:39:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A10E46B0087; Mon, 8 Nov 2021 03:39:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8D90B6B0088; Mon, 8 Nov 2021 03:39:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0124.hostedemail.com [216.40.44.124]) by kanga.kvack.org (Postfix) with ESMTP id 7D0016B0085 for ; Mon, 8 Nov 2021 03:39:56 -0500 (EST) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 41119180268CA for ; Mon, 8 Nov 2021 08:39:56 +0000 (UTC) X-FDA: 78785115192.25.66B7440 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by imf31.hostedemail.com (Postfix) with ESMTP id 3E554104F34A for ; Mon, 8 Nov 2021 08:39:45 +0000 (UTC) Received: by mail-pl1-f175.google.com with SMTP id o14so15300744plg.5 for ; Mon, 08 Nov 2021 00:39:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=yrH87tfprvHWh1UM/Pxd5vb7Nmf3wgq3PePQi/OQ3fM=; b=UXeJsykeaTS2gyNy6C+oQj/glOemNbibLh5K9uUAexVW98PENGloN1a1DQBKk79Sfz 97shWEXQKatj7t9wk3efdtyJuTylV/6D4GHU0LBtL4lvH3+9MqfldW+cxMZG7wHSw7Ty JLqnQNmZvMc9EnMjtW5gpgosJOELC0bm0xHfIqED/x296LBWnK2O39c5TcdIQL6syzJs lKYZXVvLw8Sof1pb9lqgZlUdGLs32Xc7bpwlrnX91/zOCUhNZuzGTihu/4zP+IG+9zvA kFLxPD0E7Jc0wFI8ssDHUG9vbV7+mB0nHoLZw5HfSgwe+1a4TqmO2ejzjJmLS2U3/+EK uUkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yrH87tfprvHWh1UM/Pxd5vb7Nmf3wgq3PePQi/OQ3fM=; b=gnDDSKGZzTY8+ec5HtQyazNMzky2Ejoin5Ky4ivKJxe7j2g0OEAf4xUzdNogZgeinq SBUy4+mOrdWgiXfDoMGkhmvdSp8G6seYQEyzxakU0HK41CmUjyBPkQdRY94trZ60uqrp vlxl3+2SbFW+pN69YJMXXYR3zI92gUe9WZqUa+YnrMLGwO1ZxnkImhVW/nJXhz1VjxL7 fugfEdDNXjMXbPNsgHwCAVK4YyRJYojzpKtf28cVgFbwT07JxAlRlgaLJwLegK3sQWx/ 73pHgl3kgJA8YDEIbeDI03q/hjtrx2FQdG1/ogwj7FR9EETSC5Q0FUjcbgTTv5MVl04T SwNw== X-Gm-Message-State: AOAM5321L2BU0pUUT8gJRQXI6QRe7A6owEJcryQc5pWwrjSWU0eBXZLb 9LL0r1sPHdKjoz3p5+A5v0U= X-Google-Smtp-Source: ABdhPJzwe6vL48ZC4bLnBtXKY3tyRaqx6od1EbhiMSxN08dDtV62OsE+sTwHtDjbGUZBdCwHWVvgzg== X-Received: by 2002:a17:90b:180d:: with SMTP id lw13mr38098294pjb.225.1636360795243; Mon, 08 Nov 2021 00:39:55 -0800 (PST) Received: from localhost.localdomain ([45.63.124.202]) by smtp.gmail.com with ESMTPSA id w3sm12253206pfd.195.2021.11.08.00.39.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Nov 2021 00:39:54 -0800 (PST) From: Yafang Shao To: akpm@linux-foundation.org Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao , Andrii Nakryiko , Mathieu Desnoyers , Arnaldo Carvalho de Melo , Andrii Nakryiko , Michal Miroslaw , Peter Zijlstra , Steven Rostedt , Matthew Wilcox , David Hildenbrand , Al Viro , Kees Cook , Petr Mladek Subject: [PATCH 7/7] tools/testing/selftests/bpf: make it adopt to task comm size change Date: Mon, 8 Nov 2021 08:38:40 +0000 Message-Id: <20211108083840.4627-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211108083840.4627-1-laoar.shao@gmail.com> References: <20211108083840.4627-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 3E554104F34A X-Stat-Signature: aropm7djpoh7w3gry7ntg6pscf6t1phq Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=UXeJsyke; spf=pass (imf31.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-HE-Tag: 1636360785-42552 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The hard-coded 16 is used in various bpf progs. These progs get task comm either via bpf_get_current_comm() or prctl() or bpf_core_read_str(), all of which can work well even if the task comm size is changed. In these BPF programs, one thing to be improved is the sched:sched_switch tracepoint args. As the tracepoint args are derived from the kernel, we'd better make it same with the kernel. So the macro TASK_COMM_LEN is converted to type enum, then all the BPF programs can get it through BTF. The BPF program which wants to use TASK_COMM_LEN should include the header vmlinux.h. Regarding the test_stacktrace_map and test_tracepoint, as the type defined in linux/bpf.h are also defined in vmlinux.h, so we don't need to include linux/bpf.h again. Signed-off-by: Yafang Shao Acked-by: Andrii Nakryiko Cc: Mathieu Desnoyers Cc: Arnaldo Carvalho de Melo Cc: Andrii Nakryiko Cc: Michal Miroslaw Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Matthew Wilcox Cc: David Hildenbrand Cc: Al Viro Cc: Kees Cook Cc: Petr Mladek Acked-by: David Hildenbrand --- include/linux/sched.h | 9 +++++++-- tools/testing/selftests/bpf/progs/test_stacktrace_map.c | 6 +++--- tools/testing/selftests/bpf/progs/test_tracepoint.c | 6 +++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index e0454e60fe8f..1c456691f890 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -274,8 +274,13 @@ struct task_group; #define get_current_state() READ_ONCE(current->__state) -/* Task command name length: */ -#define TASK_COMM_LEN 16 +/* + * Define the task command name length as enum, then it can be visible to + * BPF programs. + */ +enum { + TASK_COMM_LEN = 16, +}; extern void scheduler_tick(void); diff --git a/tools/testing/selftests/bpf/progs/test_stacktrace_map.c b/tools/testing/selftests/bpf/progs/test_stacktrace_map.c index 00ed48672620..e9b602a6dc1b 100644 --- a/tools/testing/selftests/bpf/progs/test_stacktrace_map.c +++ b/tools/testing/selftests/bpf/progs/test_stacktrace_map.c @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 // Copyright (c) 2018 Facebook -#include +#include #include #ifndef PERF_MAX_STACK_DEPTH @@ -41,11 +41,11 @@ struct { /* taken from /sys/kernel/debug/tracing/events/sched/sched_switch/format */ struct sched_switch_args { unsigned long long pad; - char prev_comm[16]; + char prev_comm[TASK_COMM_LEN]; int prev_pid; int prev_prio; long long prev_state; - char next_comm[16]; + char next_comm[TASK_COMM_LEN]; int next_pid; int next_prio; }; diff --git a/tools/testing/selftests/bpf/progs/test_tracepoint.c b/tools/testing/selftests/bpf/progs/test_tracepoint.c index 4b825ee122cf..f21982681e28 100644 --- a/tools/testing/selftests/bpf/progs/test_tracepoint.c +++ b/tools/testing/selftests/bpf/progs/test_tracepoint.c @@ -1,17 +1,17 @@ // SPDX-License-Identifier: GPL-2.0 // Copyright (c) 2017 Facebook -#include +#include #include /* taken from /sys/kernel/debug/tracing/events/sched/sched_switch/format */ struct sched_switch_args { unsigned long long pad; - char prev_comm[16]; + char prev_comm[TASK_COMM_LEN]; int prev_pid; int prev_prio; long long prev_state; - char next_comm[16]; + char next_comm[TASK_COMM_LEN]; int next_pid; int next_prio; };