From patchwork Thu Jun 13 02:30:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13695916 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D34D433F6; Thu, 13 Jun 2024 02:31:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718245873; cv=none; b=gc5nI8SZI5WCRtaKKlZg44/qyylH+oqCs1+2tRiqcRtTnBq/I3bl4A9Oa03K3AW6mcQmYsP7v9Y4raht5ReSErzLzMr+5jGtdU255iE26Q/mDdq+kHOnQ418hauvuEQKwVfR+RHV1Gh51inFkCzwwQeiCJcX1/F5jSd7MCQ8cgI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718245873; c=relaxed/simple; bh=LX61AqcJNexywhWKUem5c5kFXTCQtXdiKlDqbpWt6oE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JvVToUi70jUYDTOGJuBd6ysTsdrKlDAf08UdTfKPFAU2/fsEdlrBYNKCtT4WMwdD4XyhZad5/pgahhpgyefZTf6hKKKqTu5oeUQst7Dd40kKSs/aXJgPpWgM514WCxgcmPrgCo8EuE8ihpXGIrmd/cfD5XFG1nwkFdSLgL4eZ8k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=DfL/V1a7; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DfL/V1a7" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1f4a0050b9aso4925655ad.2; Wed, 12 Jun 2024 19:31:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718245871; x=1718850671; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Zt6Zo0BQwE9mYz3Q0zCJOWUGA/XKEFxfC7Idotg6GZ4=; b=DfL/V1a7sg9OmRK/rfdaZT6ApIcuzUdgSVe7aFbOCuwUjFroAKL15fJ5Pf3HhlU4iE yfa9dgL5WuLdHUgrGn6BnfiSddT/GHxAZJPxtyFpv33vfVzyDe0XsP5M2VmAjrjjA0IT 36c/GrsXlD5aArP7hQ4j/22nyKIKZBNVz6bJd/VxI73saBIjc8JtZLjQClqQhrzvP8M9 tgV7c+mn3BUfc6Vp3muQZkIEGlrWWPHIn/d1YEwOM+4OcDVwaaw0362cDmNAArf0cd3H q0jqhQvtVPQn5WSGIRfjvNbCvjIG8CZoW96diMgJFuFCAHtraWKhJLE5NCiVk+YE3Yvo RpTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718245871; x=1718850671; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Zt6Zo0BQwE9mYz3Q0zCJOWUGA/XKEFxfC7Idotg6GZ4=; b=YIuX0GtCKQjMnJI+0n32vOmvtkJ91VujrDhNlnRFybl/UCQjQ67f3/U10H2NZ1Y3uZ 4KyrD4tdD0NqsTq3m4iPDVgqjE8aNLqgqBTuz8DXHt6hRZo/aKJUMS5uq3/63eBZ+ASV Tr7PjfkMjmZHvekJTuFoCOaO3dpAsePbVaC2VLBTmTYyT83cP28bBOydt2hlwsc8Ypv8 sk84gZmwrtV77DYIntVC255Mqc5z9Go4mMjw6zOy8gcQ0RxtNrUk/JpaUL+Gyq4Rc+8X sPbCLs6PVxLmjBpTGkXwT5HtGlWSctmmmShx7E/0p7XC6TOoyL6b3Yqpt/iXffiF+zsn 3IvA== X-Forwarded-Encrypted: i=1; AJvYcCXKtglx9LHY+w6U/mNzIeT3qmIMrVvSzwprlE5E5+8p9/ax920mziZHkh9uJVMd7Qwd2Xfo9kmNaFX/YlD8IDrJ3+Ccl+7Z3gFfqwpfmeDQmZkUlL83CMJJQZs+wFS64RgRdM6+3JEu6IbmnkkAUtuFhq5R1nysMjhOMvoKUGKXyDEdCbuQxeCCoFDbQWz6tBRIE1aC9XmnbZ5vl55iIs+89K1ttwBaz/MFWeQuD3Y8v104ivJCshWfLQdY+Tn4926sPUd516K7Fkn0COmmLljJ3ZlrKxs8xZo0pBdwnseurk6AyERFjQA8dtkJn78m2FyLSEEXBw== X-Gm-Message-State: AOJu0Yxu6xT+mfXekcJIm3o3xJ+81vVAhOh7BdwEk33o5+PYMpyc6oVz at3FGkIZrpS6mGJNwtNXqMwNMYq/ithrxnPs+eQGNwVdaqxxZ36Z X-Google-Smtp-Source: AGHT+IGepHvi+lKqMo1GYaife0L6fUJVRPiktMyHkXeHwR+7tr6qOJAeLHRN5Lin69VRxAqus5DeJQ== X-Received: by 2002:a17:902:ecc1:b0:1f6:6ef0:dae4 with SMTP id d9443c01a7336-1f83b5f3fffmr43616545ad.32.1718245871101; Wed, 12 Jun 2024 19:31:11 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f4d159sm1755695ad.289.2024.06.12.19.31.03 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 19:31:10 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Alexander Viro , Christian Brauner , Jan Kara , Kees Cook , Matus Jokay Subject: [PATCH v2 01/10] fs/exec: Drop task_lock() inside __get_task_comm() Date: Thu, 13 Jun 2024 10:30:35 +0800 Message-Id: <20240613023044.45873-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240613023044.45873-1-laoar.shao@gmail.com> References: <20240613023044.45873-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Quoted from Linus [0]: Since user space can randomly change their names anyway, using locking was always wrong for readers (for writers it probably does make sense to have some lock - although practically speaking nobody cares there either, but at least for a writer some kind of race could have long-term mixed results Suggested-by: Linus Torvalds Link: https://lore.kernel.org/all/CAHk-=wivfrF0_zvf+oj6==Sh=-npJooP8chLPEfaFV0oNYTTBA@mail.gmail.com [0] Signed-off-by: Yafang Shao Cc: Alexander Viro Cc: Christian Brauner Cc: Jan Kara Cc: Eric Biederman Cc: Kees Cook Cc: Alexei Starovoitov Cc: Matus Jokay --- fs/exec.c | 10 ++++++++-- include/linux/sched.h | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index 40073142288f..fa6b61c79df8 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1238,12 +1238,18 @@ static int unshare_sighand(struct task_struct *me) return 0; } +/* + * User space can randomly change their names anyway, so locking for readers + * doesn't make sense. For writers, locking is probably necessary, as a race + * condition could lead to long-term mixed results. + * The strscpy_pad() in __set_task_comm() can ensure that the task comm is + * always NUL-terminated. Therefore the race condition between reader and writer + * is not an issue. + */ char *__get_task_comm(char *buf, size_t buf_size, struct task_struct *tsk) { - task_lock(tsk); /* Always NUL terminated and zero-padded */ strscpy_pad(buf, tsk->comm, buf_size); - task_unlock(tsk); return buf; } EXPORT_SYMBOL_GPL(__get_task_comm); diff --git a/include/linux/sched.h b/include/linux/sched.h index 61591ac6eab6..95888d1da49e 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1086,9 +1086,9 @@ struct task_struct { /* * executable name, excluding path. * - * - normally initialized setup_new_exec() + * - normally initialized begin_new_exec() * - access it with [gs]et_task_comm() - * - lock it with task_lock() + * - lock it with task_lock() for writing */ char comm[TASK_COMM_LEN]; From patchwork Thu Jun 13 02:30:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13695917 Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D088D33F6; Thu, 13 Jun 2024 02:31:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718245880; cv=none; b=sNk5CrkVsnIzOedG8Espcq+9Jyflgez7IfvqeT5WiQgTYPNMyJj4zZKoPb57PALAT8K3e3lw53tI+5CLO+/8vMp/g8x89wHTwxKU9VY2buvSJjffsorRE9+JMBxWkhziOeIEXE9R2z+os1oK8iibRIyN9f4sgHoyPH9N9LHNIXM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718245880; c=relaxed/simple; bh=Cmml+druTyvH17AsxZ9+qWd188UAOiUJPJyv4QJyitc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WTiKd0ZoFZ4Fh6mhaSoSt8I0ANii98wzWml4VQ5fWZkc7w2iCu/LuIM+e69J9YzalNuiPB7eeUACybb0YeudhtDEPzq/yFqV1O3CDIfOgQ13FaRvmUAO0BRPSJgRQ33aY9Zb96H4PDahRKLMVnXveLTx+/TyiE8XVVgcSHtFGeY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MdDP/JRF; arc=none smtp.client-ip=209.85.215.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MdDP/JRF" Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-656d8b346d2so407824a12.2; Wed, 12 Jun 2024 19:31:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718245878; x=1718850678; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=j1cpGZH9ekrwOk4ggaybwp6iXeGg5lCIICWIGBwiSB0=; b=MdDP/JRFpl815MlY0tyr04PRA5obt0cLuQWjzK4ZCcohuQ7snAyM6wgwoP4r5TK0A5 fzFSSza+CKVvmmVg49/5oqChEqHqqU5D5ZHOOVKRvQbEwhoxTMGRn88JAg7vaiXplvG3 /VkI+uA5kx46aJ7RsU8oQE5wSc9Mc1eP8DgzsYHLejm4YUYoyDys5YouWEt+N243bsq8 3Oh1/3vx8y7h8GvATkDsSM7LJ7N+oRbs+e6+tKS1Mahdho1pGFg/F/T+d7BbFF1z6gtx LN4fmu0VGYrQtsEm4GxoRxAgtfXNSpYFkBmj5bAT7SUAqVTPFX1n8wR+IlEpdKaaGN45 lb7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718245878; x=1718850678; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=j1cpGZH9ekrwOk4ggaybwp6iXeGg5lCIICWIGBwiSB0=; b=OdV5gqIayTT9VMk+ftX2RcnNTfsl5G3OaqISws+jnEdw8MrUacra6kPAvJ/tsRFmHJ rpNXrOJgjxE40e15F+1PFxD28L0GpRq4ID2ft5va6f8YlmQaZtygxr8RxQdOeQ5JV51h YZd77wCFoZlXSUl5DBjPTE7bMmwwDg+zogRAZ9cZGzdGc6MaJLqSjVcPlZLsy7siPFX/ 9yPEFRdThQ6ScmEssQ1NJJkp/r5tgByG1a2O4QS3LocxGCeT1q9GErIbKdZ8MdrOCWYk SlqmSTFpPaHtA7LBz8CqhsNirkZ4aVlBQCwiR8dSx+oCLtrtWflFCCeZYeuTpCXD3ks8 jD3g== X-Forwarded-Encrypted: i=1; AJvYcCX0Z0que3KmCO753r/vbaxmQ3BRcUR/X3s7mavN8Zm1sn3El8/ubKe2yLb6CooLrmJbA/l+LynUlaJG6/PZ8qHUJaH1P5iyk1446wJ9ns3gOJ1Svb0lC1Ja6MJI4u/nEYEBvqYpjvKpBrJleVjMkiD2PXSBwXgHqQtqDLJYyUyJ9KwgfCQJX1VHWnoWmowRHEi/NbUHjBbSuSg2CRkF+wb2/N2KHoOuCkfm1b22XQ14TC6JGAlwcooIjeZQMGNgbZDbV0EkrM1pR34vmN0J8cpY3nJ+8Twy3TfH9+4BC9FjFCpRLahAm1TjjUMQMPteXgJTsdDl8g== X-Gm-Message-State: AOJu0YznmyK2jyIWcdJ290Df02s4EEPtYsW8U1oZVmxyEC/xgnpmlOxn 9/CmXIje6oPVVngVMaE8jhQ5KtQxSE4r91iVFLCeQN8IuF7mwg22 X-Google-Smtp-Source: AGHT+IHxE49dVlqeBIraoeODhxKMszGj4KYKBCDFAoiQKvyi2cHrcHMBy4aCqKmXdfgNqTCtDPqmtg== X-Received: by 2002:a05:6a20:3948:b0:1af:8fa8:3126 with SMTP id adf61e73a8af0-1b8a9b773c5mr4249289637.6.1718245878051; Wed, 12 Jun 2024 19:31:18 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f4d159sm1755695ad.289.2024.06.12.19.31.11 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 19:31:17 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Paul Moore , Eric Paris Subject: [PATCH v2 02/10] auditsc: Replace memcpy() with __get_task_comm() Date: Thu, 13 Jun 2024 10:30:36 +0800 Message-Id: <20240613023044.45873-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240613023044.45873-1-laoar.shao@gmail.com> References: <20240613023044.45873-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Using __get_task_comm() to read the task comm ensures that the name is always NUL-terminated, regardless of the source string. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Acked-by: Paul Moore Cc: Eric Paris --- kernel/auditsc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/auditsc.c b/kernel/auditsc.c index 6f0d6fb6523f..0459a141dc86 100644 --- a/kernel/auditsc.c +++ b/kernel/auditsc.c @@ -2730,7 +2730,7 @@ void __audit_ptrace(struct task_struct *t) context->target_uid = task_uid(t); context->target_sessionid = audit_get_sessionid(t); security_task_getsecid_obj(t, &context->target_sid); - memcpy(context->target_comm, t->comm, TASK_COMM_LEN); + __get_task_comm(context->target_comm, TASK_COMM_LEN, t); } /** @@ -2757,7 +2757,7 @@ int audit_signal_info_syscall(struct task_struct *t) ctx->target_uid = t_uid; ctx->target_sessionid = audit_get_sessionid(t); security_task_getsecid_obj(t, &ctx->target_sid); - memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN); + __get_task_comm(ctx->target_comm, TASK_COMM_LEN, t); return 0; } @@ -2778,7 +2778,7 @@ int audit_signal_info_syscall(struct task_struct *t) axp->target_uid[axp->pid_count] = t_uid; axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t); security_task_getsecid_obj(t, &axp->target_sid[axp->pid_count]); - memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN); + __get_task_comm(axp->target_comm[axp->pid_count], TASK_COMM_LEN, t); axp->pid_count++; return 0; From patchwork Thu Jun 13 02:30:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13695918 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 574D884A3C; Thu, 13 Jun 2024 02:31:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718245887; cv=none; b=nAhl8Yt8S/IaeTyj+xBKh+ctbhf9hG20A3KRiTxoMYbdAwhvMH2KbtY5xauZP6zhGsxMM3/4XGF7VHrGST2vsvE3TMcCNFQjMSO3J6VrHadjdiyhmwgXRAmcSO8TM3Nft9YBdmqVb5C5Fyh2fq9KQnDJgXl3o+DGdQXc5Fo8NRA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718245887; c=relaxed/simple; bh=yr+SpMmeHSi+vcjVeTr/i3O/gnrB5nRciCU2m0INHKA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FGv3DsgOCMy7yGcvBTipngmGNmMdD6/Ta1RZgMpt2cX2/s1UNVlIIWdp/jefkPOSCXj9DSHXi93iGbvSsQZZxGVb5aquG5srKwmMidowKRhgREUc1agOS+bb+6v3x3+k3FAxtKFg29nOLqD3o6RUS2vPntHb5a5mLC0byfmMonE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=myJU15c2; arc=none smtp.client-ip=209.85.214.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="myJU15c2" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1f717b3f2d8so11864825ad.1; Wed, 12 Jun 2024 19:31:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718245886; x=1718850686; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nQwUpm7TadSQyuNlGOfh8JWtJNghSVDBcHtu3NLgdvg=; b=myJU15c2UsGWlCiRmJxSo0cShxKx8Bg7I2NvrTuLfaB7SpJSsJyjT+rQNRTKnJAEjO /QjtFEAKN1HFFQ7ERINlTzqVc3HkfRO5FEmNEJR/F+Xx7saY7b1MeyEvBE+IzrHU9qok S9gFvRZwo88xAyrRqzSBS2kddl1jUlo81hhC0ahIQyKyGqHYcyPKnJs3w03yL2J9Cv7k d3O0NbN9FJVHU2K1VlI472qzqfywnyXiPpsZi+i82RoTIakEFYkJTTpkpFkOIe/Ux5I8 852E5d/mY9QsZ5YocEQTBCdmOhetbnAiJ3PTCVi3AvWDE+yuJPtdUuZHhjGeMHIz8hOl YOfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718245886; x=1718850686; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nQwUpm7TadSQyuNlGOfh8JWtJNghSVDBcHtu3NLgdvg=; b=LQk6ifHIac/RRjC6QamsKvHLoeOHeaWKgu7sfPJeAOL2KFymoutCLyp3bEImBRohWi uWJku7qZ5XOCfFdrt15LvUzMFtvHYJ9nnZr64eldG2mJZQXjt4dURyT+5uMm42WRnfhe Rx838wj5hnk9Dfrh4HBCCURwN3GriXkEwhNuz7u0otzOQmPS4/ReScqrGHb8EYM6lXN/ qvZSWdgXslzyPi58tKXjLrtmOf/dl6GUizVkQ9D68MBGeI1qK3SIXMOvHz8iOlJqkNOi zAtZOvAdI7QnsOQ4c7vd9zBy3W1j9ES/wvRclbg8+YKM2gwKWXxt2oZN+6GJeq/DzxmZ 1LHQ== X-Forwarded-Encrypted: i=1; AJvYcCXHkki9oTMGxtVZEYysNTF0ZJhiHvK096h0WNoU1RikSV+ABu5bsROEMQgjesafIAuTnOCYxDMlS7MdBX+Ix6RaU/oi+/QeGTzkB8712u4aUh7kt7Wyzwe3X6T4xqaHMjhCfl/o+V2mUEkP4uRxe1vel0G1VgznF7rI8d1CZ74ses5c/yIr2mwwCI6YJY4i3jeg1qcGmNHNTWdFQGUP+PT87Ve5/ZPIGTsp9MmDWv+PcXl3yZjsP61nd+An4A/tBVjT6x380IGRHfwyld4YfXt1Vpd1qKZ+20F/RcV/1d+aD2GGjcHJ8sDdnaS41dNzpxsy8l5S9A== X-Gm-Message-State: AOJu0YywAIV6qyOXu/AyiP/4bu/EnoQJxt2SkmGaP5GzUX7hhkaNsWtA O4pwquCbiGAcPiaiYytDYl700LVHM4+/pyyJgEEvAQlMpb1SRaDf X-Google-Smtp-Source: AGHT+IGMO7B1LQEga8czanZyIFvbKD26XnoiSA9hTxuO951zhYSmXnFwAhLQKN8VIXez5ya7Dmvfhw== X-Received: by 2002:a17:903:40d2:b0:1f6:ed6d:7890 with SMTP id d9443c01a7336-1f84e2cc0b3mr21158875ad.16.1718245885659; Wed, 12 Jun 2024 19:31:25 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f4d159sm1755695ad.289.2024.06.12.19.31.18 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 19:31:25 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Paul Moore , James Morris , "Serge E. Hallyn" , Stephen Smalley , Ondrej Mosnacek Subject: [PATCH v2 03/10] security: Replace memcpy() with __get_task_comm() Date: Thu, 13 Jun 2024 10:30:37 +0800 Message-Id: <20240613023044.45873-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240613023044.45873-1-laoar.shao@gmail.com> References: <20240613023044.45873-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Quoted from Linus [0]: selinux never wanted a lock, and never wanted any kind of *consistent* result, it just wanted a *stable* result. Using __get_task_comm() to read the task comm ensures that the name is always NUL-terminated, regardless of the source string. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao LINK: https://lore.kernel.org/all/CAHk-=wivfrF0_zvf+oj6==Sh=-npJooP8chLPEfaFV0oNYTTBA@mail.gmail.com/ [0] Acked-by: Paul Moore Cc: James Morris Cc: "Serge E. Hallyn" Cc: Stephen Smalley Cc: Ondrej Mosnacek --- security/lsm_audit.c | 4 ++-- security/selinux/selinuxfs.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/security/lsm_audit.c b/security/lsm_audit.c index 849e832719e2..a922e4339dd5 100644 --- a/security/lsm_audit.c +++ b/security/lsm_audit.c @@ -207,7 +207,7 @@ static void dump_common_audit_data(struct audit_buffer *ab, BUILD_BUG_ON(sizeof(a->u) > sizeof(void *)*2); audit_log_format(ab, " pid=%d comm=", task_tgid_nr(current)); - audit_log_untrustedstring(ab, memcpy(comm, current->comm, sizeof(comm))); + audit_log_untrustedstring(ab, __get_task_comm(comm, sizeof(comm), current)); switch (a->type) { case LSM_AUDIT_DATA_NONE: @@ -302,7 +302,7 @@ static void dump_common_audit_data(struct audit_buffer *ab, char comm[sizeof(tsk->comm)]; audit_log_format(ab, " opid=%d ocomm=", pid); audit_log_untrustedstring(ab, - memcpy(comm, tsk->comm, sizeof(comm))); + __get_task_comm(comm, sizeof(comm), tsk)); } } break; diff --git a/security/selinux/selinuxfs.c b/security/selinux/selinuxfs.c index e172f182b65c..a8a2ec742576 100644 --- a/security/selinux/selinuxfs.c +++ b/security/selinux/selinuxfs.c @@ -708,7 +708,7 @@ static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf, if (new_value) { char comm[sizeof(current->comm)]; - memcpy(comm, current->comm, sizeof(comm)); + __get_task_comm(comm, sizeof(comm), current); pr_err("SELinux: %s (%d) set checkreqprot to 1. This is no longer supported.\n", comm, current->pid); } From patchwork Thu Jun 13 02:30:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13695919 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9450C84A3C; Thu, 13 Jun 2024 02:31:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718245896; cv=none; b=qaom8+J1mU9J55cHlegCIAh2kHzRuau+ShYTC0K5oIXNglVyq8EURFEsGdfDTqZE0FfP3p3dfCP3CkQVYb7DtTYp1O11Dp1D/dKWT9RvfXerkEa9XYTbnOK2BkdI0aaS12eI5G297uaixa/eNyCTPDRR1D8O8y3pQEcXY/l+vzY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718245896; c=relaxed/simple; bh=a+jRmBNObp02hqUJFQx9ZHvGN1Vdde7wossr4Oq8bKQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IDbP1YTyk4DDCYPXmlTSSmtUQdwpPvnVbVxySjNJXHnNqyEtN60n/yHHeujaSoRmh/KkRGZ4UzhK5W9LB9KT72rtEljks+lo5jIUxxN3dcVg6hJnc/YSnaCCandpkWRjDY1IYYH6WSNPhfTteA2apR/rxx7kJksFFlkSAa7cTZw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=RIVwInO3; arc=none smtp.client-ip=209.85.214.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="RIVwInO3" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1f6fd08e0f2so4343145ad.3; Wed, 12 Jun 2024 19:31:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718245895; x=1718850695; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=c7QH8g7nY2INvVrL4NU75ftNhN6FL7HL04IiCAjwJmE=; b=RIVwInO3xhEVzXqjC/TRPFAPuEFWSPZSFsFvpRDvVWcbiYTUcMcf3O4UhuP/QvhOKo mB8gwhgpmgMB1ux0mNrslgdeknSjtnp4RLwrg6+DzHff7WtbiUif+vKIeggEIYET8ktT G2TbQm209GmZNJL7zwIOUOckvs0pZP9C59ptzYLxAmozct86AApYidLN0KiRFqCx5HeU 8wjnCb6ewGtp0EgwgUuxozNHFwNhxvCXxXzo9PpyQyiTJkXd2pEVVNX8oaa7/DjVaCBr +t1BTTWV46Th7i5DNrTbTBn04Udb9CNydBKvbtR1LmgfSGAJjyc6R6jZfmFtZue8laHn PW8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718245895; x=1718850695; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=c7QH8g7nY2INvVrL4NU75ftNhN6FL7HL04IiCAjwJmE=; b=t0s0Wnf5MRSsZaKhH5wu4K7E/jWfVtc2+toFi16IvSF8AOUtlyW/gz4gvIU2FrajuA ASKmFerVA/qPhT2kR3zIpoaBcKfxFbX9s6W6yNh2oov9KStYuS0WcOczU+6FTAGUXELF nE6AqmiI+WSCnLQwcNfsg2xxZuLVBF7COLgTkVE10oL6mmN6b8jquQ1H/EQOQN5WV2ew ziznG/GvNDm8a24Q7HU41cDstG0w7W7OwkP0PG5gamehAKMvFRgVEntoi4e30F10RLjl 2dBQInzoru61/1O2XENI/WqfqTph4f0RpfAHoHpZOFLQzznoseAinSW5KmCBDFLAZetG wEsQ== X-Forwarded-Encrypted: i=1; AJvYcCV73j41Ch3sbTmizNFJbiZITKvjca56Kmv3SjqaVVLIXneBAzVWYxOjGsRuacZOUU+kn+mcwUuapqY72LVNzDjPURXzFHI0PfAMPRzj7pP45AUX0ykJ1EfR54p0LVH4PWCejAk5wXT2spMacBh1DN55cQZLDEmoKAOGE04hizpZQvjdO8GF6mUgVX0iHDOI53SSowO9YcZpJCLLX2lKCtLy29iQ1jz37RHwef6Fl2CJXkMB6XvXuZ3GKYmqMEe4uA64IVQzgFTlssXXL3e5pNwKwKpJACEZv6mkYtzTIwLf0g0d7hkvzhiHo4mDRMSrALPtTWsb/w== X-Gm-Message-State: AOJu0Yx3CMjRO3PzbxY/1sgJK/3tfIEbRX24VXvEWrIrxlAVQzXIiBgd s3ePcmur41FaVRR91R7KDPEXpNyNG2HxcjAfT+CUjDE+5Sb92sCb X-Google-Smtp-Source: AGHT+IFO0RiQEuS+ZZx5NhUKFkro4pSWALq4fQpBCAuUBHe6UDJpEGOEOlucSptFPDnQxh/E7HkfRA== X-Received: by 2002:a17:903:2283:b0:1f7:2051:18ba with SMTP id d9443c01a7336-1f83b565dbbmr48444305ad.14.1718245894762; Wed, 12 Jun 2024 19:31:34 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f4d159sm1755695ad.289.2024.06.12.19.31.26 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 19:31:34 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Quentin Monnet Subject: [PATCH v2 04/10] bpftool: Ensure task comm is always NUL-terminated Date: Thu, 13 Jun 2024 10:30:38 +0800 Message-Id: <20240613023044.45873-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240613023044.45873-1-laoar.shao@gmail.com> References: <20240613023044.45873-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Let's explicitly ensure the destination string is NUL-terminated. This way, it won't be affected by changes to the source string. Signed-off-by: Yafang Shao Reviewed-by: Quentin Monnet --- tools/bpf/bpftool/pids.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/bpf/bpftool/pids.c b/tools/bpf/bpftool/pids.c index 9b898571b49e..23f488cf1740 100644 --- a/tools/bpf/bpftool/pids.c +++ b/tools/bpf/bpftool/pids.c @@ -54,6 +54,7 @@ static void add_ref(struct hashmap *map, struct pid_iter_entry *e) ref = &refs->refs[refs->ref_cnt]; ref->pid = e->pid; memcpy(ref->comm, e->comm, sizeof(ref->comm)); + ref->comm[sizeof(ref->comm) - 1] = '\0'; refs->ref_cnt++; return; @@ -77,6 +78,7 @@ static void add_ref(struct hashmap *map, struct pid_iter_entry *e) ref = &refs->refs[0]; ref->pid = e->pid; memcpy(ref->comm, e->comm, sizeof(ref->comm)); + ref->comm[sizeof(ref->comm) - 1] = '\0'; refs->ref_cnt = 1; refs->has_bpf_cookie = e->has_bpf_cookie; refs->bpf_cookie = e->bpf_cookie; From patchwork Thu Jun 13 02:30:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13695920 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 81D7281721; Thu, 13 Jun 2024 02:31:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718245904; cv=none; b=dq9+iMVt+2PzcXR7vSXohNxetD2qGgxE6EEhT3x8hCEoOJCsTm079riysHzXYiifIRcgLRZkQkzdeApIgsVtK70yynFIMSQJ5Eh3+zFvRQwYuFRgDsgfL4t8ZBkfd6dk5//FAJw2lLAEs2MHPYt/NhOTO4qYNlkbeoDwUbN4J8M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718245904; c=relaxed/simple; bh=g1/txTsR5nMIIXRnWhaYOCg4jtbwbYBXW19DaxxjazU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZjEChvjqCSvszJE5l7WrxiAqEoyYYsZAW+DdVjQMGcIxA7gmVKQ+lKVlaj8VBwXeJX6S+DaHldhcKtqRSKTp1uYfcHrlf/X8wMlwbMd8pp1jz4OOVoQdRgPWuaHnbsgi4a+PoJNg+qVXdiH7LVvu4F6JGe/qm31AYwJAncrrSjU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MyblUz7T; arc=none smtp.client-ip=209.85.214.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MyblUz7T" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1f4a5344ec7so3677485ad.1; Wed, 12 Jun 2024 19:31:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718245902; x=1718850702; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Ei/8xwKAZ1rE7ZNdSGJjlup34y97vbtFvnVfEPyjJ8Q=; b=MyblUz7TFxfkFWL6y2XnJ0BLQcU7m7eUORob6njDLq1udJrWHYUIUd/1A/1P0jnCi1 mpQN5mpprj9p19GonvxCLbRjXoYkHNBLPSUWYuAEF2ELkCxPj76QSd/WLYq9NLRO8Pgy 6SKqRNR1XeTXTuEu19xHJQyDxubZrwyDGrcfGd16nPdI3bKu9d75buaxYzusE6So6QIe fKAkFuiTbAlhMtdfRwgdMLuGkXzOlqQhqxnvgPDBsbZ1pbd+d5pGHPcu2frt28E0oDEh mLBALIU2+b0XW14DFrF8nSSxh0zUTr1wGY/KAzWQBpAKQbWUqDnjV71M5rkEkgB/mcHW TFYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718245902; x=1718850702; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ei/8xwKAZ1rE7ZNdSGJjlup34y97vbtFvnVfEPyjJ8Q=; b=uUyakDL2cPwdZnk3tgtFvwM6xJe14C6UWpg0EtD8773/Lxvi3qJqh+u9UDsSzkcP2k CT5VJNkALq47oafq1wrV9jGH/Ez+8aSOUIn2VosiKsq6rZ+LNgdf7vpiFJoNTxaPBL1o oYEC4Cp14Ghws0Pmg1OmUiCJs9vUPzO/dROXK/2cGGsmhu+it6g4VeUdU/4uL6nF5m0T iOdU8hraJoMX+AwafdEhW7FrQ9AAWWYR3wvjH+rcBzk6K/cy8OebCHOpiCV/M03t9u4q JNMgZihLqAbOCoKhQkl1gPfZhAltp/09qwiMLtu6HfeRhd45bBaiT6lBiwCxdkj2fjiQ 7CuA== X-Forwarded-Encrypted: i=1; AJvYcCWKsp1/++w/IcsMZxpNJ6eei2KdaMTu6QK4y7MJs9IpHfunX1nX1brkMnIxyqaPdmNwzgm57KFo3LeGhK41vv7Q5VvS7SLHRXIIBngacG7EgSHy8u4PGAW/R5+RJsFmFdUuGNVJG9VKRmtTDm18q9iVi45NeSEzNaUx5WqJHL36hlNHDzLSX4ZAit+EjTeUaFVLS6YpeSB9jZMj9e9b/MqvfTUilAtY5fAsp7mxPVG+u/usvvqKfKl/sa1Xt/15Uvglvkt1pg7h5bHFjqoxvB/P96FR536EwtQjswrjDGvFlzZNPtPt0QjZ/0J+B1Y8pB1tRIjv4w== X-Gm-Message-State: AOJu0Yx4BAoNEigfu+TDoH4S6G74U3rBK5t4PeUrAzmaoT6eDXzHGu+v gyh9P7glM4GXb8nmdaiwVWDXlvqHFwXlXQM8xauyHrlL0T4C/1sD X-Google-Smtp-Source: AGHT+IFIaVarDVsj1gEbMfPNFvaRxIMZj6V1FUrejF3TL24xnCXwEw+tDOS2CgmUK4jtVsLBUQ40Ug== X-Received: by 2002:a17:902:db11:b0:1f7:1d71:25aa with SMTP id d9443c01a7336-1f84dfbb4cfmr20547645ad.6.1718245901789; Wed, 12 Jun 2024 19:31:41 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f4d159sm1755695ad.289.2024.06.12.19.31.35 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 19:31:41 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Andrew Morton Subject: [PATCH v2 05/10] mm/util: Fix possible race condition in kstrdup() Date: Thu, 13 Jun 2024 10:30:39 +0800 Message-Id: <20240613023044.45873-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240613023044.45873-1-laoar.shao@gmail.com> References: <20240613023044.45873-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In kstrdup(), it is critical to ensure that the dest string is always NUL-terminated. However, potential race condidtion can occur between a writer and a reader. Consider the following scenario involving task->comm: reader writer len = strlen(s) + 1; strlcpy(tsk->comm, buf, sizeof(tsk->comm)); memcpy(buf, s, len); In this case, there is a race condition between the reader and the writer. The reader calculate the length of the string `s` based on the old value of task->comm. However, during the memcpy(), the string `s` might be updated by the writer to a new value of task->comm. If the new task->comm is larger than the old one, the `buf` might not be NUL-terminated. This can lead to undefined behavior and potential security vulnerabilities. Let's fix it by explicitly adding a NUL-terminator. Signed-off-by: Yafang Shao Cc: Andrew Morton --- mm/util.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mm/util.c b/mm/util.c index c9e519e6811f..3b383f790208 100644 --- a/mm/util.c +++ b/mm/util.c @@ -60,8 +60,10 @@ char *kstrdup(const char *s, gfp_t gfp) len = strlen(s) + 1; buf = kmalloc_track_caller(len, gfp); - if (buf) + if (buf) { memcpy(buf, s, len); + buf[len - 1] = '\0'; + } return buf; } EXPORT_SYMBOL(kstrdup); From patchwork Thu Jun 13 02:30:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13695921 Received: from mail-pg1-f171.google.com (mail-pg1-f171.google.com [209.85.215.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A941512C468; Thu, 13 Jun 2024 02:31:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718245912; cv=none; b=VlkwHQgrlb27kBiWtk/438hVnGbYC1WwBUTZeGUAYL9j+FBHhNfF82zBS1UdlbDgEh8yfUInRpUJVv6FtXyGHCEsWZMVYD0p5j79nyiAJrBcc77RcTKPrPO4UoL24ZF+AOfw3bYGYcogkV1wyIFcb+2KaLF93l2ivHdw0JoZaRk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718245912; c=relaxed/simple; bh=+dQ99ROKczL9d1RCLteLM9GCbPMd/sWNoUanj2AALC8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bSr/kC3wat/TtasdkE9bRgY+7SuKdg8snJF6KKWmRWlaeP0w4QnWZRSxNbLeQxXeieYuiBR4eJxBPH1ByDX1ugHoggyKXz9NpT426ICYQVuHyoIfccJS71W61ZjRDL36FZmF7iPCOZjEPbMv6gRr3/ss7ptRQ2l60vXIW25B4fs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Y0Kjamqo; arc=none smtp.client-ip=209.85.215.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Y0Kjamqo" Received: by mail-pg1-f171.google.com with SMTP id 41be03b00d2f7-6c4926bf9baso448071a12.2; Wed, 12 Jun 2024 19:31:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718245910; x=1718850710; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=rI/qiwYumUf9WdznWs8PjjNV1aSEYVRYeuHEKQmKc7Y=; b=Y0KjamqoH+K4NlliQhFv2UJEvWzMnXXZSdpxNzotQKPW4PAFLSAz0mGhwl7nIfdwQZ Djm8Wy4NYlh4xPEf/u1N8VZAqPSW98gXzR1fkBjIJ5IvtCR92KO55JrIvmBL/MRiivjS as8KCFKqtfFxl4v9oifn3dK+qk9rWu4Ss0of57QViOFc9+nA9knKi7Yde4fbv0lFIDHA R2u9FeuC0/JWo2jztoKIcFlw+Ts5+SWAa2XvjflfriruDy1iyzmQJJrA4UkvPWePrplU UEKw2cHXDXmQtm1qClr5Bvcf3okaXmCv+eUdPlSmmOckKJL3pgNlA7/+z2EGw448hXLP o88g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718245910; x=1718850710; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rI/qiwYumUf9WdznWs8PjjNV1aSEYVRYeuHEKQmKc7Y=; b=wMxCbuC3ib2WhSU2AZems/ikwU+7wv2m1oq6s5j6Vgs2rNGz03dhXByVsz9gvCT+7o 74cGsMmWGq4f38L7eoIrAxxiCQ5o3dwwSh12QI0sC6ALpxTEf3z4ZsWGOl2g8Hmu1u+E 9tuv3TJKK4f870xf/aQKPSwRkl7x0I+rHj9VUTdIMXw1k/2U5BSqDoeBJ31Mvy2VtII3 qCpWf0KeuiMo0aHlF/pDE0+ObmOWdD384R4zYtOO7tRZPMvHMF2NvWRdCJhJiVXKSSRB GSkdYRu/x3kj8BANTkxYJ+02OmcvFI+DcNBvrYFlVh19YxMy8884Bsiz3DP1/tatH9Vo DG8g== X-Forwarded-Encrypted: i=1; AJvYcCX9E0OTPRZ+U3ThL71BXEkazXQi292ZADihiM+ql9v+Yjy/ZNRm8QN0LPOsRu/PjSVH453v9ALrErhLc0htVVDQ3iq2PoVYRujDatniAMeWE/G5Q15tFagDWVSkaBWnnsjbM46e+SVRhqD/k+GkNV2zpFmpDGCGFadPBWw3lmst4EYKmsw2DhvCWossJw96hewBQL6amE8GBujiHFodhw3+e7XAGQmAHyB1KWLM9lwDBlGhqcSwSeISFa0aFtFKDZ424H+PMIpLIOLYRfrI3g0s6jn3VMUoBrVrfYsHCU8ukz8kNIk2RTaNr/4wpSzCMeimz/1vXQ== X-Gm-Message-State: AOJu0YxYcqrx3Jk+xADeIogMivjW8jnj+yehpY6F2kT3oP6Qju1qPYsl 36kd7GeYcPdYpBD/w8A0nadVrcMlCQ0CefQ33YGQwrUg6rhg9evx8l/nFaDryfY= X-Google-Smtp-Source: AGHT+IEHZPxKX/RP4SBm4cAnIghTfWMy7FOuVWWLSjXVUtL6OKmdClkmrYCnnZGc6EFhsF+GbPMHHg== X-Received: by 2002:a17:902:d2c9:b0:1f7:22bf:57f4 with SMTP id d9443c01a7336-1f83b730b3dmr43536295ad.55.1718245910002; Wed, 12 Jun 2024 19:31:50 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f4d159sm1755695ad.289.2024.06.12.19.31.42 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 19:31:49 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Catalin Marinas , Andrew Morton Subject: [PATCH v2 06/10] mm/kmemleak: Replace strncpy() with __get_task_comm() Date: Thu, 13 Jun 2024 10:30:40 +0800 Message-Id: <20240613023044.45873-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240613023044.45873-1-laoar.shao@gmail.com> References: <20240613023044.45873-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Using __get_task_comm() to read the task comm ensures that the name is always NUL-terminated, regardless of the source string. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Cc: Catalin Marinas Cc: Andrew Morton Acked-by: Catalin Marinas --- mm/kmemleak.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index d5b6fba44fc9..ef29aaab88a0 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -663,13 +663,7 @@ static struct kmemleak_object *__alloc_object(gfp_t gfp) strncpy(object->comm, "softirq", sizeof(object->comm)); } else { object->pid = current->pid; - /* - * There is a small chance of a race with set_task_comm(), - * however using get_task_comm() here may cause locking - * dependency issues with current->alloc_lock. In the worst - * case, the command line is not correct. - */ - strncpy(object->comm, current->comm, sizeof(object->comm)); + __get_task_comm(object->comm, sizeof(object->comm), current); } /* kernel backtrace */ From patchwork Thu Jun 13 02:30:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13695922 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C3B62B664; Thu, 13 Jun 2024 02:31:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718245920; cv=none; b=R3ZA6bNdE48ErC5hliZenH7FpAi+cXf0agqYakJDOLXDqq3fd4UO8DCYNwIM+IAqbLK9Is4aLbCSSERs27QpuCsMp9aaWNh6Lzc5wsokZi7/VSrc4zz7sCJty7QyTvF7Eo+cLMbp+3tVQnXLeZVjO/4rvdsJ7bFggoTg/czFOiA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718245920; c=relaxed/simple; bh=xJqJZWbEhk8Nib3lnX/uTBcWIy8/xwBU6awufruH+d0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rhzgCHewfrJBB+NHhu3/zaZusD6WHuZDTEXQ9yhXf1U0vYtdvGuoH1sJDVdIw+posP6jAQ+O19teruLv2jQ9HuD8igF7RvnAd1zoivfZKdDWQyVMSV7j9z5aTXKHs4xa2N3FoPtAFYULSSGU9JDpboH+AhsmyCSv4s1mBCpN09o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Gcy2BYQy; arc=none smtp.client-ip=209.85.214.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Gcy2BYQy" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-1f8507aac31so3954225ad.3; Wed, 12 Jun 2024 19:31:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718245918; x=1718850718; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=MqSWr/pfZwtTIRxMBQN6sYuDk2R2VvioPDgEPjsCPow=; b=Gcy2BYQyOsefyUte/EUfJJOCRpshy1zp/x4bERJEDGxNFYW0qsy33LoZLlyvm7J9V+ H1vQIPXMrEW/O4rDNO4jWp6W1zT4U6Fq0mM9DGtPM7j0c4AyIK4pIGfOKtWOH7azfmPQ Qd+ngLWEFwFZ1jR8a+edjDt7+FqqwxX+Saf+p1qmOyfUyz4+DzALwwVQ0K4FxuHJSf4Y YvBrirpwsCfJCAqME6GyrkBwIR4t7lPEv16TuTZ7iGCYTa1XxFvWM7yjThNYYdhEUxDJ C45MyZZSc/KAU8pWRnZC8LkHXGHyV3qgryebeTUuxhmRL+zyNMFoksxjYil5iGlWt8RT KI6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718245918; x=1718850718; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MqSWr/pfZwtTIRxMBQN6sYuDk2R2VvioPDgEPjsCPow=; b=YhrMilOtkkZ30FfZFKww4pz3sxu0OqYZJ1684/zi4k9I/z9/1XgyrmJtgc91dbY97f 9MSJbxsBS4lPOEBDg4cdZdHIPg1IOk47vBYF4hdkiZ/FnKzBiu52zc5s5qzPzvLgdyw6 VzBjVPapyqJgwbITbiL6wYx/vKVZM6xGMctFVFQswQxpm7BzoMqrkih27FEtoGATVsoO 6wOW4lzEd2Ey2Yznh+uEJJ+PrCctjsIBVyDFIsUmlY+Og4KwW3TPnHU0WC2phX87admX gare5vcwjFzZMdlgJAXvNmoIWQIwK64HRY7rdVTVoPK5XsV0FWwWyb9pW1poOzokLkVW 8jTg== X-Forwarded-Encrypted: i=1; AJvYcCUWNP/Yp50yOmNNKOnXGUKtkLa9sgsvuAVHkQ6K3NmBX0zJhRM2Y3bNKg8H6yx8smqXjnu/Hm4NnzpHsSkPsWc6UypzAXuaaLNCXszsoz2cnc013QKj+B3oZHXqQfuxeCegSvZCh91aoVJgBcAGbJcvsaE8okE4sFSBgoHTae08fe1ZKttDRSUM8+/07UmEsU9vPDGYxFmh5Kc3nZ/L8PVQQ0ixzBGFFbvAGXB6CmBZztX9+XjldPemunZJ0P/5zD5uFvdILobk/BywMnxr8n/Cu7u6C9ZMIKSMu4sIij9dueIyj6stMl9tgEN7kElGNPQtQGPeOA== X-Gm-Message-State: AOJu0Yw3XJUvbdsqQwkDaYS6Od17hHYc5M2OaVkOJaojc4gVmAi0ZrpZ pt+LoaahbOo6BNcjppF3XdN17f+/Lti+L7Kxx2d9qUber31/DQB5 X-Google-Smtp-Source: AGHT+IF+JYjBn0OjZmgrQWU2wgsy8ofTmlVYqxM2KmLwEfyu1c8K7LtQBzzKH0FH1zCzrNJ76ASDQQ== X-Received: by 2002:a17:902:d4c6:b0:1f7:1893:2587 with SMTP id d9443c01a7336-1f83b55b817mr42002945ad.14.1718245918153; Wed, 12 Jun 2024 19:31:58 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f4d159sm1755695ad.289.2024.06.12.19.31.50 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 19:31:57 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao Subject: [PATCH v2 07/10] tsacct: Replace strncpy() with __get_task_comm() Date: Thu, 13 Jun 2024 10:30:41 +0800 Message-Id: <20240613023044.45873-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240613023044.45873-1-laoar.shao@gmail.com> References: <20240613023044.45873-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Using __get_task_comm() to read the task comm ensures that the name is always NUL-terminated, regardless of the source string. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao --- kernel/tsacct.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/tsacct.c b/kernel/tsacct.c index 4252f0645b9e..6b094d5d9135 100644 --- a/kernel/tsacct.c +++ b/kernel/tsacct.c @@ -76,7 +76,7 @@ void bacct_add_tsk(struct user_namespace *user_ns, stats->ac_minflt = tsk->min_flt; stats->ac_majflt = tsk->maj_flt; - strncpy(stats->ac_comm, tsk->comm, sizeof(stats->ac_comm)); + __get_task_comm(stats->ac_comm, sizeof(stats->ac_comm), tsk); } From patchwork Thu Jun 13 02:30:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13695923 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 245DB33F6; Thu, 13 Jun 2024 02:32:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718245927; cv=none; b=rtnM8D1ISiJzlH7mOuamF7xz0okpX92qVf29ZX16hVOJgSaTA15NEnQOM1zyPHKGXOZA3jQ7D14kJrmtyx9sbfUflsH3uxPXpKT9i9n9lFYtaR6MbcSVTHHnPSLB/KTElrq6c8JbV1OlzfrBE1bm1ZamzYyYq5yC2Af84PcuDuY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718245927; c=relaxed/simple; bh=/gCH7e28NIl/tMHPCXOAQ7VQOVOI1s0RashVXkQPLGY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=e1i0nrn7v77gD3XrP28ELMMTCieUhDGlOIBGGuFAcErBm46P/dWu6wwnX8jVo5gbgUxEyurj4X9WwL5CrEEqWxns7l1RB/qRnQxkJuvh0lFjmSJT9OkEbbaT/QSKqfJaKL4JkHcvEQeUi3D9TX0mdSv8LKiBE81h6z9JMV5QTuc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ahzSj4bB; arc=none smtp.client-ip=209.85.214.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ahzSj4bB" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1f4a0050b9aso4929575ad.2; Wed, 12 Jun 2024 19:32:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718245925; x=1718850725; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HQT31JSkonb7Xmqn+k1uqrjgngeoHr6IFKrX/+KN6Ok=; b=ahzSj4bBabw5mTPe2ui8++fY3UPhZ4793SJtyzsXDMY4mm/KVBk3Xiy7Ik3mY+U0VW yllAsNR9DdOs12k/b8ivorcWkdiGSlM3qW7l0koN3NytmIekX/VFRR2onZhvPVaJxyYn 0Cybg1pVvcgAUWPJEaIRZPm3ThoCOqRZyLj6Vqyu+WkDyS3hiVj6mhPQf0gWrJxFrme5 pkMYT+yAmZjk+ECzN+PQUMOiACWVjFTBjedbyOh+7zkMyZP49fAnidhYCCAik4ZEi73H rusWyGDmssIQVQoc0TNeAuNn7HtDAHGSkC7Jiuk1rmTsAWs8bEXFcWlkwDMKXiH0fAWI PQ6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718245925; x=1718850725; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HQT31JSkonb7Xmqn+k1uqrjgngeoHr6IFKrX/+KN6Ok=; b=YfNzyC20TrQg7qwea8UixKmh6yCy/Dn4AVm6GMeL5qXv5XguKOtcEMlfHZaKt8Yofh Dr4THKp6zaZ6FNrjIjlpaC8mRown8wwb/RgvoNxl0u42PMUJdKZx0/0M/h1mxdOxXJ15 bn1ntBTP29mJR5guzvm+KeOmq9KiGlH5cRvhtJ7oD6d53QkJDVLkEvEWMLvlSsQ8qYYL DK8i7PTB1SYVpCH8NCstzuxGZaXeSQz5enrd+00BMMRrU70xut2lvm3PNPAgZcf36ucH txvLmfyxQAPE3lRnGJpppfurgqPLE+DWjMkQObWMjD4GeUCDZK+WQ7OuHxB2GqNXQleN 5xmg== X-Forwarded-Encrypted: i=1; AJvYcCXpdo++oAtSQzFhaNB/0+QScE5PcqNfkUBWD1Wq8ETdrhB+JGrqaM7W3jfRs28v0b+ORw37rLi5fJxJkGHJu4VwOT5QltjmYtjKvEuNv/lxQ3MC+E6QnUAihy26esSQeRhKG120M1lVBv29rVULkqFQjcyExrV9IQuYyJtxDZNJslxeodW54vlbsu9FiNkeWSN/883uHcX4Hci4siKyn0I4pB9/qlplEweH+YfrqN+aiLM8QSXLRBL3qtRbFmlZ1LMP5Iv4YIQemi5IYvwpK8+2bFrUl34y+P/fcWA3yJH42GgNuOQawXGnXS5XArRgfeG0bvM27A== X-Gm-Message-State: AOJu0YwOMg0labgbRUlcJbn92Ka5CVs36uLCAveTS4HEGB2yFz9BHGdt DrAAT9n89yATJ6vrbSZO/S6GuyPR2hxIk+umbCSflsA0OL8XZ2vn X-Google-Smtp-Source: AGHT+IE/Thj9zy8OzNEgltY85A2YMlP/9PBe6RK31xV6C4QSBrLzPw+agMFJyhDUlBI6xydC6ujXaw== X-Received: by 2002:a17:903:1c2:b0:1f6:89b1:a419 with SMTP id d9443c01a7336-1f83b5583a6mr41814045ad.17.1718245925584; Wed, 12 Jun 2024 19:32:05 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f4d159sm1755695ad.289.2024.06.12.19.31.59 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 19:32:05 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Masami Hiramatsu , Mathieu Desnoyers Subject: [PATCH v2 08/10] tracing: Replace strncpy() with __get_task_comm() Date: Thu, 13 Jun 2024 10:30:42 +0800 Message-Id: <20240613023044.45873-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240613023044.45873-1-laoar.shao@gmail.com> References: <20240613023044.45873-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Using __get_task_comm() to read the task comm ensures that the name is always NUL-terminated, regardless of the source string. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Cc: Steven Rostedt Cc: Masami Hiramatsu Cc: Mathieu Desnoyers --- kernel/trace/trace.c | 2 +- kernel/trace/trace_events_hist.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 578a49ff5c32..ce94a86154a2 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -1907,7 +1907,7 @@ __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) max_data->critical_start = data->critical_start; max_data->critical_end = data->critical_end; - strncpy(max_data->comm, tsk->comm, TASK_COMM_LEN); + __get_task_comm(max_data->comm, TASK_COMM_LEN, tsk); max_data->pid = tsk->pid; /* * If tsk == current, then use current_uid(), as that does not use diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c index 6ece1308d36a..721d4758a79f 100644 --- a/kernel/trace/trace_events_hist.c +++ b/kernel/trace/trace_events_hist.c @@ -1599,7 +1599,7 @@ static inline void save_comm(char *comm, struct task_struct *task) return; } - strncpy(comm, task->comm, TASK_COMM_LEN); + __get_task_comm(comm, TASK_COMM_LEN, task); } static void hist_elt_data_free(struct hist_elt_data *elt_data) From patchwork Thu Jun 13 02:30:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13695924 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A2C3B664; Thu, 13 Jun 2024 02:32:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718245937; cv=none; b=ig7QDpqrXVTkMU/sSvdWjaEGoKyfdI3ezb7dSF3CcQyuuQPkKUysVaAG7j7vhxQN/N9sIj+q3L3E9p/OrlFvmRDCyoQ8nF5eIjjDGwDSPe9AH4xV4viDzWFb9nTjmYdp9IHlDKtHkbdhUhMkjMNvFEuOA7spjVu29cTLnMl04hM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718245937; c=relaxed/simple; bh=TWuud7jK3hME1+ZYOV8l3v30JtoJYeBhxXk+ueWY4nU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YxCKyxOXv0IPzsuRwCggf5ijDMbOYWkvj1xlqpO1hbSJwsapQh/ITNapRpwaWldmNU6pwTDIsANAlZvzICpAlMfdlaZ8aw5d+hogodHHCP8nN2jydyWWd5xwaJFPnOk2f7piBWUTHNX6dIRedSn94g04fq1ZH90h0RX1pdlUi3Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MqmpQnVi; arc=none smtp.client-ip=209.85.214.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MqmpQnVi" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-1f6f38b1ab0so4496965ad.1; Wed, 12 Jun 2024 19:32:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718245935; x=1718850735; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wLV4tdtvKwjUMxvLd3MjEerWvvi0S3KYqORy/5kgs/I=; b=MqmpQnViA3btrY3puGSfBvCJAvfJ2Iovn1Lr6IKTCp6IXBy79oeV+Bk+FzP8aRFBMp PWCuicLk1rSu7ynri8m5yc7Nqd9tADBwlQZE0tjrvW+9VQCVf9Ohlvdh/SKuvEIMqKhP 3uYXjNjDj948wvqhiK+/SzQ8lqqF3dfwgm/sHK26L9Wq/+aIkXngclViccGODdbLxjv5 58wvsH83+EnQEeZr4G/dlscYQUkYwfES4cXoHw2F0K6CkMqvqRD28Sgov+S1jTVtfNlW qAsWVzYKXXfljjS9gGPUrkrmXZYdpdzjWCWQZ31Z6gbeYt0ivCZY0xkkBzNL9ShcvVKn L+cA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718245936; x=1718850736; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wLV4tdtvKwjUMxvLd3MjEerWvvi0S3KYqORy/5kgs/I=; b=RnJyO2kzov8WlDi9w1bz7BR6Qbnwh/vzC4P8yof5gfFaPawsLwrcqz5+57sP2hTaqI pkG86uYSuaxu+svU4I8CZjULMTLMuDS7LVpj/PHip1YrvckLNgYJfhIn9USYM4RbxGmX PQ/jCFiwntfOP748hQBnD2+4eQCCsZ0ZcjAXzQ0yRI5FM/cifzAYiYZSiXV3V4FdQnN/ DmFJSyRVkWxm5wqwWu0Ha2pmOF3qZ02cEIFMFv9QhkiDt9Pk8eD5CwRUG3cWWaS9x28v ZFxfE+KDcXj4Xd0Jg1gWYHSRlLUqSTxgopsipoxaD7DTq0iXWh5TmO1+P3phha1gFufy BxGw== X-Forwarded-Encrypted: i=1; AJvYcCX5381DHe0PNMv4Lbe4TOrgKRvfmvZl1zfC2CFqg1Cir5EinnFSYpdBBpqfqxHrN9pUjFKZ8PB/sMt1BZYgfGjwDZoeOFKZ0Dq+KfOA5es0fLGZOvpRJlS2+XqOl+mQ4CFj6oTMc8IQ+7GOlG0EGzBGuUJkxbj0RYI/akua6ZU0M0LvwHzfyqd2QcFIxAv6znunhd1sFA88Yv6qFBmfMEqcbN0Izsf8MV+pXYvBIcEiPwLpUOaLh6iVf9Y7fTZ0guoynhqKDDU8eJN4vtsfDKpPe32u3IH+eIqprkoqbMc6LdNgzX7cnwvk9JrvUzM5ep/8ioaT8A== X-Gm-Message-State: AOJu0YwmR7JVtZoSYKz3L5S36ZLov9iB+hoLlmppkE8LI+rUxNpxFJcZ n/Zho51YyKHs26cCDK7fZz0rzE66C0n1qNnHjOgaGMGGrYbmt8C0 X-Google-Smtp-Source: AGHT+IHU8nljO/XpjX7VaEB7PAOC5fyOplOexRJH9EcW/3foBQqPSTf9j2DyRFgOTFj3wqtthRwoDg== X-Received: by 2002:a17:902:d4cb:b0:1f7:3763:5ffb with SMTP id d9443c01a7336-1f83b74d134mr37873815ad.59.1718245935611; Wed, 12 Jun 2024 19:32:15 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f4d159sm1755695ad.289.2024.06.12.19.32.05 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 19:32:15 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , Paolo Abeni Subject: [PATCH v2 09/10] net: Replace strcpy() with __get_task_comm() Date: Thu, 13 Jun 2024 10:30:43 +0800 Message-Id: <20240613023044.45873-10-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240613023044.45873-1-laoar.shao@gmail.com> References: <20240613023044.45873-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To prevent errors from occurring when the src string is longer than the dst string in strcpy(), we should use __get_task_comm() instead. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Cc: "David S. Miller" Cc: David Ahern Cc: Eric Dumazet Cc: Jakub Kicinski Cc: Paolo Abeni --- net/ipv6/ndisc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/ipv6/ndisc.c b/net/ipv6/ndisc.c index d914b23256ce..37fa3b69df45 100644 --- a/net/ipv6/ndisc.c +++ b/net/ipv6/ndisc.c @@ -1942,7 +1942,7 @@ static void ndisc_warn_deprecated_sysctl(struct ctl_table *ctl, static char warncomm[TASK_COMM_LEN]; static int warned; if (strcmp(warncomm, current->comm) && warned < 5) { - strcpy(warncomm, current->comm); + __get_task_comm(warncomm, TASK_COMM_LEN, current); pr_warn("process `%s' is using deprecated sysctl (%s) net.ipv6.neigh.%s.%s - use net.ipv6.neigh.%s.%s_ms instead\n", warncomm, func, dev_name, ctl->procname, From patchwork Thu Jun 13 02:30:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13695925 Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 81ADE12BF1B; Thu, 13 Jun 2024 02:32:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718245946; cv=none; b=cLeBHX6oYEJCwkm8KrUPIeX+5rgvyOawVg+/t++Jkq+J9dKdoC0T+2iGuKVR2Hw/KMAh3L9gj1gbHkDvphBL6AViV+zEGDbiEaEHTj0R2KDaYp4d6b74rpADWxRYVLlL2Engp5/xuv1l9eki8Y7rFaQGnyQHaAVnQMZW33vQJWo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718245946; c=relaxed/simple; bh=TrtFPtQa1k97XzAC7EiU7HM1PNrmylMnQBNN3TI+v6A=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dgADylTPRDdYfQdybrUMlpojp+fHiBR4xGg50ixYYXMd16ETtBkhkAg+K44CrVfOQtL5RdRTz/3LfoRYRA5D+ri+ZhyiO10+PwzHbSTTRioFo3nJ7Ca1nXZuzwsPUBmScL5vl7UwGytTiCTP69owRyWEIew+SD5CHwe+IcO8Ko8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=KNz+sP19; arc=none smtp.client-ip=209.85.215.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KNz+sP19" Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-6bfd4b88608so384166a12.1; Wed, 12 Jun 2024 19:32:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718245945; x=1718850745; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ByPZqEdQZBBtzQ6Ut5Ro8SR1Vw4SYts83oZvgSR+UR8=; b=KNz+sP19rR/J1QPCoy92dk++7ImDyANr9pg1B+U6haVHH2QtXuC0SR2+ExDKA3SCHu VSj/azb+VYMlOi/bO2c3fqiKJ9XSHUvUYRFsvNjON9CvLk4j7tzcM9kvarV/z4VoakDg yltVoBq2qp4g1VY48Wipz1i8bmJPCSiIo7/F/RB7KVCmDEekDwzkBlmHr0ZzE8JZy683 inFxaX9bqLhXnbVikHlr1E5N53mcgLGolzphRxyo2Nh1VXk8qZ+B7PsVL8OoWyxhI1Au 5uqUy3A+V0pPWAlpu1CYnSLsBERNZPMeXnejh3vIY/SAMZJUa7jjr0jaNiuztKBcagoG 4yNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718245945; x=1718850745; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ByPZqEdQZBBtzQ6Ut5Ro8SR1Vw4SYts83oZvgSR+UR8=; b=PBeD76lnxPiT8QiEGU77oFM+AysSRYVtOOJtVxSxzVU1QbmAMHGWyE1Ayqr9clIESX CZ2bA1jMVcMQDW4oieFVJ6MwbddFtHZuGXXj4ql1Snylb76Nz7QO0yCLW2M+hrxDjmA9 JaCiWtgMCG2PwX8lnm47O92L/1lhNY9EBYQm7vBcDYrraq6Ge0gDgDfLRITOIMQtlelo Nhmfv5ZZ0+guZ3El0iqFsnIe2hpo9kk17kqKm6GHfOih+7ghTwbiFj0e/4ggndFvSjdM WcYzZyjUPQgv0Bx0vAF8viircENBdvn5nNb5z7UQ0vjFwuM7w+loMsJ+gc1eG3bFtIbP Xv0w== X-Forwarded-Encrypted: i=1; AJvYcCXF11T/XjWHaHCZwDrMzO4Z9DFIusqpXbC8T3D0vP5/Y3FzQ9La+1p9Xmzqisxo3bHOUYtRWVur25gwdrXqv2cX2YONxP8+5g/jLnAp3FZwIZ4ci/frp9KzhqxqwAkKiXXeyt/8ZA89p1ep5trhViGK6lkWz9TkKHCXqXLh9DJJ8hsfc6xXjERXqpCTziXHFZjLpjzSSFlByQW7JaCgkA3a4nrcEuyahYM3YbKze4ESe2ENd+tn/hcb9ir2cPuACIMBDn5BgViXb9UMertILWKSeeg38Fj0azexPjd3uNlINDg7qK0St+1cFY1E/uJkGoALp3dOgQ== X-Gm-Message-State: AOJu0YxGGxbzhhxH+g1cXH2+sMi6gri5mWQ/ZACw0/PdlBGl33bzRnxS 8O4TBZEdtAJU7N071vumg5wq0PtUmW5UfT81KfdA+8xDJnUR8Sh2QAgSexPPjAU= X-Google-Smtp-Source: AGHT+IE+I63Tcz7mU3dbe4p80REUOFhGXlU1vxQyrrx4b613sOPf+IM/heFPON96TqCsfc2pgVpt6g== X-Received: by 2002:a05:6a20:840c:b0:1b6:c527:7e42 with SMTP id adf61e73a8af0-1b8a9b1eb09mr4050153637.3.1718245944827; Wed, 12 Jun 2024 19:32:24 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f4d159sm1755695ad.289.2024.06.12.19.32.15 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 19:32:24 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter Subject: [PATCH v2 10/10] drm: Replace strcpy() with __get_task_comm() Date: Thu, 13 Jun 2024 10:30:44 +0800 Message-Id: <20240613023044.45873-11-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240613023044.45873-1-laoar.shao@gmail.com> References: <20240613023044.45873-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To prevent erros from occurring when the src string is longer than the dst string in strcpy(), we should use __get_task_comm() instead. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Cc: Maarten Lankhorst Cc: Maxime Ripard Cc: Thomas Zimmermann Cc: David Airlie Cc: Daniel Vetter --- drivers/gpu/drm/drm_framebuffer.c | 2 +- drivers/gpu/drm/i915/i915_gpu_error.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/drm_framebuffer.c b/drivers/gpu/drm/drm_framebuffer.c index 888aadb6a4ac..25262b07ffaf 100644 --- a/drivers/gpu/drm/drm_framebuffer.c +++ b/drivers/gpu/drm/drm_framebuffer.c @@ -868,7 +868,7 @@ int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb, INIT_LIST_HEAD(&fb->filp_head); fb->funcs = funcs; - strcpy(fb->comm, current->comm); + __get_task_comm(fb->comm, sizeof(fb->comm), current); ret = __drm_mode_object_add(dev, &fb->base, DRM_MODE_OBJECT_FB, false, drm_framebuffer_free); diff --git a/drivers/gpu/drm/i915/i915_gpu_error.c b/drivers/gpu/drm/i915/i915_gpu_error.c index 625b3c024540..b2c16a53bd24 100644 --- a/drivers/gpu/drm/i915/i915_gpu_error.c +++ b/drivers/gpu/drm/i915/i915_gpu_error.c @@ -1411,7 +1411,7 @@ static bool record_context(struct i915_gem_context_coredump *e, rcu_read_lock(); task = pid_task(ctx->pid, PIDTYPE_PID); if (task) { - strcpy(e->comm, task->comm); + __get_task_comm(e->comm, sizeof(e->comm), task); e->pid = task->pid; } rcu_read_unlock();