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: 12608013 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 D4B80C4332F for ; Mon, 8 Nov 2021 08:39:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C14816125F for ; Mon, 8 Nov 2021 08:39:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238096AbhKHImP (ORCPT ); Mon, 8 Nov 2021 03:42:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45654 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234965AbhKHImN (ORCPT ); Mon, 8 Nov 2021 03:42:13 -0500 Received: from mail-pg1-x530.google.com (mail-pg1-x530.google.com [IPv6:2607:f8b0:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1A6A0C061570; Mon, 8 Nov 2021 00:39:29 -0800 (PST) Received: by mail-pg1-x530.google.com with SMTP id j9so14544958pgh.1; 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=wffbn9bfccyGjgwIC9jokkZ5rPTy/W0mhk2dL41pPWzmuYa/Nsvo5IIq6eVHGrXhcT J4WD3x3X5X95eYVUyJK8ndLTQIzjn93plRDd8s2iIUEnbG6QdbyRrTVgslA0aOrqZONj HfGa8hIL6p2fd7w1XwqgPNLHkRqgEknOFwgP1d+3gcWLJjjcHSDRwb21WKoIGKWwhQOK pGBZ0JTiB02CGeRQ5GehUejNjkGtem+FBEHKeHDARGS2F2Ch3ADCFilma1KVOpW8QC72 A1JmMd+xTgtPaNqxth5y1H/V2l54vNJObhrIztapKU2Xopm4tSdu4NjNc5yw3I4Epp2m 21iw== X-Gm-Message-State: AOAM532VyV0/agmVC7DEfq85JfqPGYKoNdS4l6CP2f9lgpL6FH5QTs2c cE/XHYfd9CCe6vW6qA/K3aE= 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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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: 12608015 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 A6397C4321E for ; Mon, 8 Nov 2021 08:39:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 923D16124D for ; Mon, 8 Nov 2021 08:39:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238121AbhKHImW (ORCPT ); Mon, 8 Nov 2021 03:42:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238099AbhKHImR (ORCPT ); Mon, 8 Nov 2021 03:42:17 -0500 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 71888C061570; Mon, 8 Nov 2021 00:39:33 -0800 (PST) Received: by mail-pf1-x434.google.com with SMTP id g19so9286023pfb.8; 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=aTiDQH4nbIdfy2TT8PpZxpU7LNVbseTWskpPlEAoUIwmpqF9XRKfrhuBM9uJxE/ZTQ fIiw2ExB25mAtqXU2w8OI3DYSx9haIA7qT2D+LEOUp/N+4+OuEVG1w9xfSO83RRkRFyo bTz0u/t8StK5XFUPAiNrZIY1LzxltLXuE7TKVWt8aTtRzfOcAHeDjVUo2ZZ6XgQSAzDZ SoM/CsNU/imXZPkVyPin6vOEnmzxgEGmOUHd4ty6m7aOys33Tgpkg/bw8FNVfGV6x8Jq EdUcX3/aU0ivciNLJ403wN7wqhnaKoAi8EbcyAlrCLGDI4B7hCb064aC3tPHeNDKTg4B G4dQ== X-Gm-Message-State: AOAM530LK9Aed+rQ14//6LjSko5tTnYN3YsAbumxYIvNQH7gaIxoutLW sBOGOwSLjHp2rlS5QBYMaM8= 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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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: 12608017 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 36441C433FE for ; Mon, 8 Nov 2021 08:39:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2055F61163 for ; Mon, 8 Nov 2021 08:39:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238141AbhKHIm3 (ORCPT ); Mon, 8 Nov 2021 03:42:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238124AbhKHImW (ORCPT ); Mon, 8 Nov 2021 03:42:22 -0500 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F212EC061570; Mon, 8 Nov 2021 00:39:37 -0800 (PST) Received: by mail-pl1-x629.google.com with SMTP id u17so15265491plg.9; Mon, 08 Nov 2021 00:39:37 -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=aLZxgVkLjR4juXE795B5opGZGVacPD1DQ9sWstn5wZDskTtWMOqsq6ViSTdwRwOMch QGuXepSJCzWm0w0uih04DIfamBYWTS2hrKAzpsWJ3dAXzeSfKKsiUvEt9lU4jmc8Gpil tBqf1eGw2dfkwVgEAPnWBsCYthU8fm2O0b6CkRnU9T2po57Ouag2FcgTNzbUsoaPRXvx O/8qi/yv5R8g2W2ojWY7QjPcbSYSxAyIYLqq8MnCkZCZNwHod9ef8JsYEEIhK5e2QXcT D5C3p3cTLalL6+g/5VK+Bbt/fkuR5mjBozm9L2FMFizezsFi8xDvSIhnZODLCpnVbLZ4 90bg== X-Gm-Message-State: AOAM530TQutyK5kntK9wQpPfFHH9UHx8DjqGVfAlQ8k5wvzzdVc5dLOS q8x3DINaDeZP5E+mPWVahs4= 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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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: 12608019 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 43528C43217 for ; Mon, 8 Nov 2021 08:39:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2D5FA6124D for ; Mon, 8 Nov 2021 08:39:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238080AbhKHImh (ORCPT ); Mon, 8 Nov 2021 03:42:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237415AbhKHIm0 (ORCPT ); Mon, 8 Nov 2021 03:42:26 -0500 Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52BB2C0613F5; Mon, 8 Nov 2021 00:39:42 -0800 (PST) Received: by mail-pl1-x630.google.com with SMTP id u17so15265603plg.9; 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=Q1iNN+OolM7LjzfYIyKhUWjmfE5yTrcua+TT7I+YvTHZG+wyCkGTbYZABWhazuseNG 4Cw4xQ4M0GtM6Q95ErEdRKezykm10v81DG5pKZ3HFyARtcdx906veHEdCappg3sNNXSj KlHEMG/UGFdvP8kMhXFqmz/+ArK2AVqUqnrjnJAiKH4qUdT6MaIvOcB2XtRgNTqEu4ju GNS6S3mVypN/qKVAJA+TAbRitrByEN8moju/cg2OvBBI6GYzkcaIzYVcF32KkxT0CUGF J1gdUoqCfe5xgfAchLv2XE8mbuBphYbZ6fXmDj69z2XfgM2QUO1gcNpGxca+wlAEVr3b kXLA== X-Gm-Message-State: AOAM531Na6uHSyBFOtvO/GWbxAcOXcYGrC0lEkX0stcZ4kew3RZ6yyOO WpxjZhZ4TFngHVbCogN4K70= 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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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: 12608021 X-Patchwork-Delegate: bpf@iogearbox.net 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 8E93FC433F5 for ; Mon, 8 Nov 2021 08:40:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 785366124D for ; Mon, 8 Nov 2021 08:40:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238178AbhKHIml (ORCPT ); Mon, 8 Nov 2021 03:42:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238146AbhKHIma (ORCPT ); Mon, 8 Nov 2021 03:42:30 -0500 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A771BC0613B9; Mon, 8 Nov 2021 00:39:46 -0800 (PST) Received: by mail-pf1-x42c.google.com with SMTP id b68so4649582pfg.11; 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=a2AHFc4nCS+UR4fCGywTT3SrkljPYeRKNASRyQtF+78jVbuVLSOQA6DhTPj2RJcbWE ToeIhtINIl4rou93yuy9VlIw6wsvnzHFSPJ+nrrDP2Jv9XaeV690MmMit51DpNxZrTMS 7OEToKfYhSPYgQGl8R9odtNinLHwLxvOXbe10zxXW7n+y5oVEYCeQPZR7/7fmxogmbzH GLrpagOHDoEiX3EeEi/SkygmbS1uu9aVwdZTZ7QyWq+kRTyvLLPE6ewrp6SBsdAzEUxV 1LFE0yG3ahxYvrTxNcn6j9hVfbXIIBz1AB0MOFHO4PtiXS7wVSPYt+xIbWXOX6WuVxtd cFmg== X-Gm-Message-State: AOAM532johcmL5PFwwy20/GniVslMKVwQ/AwwKnbF6BvbT0ZkpIorkJV qNXA2S1cJUQw0HS0Ui1D32M= 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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net 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: 12608023 X-Patchwork-Delegate: bpf@iogearbox.net 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 D9BC7C4332F for ; Mon, 8 Nov 2021 08:40:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C3C516125F for ; Mon, 8 Nov 2021 08:40:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238237AbhKHImy (ORCPT ); Mon, 8 Nov 2021 03:42:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238158AbhKHImf (ORCPT ); Mon, 8 Nov 2021 03:42:35 -0500 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C050C061714; Mon, 8 Nov 2021 00:39:51 -0800 (PST) Received: by mail-pf1-x42f.google.com with SMTP id g18so10512012pfk.5; 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=yjfsUtDZYpxN7osO8gd1iUa0uRpa2Fx8q5eS5UTuSIxAU+oYQA6yCI0lVyqVWS3XMX uDtQYYq/KvQEAW5RKQs3T1h1Ra+fHJXAB+d+wDR5ispughYfTt8tkU6tQZm1ySOZ3Fwi nCNB8mgyObYhiiRzvUm9UFugmg+emNU6wcFNnA9KlhLaD5Tm8e9RpphNc38jzdQHjo4z Ba4epPt7chv0xA16pZnMU2WyAgCFD3J9/LzWZbjrAEIMK2qb9B4WS5ToFUrxKXIbmJY/ TIb7M8KthGcqcVZvaY3m1IzmDV9+AjlYIXFDEnSSAiWD9N1EV/1pTeOcuqX7fwFmvT7p gLRA== X-Gm-Message-State: AOAM533PGjc55XNhhR8Vk4mUE42z24bBJI863TjF7bfp1rt0vr9nJpil KAV8trv517T6NGblBr6+KlfDDcONIejJOAWHWu8= 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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net 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: 12608025 X-Patchwork-Delegate: bpf@iogearbox.net 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 649D8C433F5 for ; Mon, 8 Nov 2021 08:40:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4A61C61163 for ; Mon, 8 Nov 2021 08:40:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238252AbhKHInA (ORCPT ); Mon, 8 Nov 2021 03:43:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45744 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238172AbhKHImj (ORCPT ); Mon, 8 Nov 2021 03:42:39 -0500 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A57BBC061570; Mon, 8 Nov 2021 00:39:55 -0800 (PST) Received: by mail-pj1-x102d.google.com with SMTP id j5-20020a17090a318500b001a6c749e697so6128436pjb.1; 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=Vr4hvQM2yvbPRMQXQfzxkWgT7toBVQxNKz8EZsCpRNMY4QUkQvFtUAbaKMnCkXqGsg f1PhSztDXhRdP9FH0Fq7PxzD/oqkvI2ZASVknaQ/MLgJnwtLJwyVrWziCF2gXwaT4do6 VR/gf1q3ox5ACZsPABJdkafQs5DleLuU361WGzufmrFpcNt7sRIetjivV2aK111UbSlk SOfJrTo8zoTqpRJ1CpaCIo8rNUdc1N5ZYMhrnJD/tYLxyIUB94iXCSed/nGu3yaFMrbx pnMlrOZeF3U0YQiWYJ7+2x3CG7erpKQ4GDvZxFXvHvjU420KQcf2Fq7zoM5WOV+Ov+aY 845w== X-Gm-Message-State: AOAM533wW/8Q0r3hoctqKEIM5AjJW3xeZABTdNxbqK9TFETMj4Dhp1MW ApKD4ieupJqHfKIfBsEASqI= 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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net 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; };