From patchwork Mon Jul 29 02:37:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13744247 X-Patchwork-Delegate: paul@paul-moore.com Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (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 D58261E49E; Mon, 29 Jul 2024 02:38:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722220719; cv=none; b=eErRh4DQMOtiTdOcHnp4bxXe3Q07u7fCeofbnAt/igXXt9RcBdbK6GZNLWE56vVtlkso5SQwjcxv3Z0fdqwgZtxcSCpXj601V2Z92UYX8PZtT7lM6MKad8iX+o0wvBfiHhVOFTW1buHb60nU87SpRNXJJhB0y3xV1rr8VgveAVM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722220719; c=relaxed/simple; bh=YDO2GJm6IOYrfxyYfmn9qJYNq9J5YDMBpKRsJS71FYw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ubRzkqe5LpRzab1oFQnBYp0fKQEgKoK7lenkvix69k4QBL2bA43GJZAZSO7l69W2C84UliIyF/JhmOon9j384I6WCiO1wFOfQKet8hZVACXKTu5/NA+/kZDNpHRoEncAypJNHFyKff8aFE5qqoOTUMUcPDLpKsaQcF/eYhviWes= 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=WRCflNfk; arc=none smtp.client-ip=209.85.216.51 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="WRCflNfk" Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-2cd5e3c27c5so1623243a91.3; Sun, 28 Jul 2024 19:38:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722220717; x=1722825517; 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=/dYktotwfBu6Roz80A5YCXaIPxsklnKpN0JVw9YLBJ4=; b=WRCflNfkcYNOTj1DA/suc+jZZ7vpMtHf2osVZVs/XOhVbriI+DOWo3c9a4jO+rh9dy SJJWYmCzEP3uZFshum2p5YlQxjXCpWsVf9mmPWbRTW2trymDtz0IqkkiTkVNjeoeYIt4 9bZiWscqrOrwzMHKGe0Cd0WFgy4gy8rGyq7/3GTyCs1VHWwBkAnR581uMwCZwkMFrL80 /ppw42pHvuplPVnpfi4mBaZTp0qUPsXAr/iC9ehLRzHeuaIpJye+gZWR9bsLfjaSK9e4 SR7C5KgYvNG2qAmPFKk3Oz5+IEzmZzZUVPMwGklTlLO4whitJhSdCClQnvPFPlJf2W42 AyKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722220717; x=1722825517; 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=/dYktotwfBu6Roz80A5YCXaIPxsklnKpN0JVw9YLBJ4=; b=ifpUBplsLkTdun5MXQzA4gN7LetPM+5II6NzWGG/C2CFAVVPtB3UPrjHYzWl2SnNdC B/WIIrJUvFnGYCqOjHL0fIWYwTN/6CIGWhzTtlXQ5G95xdq0UvQ8XV40RrYlf0iz8aKE /1SQuLWGetJMkg5lmU4CdBGfvd+N5bcUv9ROpIqy2s2Ril31UZkwxmvna0TQb2YJsu5n VgWlgnM9K6Rj3NUrtDXxEUgtL63LoT2zXrzzmIhEUatjFyaicHbqm/96DtFg+mpMkDth cdZovjiHJIJp1S/6gJpD648zlM83XAx2VRFLmMpCNbSMRAxuw9lan5U3qS+3iu6Rb02s pIXg== X-Forwarded-Encrypted: i=1; AJvYcCX3IV7Yc6A51Vy1VsuK6PawnOx51ePaauJh1kzuQh7yymJqDwTKYu571AAK/wnYwvmE0Oy7K7SDBUfQMXdZFd8IXvbzcrUZ8qJpHCE/013/FJedEej8e1zhmwDxMiUBjjKl1bbbJ0TyLH1WBQ7vMl9/dAK71bkgi5QFtq7+/uVCsYyoUDek9/Rb17yHEYIOfWrFkNaZXwQuCGI9isBvlaVYjePi5QqtiKHFYPgfSP2vuAuS1OC3XsPr32hBT0T0O9dRNjH6BJBEsinJFc4rkDfpZUU2YYYqyqt6CiYFuUXicU1lyPidyV0RxT6n5N7t7vONcD9FPQ== X-Gm-Message-State: AOJu0YzvHvLmQDC+BBfjTpzDa0lrzlK+/rUBZ2qCSQ/IxVhEpMQ6Cmoi N/amvhN9KcHenFps5EcXOUR+emFeXuiEgU0G3osodaxhxNG8Geb0 X-Google-Smtp-Source: AGHT+IEUvSCxvTt2iFtiQZnOEMmTj53JiCANDbCPnBaNHXCCebMev5gTq4Ke5agmpYOP2sLQoQ29tA== X-Received: by 2002:a17:90b:3793:b0:2c9:5c7c:815d with SMTP id 98e67ed59e1d1-2cf7e2275d2mr4251165a91.22.1722220716985; Sun, 28 Jul 2024 19:38:36 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.31]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cf28c55a38sm7332247a91.10.2024.07.28.19.38.23 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Jul 2024 19:38:36 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, 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 v4 01/11] fs/exec: Drop task_lock() inside __get_task_comm() Date: Mon, 29 Jul 2024 10:37:09 +0800 Message-Id: <20240729023719.1933-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240729023719.1933-1-laoar.shao@gmail.com> References: <20240729023719.1933-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-security-module@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 Mon Jul 29 02:37:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13744248 X-Patchwork-Delegate: paul@paul-moore.com Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) (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 A9BDD26AFC; Mon, 29 Jul 2024 02:39:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722220742; cv=none; b=lqW747M5v2SouA7kY44ViLSOFmuv/joubFrnPSOxxXgXgWuey57xU4vKp0vQCnnK7msOrRx7IqfrAWpses1yaugC6e71CTQ+/mtFzExgUnD7i98g+pRw/L1j1T3U+2eje3s7GPB7sfXVP+GtpFWumm16sxwY8gAOUfoQGSpfDlU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722220742; c=relaxed/simple; bh=RhhRTzGzjqJG2IuRKewfcKjpBWjGGob4kNd4UpTBjcc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GO0S5a2/hpuQDMcPYSmmgYeDUUIR7lEiSPh9XLi9SMcBYQbWhKNSWKzkfhF5HoZuzB0VFo98v8eBge5cKFX30S5+ad7VFpJrkuaNLf7ZFh+ikfcgiJlmPAs1yqgRd1OuG0XLzsvZsvIZ7x81BauYrG0BfE7yRhYSVzJqEykHKnw= 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=D+yXPImA; arc=none smtp.client-ip=209.85.216.49 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="D+yXPImA" Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-2cb63ceff6dso1625775a91.1; Sun, 28 Jul 2024 19:39:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722220740; x=1722825540; 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=y+o2QcP3zzShPD5bpWdvqtRKk4SGVf/ncCjWPDHjjaM=; b=D+yXPImAz6FgFP8cyciCw9CmffSqDIq5cEFQ4ss7LnousCvVQ7Nu+5RHuxu++9mjXo lnyERIoHDN9MIYO9irTd/RA0dmIlGKbGKbDVyaOv5X8n9kl6Kb7YIMmu1nXHAn8uqz0c cGUtiZOx9XvsRj/cl9r5O6ECSBZIHQx6z0cyO8qwHggo956pVdkStvomtd+oWcphYarV VSdHRpYZ2ZoBWW9RMSB0O/owVSms8gXrVJBT3QjDLEyTxPBVBloaupW4lGR42Su6ac/n JCEWO5aFA1w+unCidG7qh8ttLMRJFGuXyrSb+DOxEbTZhuUSHlzgmoDFhpuerHu/0MoY N3iA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722220740; x=1722825540; 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=y+o2QcP3zzShPD5bpWdvqtRKk4SGVf/ncCjWPDHjjaM=; b=XHZIe0+kMWrlPgdJ1zuwu4RTrspv2ehAZEockhNmUH0LtRHsT6APJjzYyVItD+6Uas B9bVGpdlt3RYFpZrjju3jw056BadT+IhwzzClkp0A7XAbs+BCkJ17HN3Be1/htMocF7x v8WQyIAtJ/8/+IYcSC7KvKC1rhR4oT317L34YA4nYINWWcPFgicGIN+dlMhxLuRO473a ZrGpxpa8Km6JPfffsyGEEUxgfEGI7IlUAr65WaWS93zj7FgYwVsCxZd2F0hVN3WJO+ja Mi3QtL5jt6viBrkevTf/miu6EPpbxD8vmLEjJjxsf6eYruho3n27jjYlGAXBmgAfEZd4 ldpQ== X-Forwarded-Encrypted: i=1; AJvYcCWnpYIY1Yw0iXfIkkuYcoHDSr+O3b3QpZQN4ukVKKkXKJoQIfzqa7P+S9Kx1xbUiLlMsx9m1K4TJjQkanHI2tdwi/cp09cPpIeSLK+Cah6cyR/qcJu6hbuzDSajW/b1akNUUbUx1alq21T4yYdxGha4baEw0Et4ia59bsDu3FjrhPxLsJsQT8x5yBXJQUQXpocAzQa05e0OwqcYGTQT66gC7aeNyGoRUr3pPWddv5ampdit29Bgp7SLTGGybPafBKAlZxwmLhN77Rj/EVNf9pthhaFfuLMHJpb54ITf7O3oIP2owbHw1mkjjEzvV+5keVb73WK/qA== X-Gm-Message-State: AOJu0Yyp82MpuOIXYMfraIkpJtw7yUXj7CBE0oHgzJHfmrvRbd0JUm2D LyRrANpemKXhyAH6bM2yUvGaCmIWjb+1hpqOa1nEAK3au59hvMxe X-Google-Smtp-Source: AGHT+IGBArXuQ6UfIKXak7ZIiMIQBzEmqSTo4bq3Ao5W++iZVvXLvRGW5ku9X9sKf2s7GMsSLwtHOA== X-Received: by 2002:a17:90b:1649:b0:2c9:6f91:fc43 with SMTP id 98e67ed59e1d1-2cf7e1a3e76mr4224751a91.3.1722220739919; Sun, 28 Jul 2024 19:38:59 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.31]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cf28c55a38sm7332247a91.10.2024.07.28.19.38.37 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Jul 2024 19:38:59 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, 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 v4 02/11] auditsc: Replace memcpy() with __get_task_comm() Date: Mon, 29 Jul 2024 10:37:10 +0800 Message-Id: <20240729023719.1933-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240729023719.1933-1-laoar.shao@gmail.com> References: <20240729023719.1933-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-security-module@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 Mon Jul 29 02:37:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13744249 X-Patchwork-Delegate: paul@paul-moore.com Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) (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 259012B9D2; Mon, 29 Jul 2024 02:39:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722220770; cv=none; b=eurUIL+TPpriNhPR9Q8bOTuElNh+9gIxHZxdY0+dCNYYFNmpdtXUUkte1wb5q6uSOX0NMpwjV2FdeayjTcGuQhy2r5g4Y5uIElcM33bBbmbrn0Yv4evIFB7MQq8X3jG6dUpu0iDQTxkm26aHDsKdas53K+Tj6uZvo116mMQNjHI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722220770; c=relaxed/simple; bh=FkchoV1pKKhU4iF2H0UnfhLXdjU/OqthurkRTQdk9Rc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BdryAjac/PWLdlRJZVcJf+HRpbdp3P7zOJNOTxXJXS0CDojIFC2H087iGl4wrqS1xVrb2mz/oQKGel0Kdg/GfD8D0Tzo1QIAHh88zlPwst0HxTOfMOZEsh+V68qhA7Ovmwuo0VRvILp3AxN5FL02PnQrokFyP4nPQKpnlKvVfkI= 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=Kse7s0Gg; arc=none smtp.client-ip=209.85.215.177 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="Kse7s0Gg" Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-7ab34117cc2so1939264a12.0; Sun, 28 Jul 2024 19:39:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722220768; x=1722825568; 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=J+uQG4RKAtUTljj+c6PuEzvl8DtV0722F0vMFtL07WU=; b=Kse7s0GgEvoQ7ccetpRrag0zVGEBmFBFpU7mxl0kN3ksYuxyVgcdRYJ2sCxxlkBWqc nbHxE1R/J2kKKBaV3/7T09UJ6pQOa1o+uO+DcbteLGMhtTourRrgn5yxvR6ZD0I08e4w AZRO3eSff+kaWROg74gG3VHIgnpK3tG1XPdthT8DuwEVJCJz4DWfotOU6OB15njGC2Pt tS0C5DwPOfcj4S1TUppWC8CeMgY4Qj0I6JuEuabb5PZ7qQX4k2hkYi2eaVL5paVTj7eS sx+LiFq95KDVhg5OZVpYC0nFkN2p0/L9Q7ReCAHasmrADQlJkzT815vkD6c9fomOSbaI Fd7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722220768; x=1722825568; 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=J+uQG4RKAtUTljj+c6PuEzvl8DtV0722F0vMFtL07WU=; b=IUXv2nT9naYweN99MZsQmQ11bwlmQ97xBqccil+15S7fvhfh2j8nf73gNxrRFAo1UA zRYedJIsIceKLnSCAHeOcOD0Sf2/Ltq8e0r1BhBKoGEXDP5Ts6nK59LvVLUO4M4H/HZm eKH5f0ZnCWaq7QrgXNkzM3VuSKMLbyrNoZAaeqCreQdGmqn0MIFxh8IjOV4ma8k+KphH Scx8BWAoYpJovpyw6W9VBo2K9a9/wYP92vSCPjxPsBvv6V4K3oQ77ubRreoZVSk1ZfY9 I2jgNWFqaIe7vv30VemmDfRDQW34dh6lo1ttJ4rXJ42C1TkP5jBENSAtxQ6T3OO3GHLL wURQ== X-Forwarded-Encrypted: i=1; AJvYcCVoe8XVy6XWyeLhnCibo3FX+kr8ZS4/TFK4KXAdqeKjE1vYpwrTFoUt6WY+E7rGY5HhGsfR6WThGimn1nOuL26JaUlmimXHqQgfwyYW6zR8WNJfHefyApu/gifrmEc/c3ITPaq2yh4obzBBJLmHhDadGyQ1N9uEbZGkhkGPGoW1MwbOcFeavpajqXh8OrxL1T2xaoiOqjP3B5brRfLTUex1Qu8141ux61b0JbEMBo+wjkHnfK47Mo50B8bX2tP3P58omtapLhclD8fHqBBQJeiXIQXTupRfinfH1tJgNWhfQoSvlXT2gL+V69bHsfSoKIbCqhnNaA== X-Gm-Message-State: AOJu0Yx7up9qsWSbHVTJXjGL3eG7RlsAokxeCtUouaRVCqyHSGuastIq cGNTIoG8Dth0u9HAKzMzXB0diCI2Dc4JnKkCzOrgm5lL2EPTBqBO X-Google-Smtp-Source: AGHT+IGDDVacAHzuqJf1zY2UQsKDkDe/fVaez1tdPtuFhqBPLEHRkdQpCzionBNRtCrdgKFvX6YkKg== X-Received: by 2002:a05:6a20:d50a:b0:1c4:8bba:76e9 with SMTP id adf61e73a8af0-1c4a153afecmr8452767637.53.1722220768309; Sun, 28 Jul 2024 19:39:28 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.31]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cf28c55a38sm7332247a91.10.2024.07.28.19.39.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Jul 2024 19:39:27 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, 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 v4 03/11] security: Replace memcpy() with __get_task_comm() Date: Mon, 29 Jul 2024 10:37:11 +0800 Message-Id: <20240729023719.1933-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240729023719.1933-1-laoar.shao@gmail.com> References: <20240729023719.1933-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-security-module@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 Mon Jul 29 02:37:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13744250 X-Patchwork-Delegate: paul@paul-moore.com 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 C676726AFC; Mon, 29 Jul 2024 02:39:42 +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=1722220784; cv=none; b=BGSJw09AxW0G5wc24SIwUwVFVleq9jW5Hhs96pXELe1sn9hsrGXm4JPcjQo99PLam0VvKnfpeJ+L6i+RFQYc0bWD/g56813BrQeKl1IBDi6cu/hn8IS9uIiliY8emXdu9aPG8S0q0r3qFO6FiuNu8WqGDK3lpayKOcs2NcT5Azw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722220784; c=relaxed/simple; bh=56ESQtaoBbBqZU/saKgTljkr5U/isylgWUhSQpUUDZM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AJ/OOhn4FMDttt3LTwPEEnApi+r6SobEWjYTPNESFg+VPU+2OgGcbqxP6J6W1c09VDo2nsNfWekZcbUndrVMtIliNkOg3kUF6Sbi9zQJFaeUB9PG71Ucn3tfTG+PATq+nCa5f0aCkPP6J0hDNCfFn6ZW9eN/qmRGcgl+hcpsZDY= 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=EcB2/bmA; 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="EcB2/bmA" Received: by mail-pg1-f171.google.com with SMTP id 41be03b00d2f7-6bce380eb96so1459777a12.0; Sun, 28 Jul 2024 19:39:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722220782; x=1722825582; 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=QQdkC9XISn7BWPlTE5/iE7aUj/L2jeLonKoaBsbNpS0=; b=EcB2/bmAWFYODMlXFBim+/nzcdEPy580EgjIQ+hEc+XuhpauULwUwtbTO2fHYQfh0U wIaMqM4LTH+P06diYzk9sYwUg5yK73OwUzpa4hKjuu0+TqnVgsEII9vq8KN5aBMj33bB 21TIlHS29X773RV9HbDxTpNE+D+2iS3VvZxE2xYCe5VPHJyN+QKQzT30/JWe5jSYU8Qt qGNq7F4ILvHH+G50mst4RLXnhVkb/HSpA3vL4GGxp2h+mOxzXxOTtHdWfaoEBn2VoOFV /vQ5uUyVkwOyBHMqGmCLz8oXoH0KLrkVDjs596CNxYLaz4iGbqmzciFkontY4ims1D/q moqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722220782; x=1722825582; 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=QQdkC9XISn7BWPlTE5/iE7aUj/L2jeLonKoaBsbNpS0=; b=tCuQjrmwUnEApliYBdtz0TVdlalJVGM85TrFyVcZWSWAPiADLBxbZzrgL1IcItj+7A UIPmVFvmj8YskawX+UwKEjZ5c6mb8WO1YX/SFvkCLJTI19mPy4Yu7iQz03uELq6ryBlI jii9G/Ojizk00hpAPw0Y7wZi6s98rAx2qW2NLqgxDoJSweYuOgkY5zLG8uFt8N1/gyZy oSsA83oW/km2TCOZA31x1/X+X8wznNevgJDfWEmXA19tD+mVceASbGkHXNRGnIxVIULK BOrgN87kaqnn7VIOgHUMM4FHKMqRBSTL4j9Z05+wYrItsy8FqxsP7BbPZCES7jvpaHsp w3EQ== X-Forwarded-Encrypted: i=1; AJvYcCXZexdgsx+3NJdSKjIH7zwgubvAElaJOn5Q361Z0xpnUHx5Gz1gIYMvnXUEn8DydPtnYRMd7lL/u0v0MUx/e+0k/s+NisDwsIrnB6d4+Sf8c/bo0W9FGSU0zu48AC0RObt7egNfFH22Mq5ZEkDytt2q5bYbv6t18Ij94AY8C2wdMYeG/bRLfy4+WSnj3wHSmdKYGsiBhKX8pbhST2zqWjxBbrmeslu5IpKOHoyX/+1mI2QY5OA6iEKj4r/vk0dJiS90O9CAo63iFNdYxTI4mXAdeaz9RCFADa29NqPBLp0Ako/jLj4YFTZ8b3PjWwUnNr6r2TCBVg== X-Gm-Message-State: AOJu0Yy4vqdjcpAS90S9dJdRGsL8F0FAVn+3uYxgvtNbv/dTGQA0IKpF Sc3/ickPc0SFfJ0CoUyoar6f4SMncIwVPbS5i1HBojmLDVYnDuWN X-Google-Smtp-Source: AGHT+IEB8l3Gwpw2EoLQWPXs9e5ig3IiPuxqtfGfuMuw62eXKcBpM2d5t6Uy/en6iDAcfcY1gaCpLA== X-Received: by 2002:a17:90a:6581:b0:2ca:8684:401a with SMTP id 98e67ed59e1d1-2cf7e5f27a9mr4176294a91.32.1722220782084; Sun, 28 Jul 2024 19:39:42 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.31]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cf28c55a38sm7332247a91.10.2024.07.28.19.39.29 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Jul 2024 19:39:41 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, 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 v4 04/11] bpftool: Ensure task comm is always NUL-terminated Date: Mon, 29 Jul 2024 10:37:12 +0800 Message-Id: <20240729023719.1933-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240729023719.1933-1-laoar.shao@gmail.com> References: <20240729023719.1933-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-security-module@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 Mon Jul 29 02:37:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13744251 X-Patchwork-Delegate: paul@paul-moore.com Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) (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 E07267C097; Mon, 29 Jul 2024 02:40:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722220803; cv=none; b=kCwjvQtdDZQSDeR/LnR+Yvn9p8nhbestVe/+QOtkkuQwxLR1MEWIyDXod5oS1udgsKknCeI80z5ktXOWVtWwqTKpjKKEM2fYjdLPIBdMasEfkXb7hlhTm6E0xM4tvfCzyFIlot31R8Dp+TVYMNOa0LdD/Zl2MQEPR5TZfs5UTr8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722220803; c=relaxed/simple; bh=LGO/jG7Tv89MIg13raRY5z42pAUoAbxhcnib171V4yA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZpsFKQEpBPLHleg7EgJ/xhqWSYZnCDv/yXHL8IMtMZIdooSCGO/4QMe9WNW3nMdEC5OtLTjnrr6sbKNfgJgmiO+liBD3uvXhwtiwndqLeDr9tBpg+3XBy0N+Y6ki4U9gveCfqlJR7bbnlc3Mtd3pnZSCnQf0u7YivTIFU7J6+WI= 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=I1wDZDN5; arc=none smtp.client-ip=209.85.215.173 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="I1wDZDN5" Received: by mail-pg1-f173.google.com with SMTP id 41be03b00d2f7-76cb5b6b3e4so1535142a12.1; Sun, 28 Jul 2024 19:40:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722220801; x=1722825601; 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=8rSpj4+mjUH2uIm/J6/DitPwV6C8fJ6cAywoYkpJrFY=; b=I1wDZDN5coC8uIxvA2mTRhc2EPYUm9cohl7OVcnBJ4Ln1cCirbEeABMP5NUOLWvU2g 5lfpcVOAvTWWbyFnc1TJTdcobmC/qakPErrhytVzxzXZiK1Ob51vr95xTXJkCxkDNN/J tW9hkp4+DTPU1Kvwe2umx+U5iq8KqE2tqXdUws5OvuBcyJ/eeQfAt00z/qgwNbpTb/WO m95EjlKfknAcp9dVaC/yXflygwOPS/qPAZTc2bM+W7Erh/MPjxPRWthNdnTDib7/v+TT wWk7M9O5+WGpOyzSGYplOHdz0b8qgEuFpoJ7bdgUVfgmCh28gUAoP5mU7DkH03Xrx65i Eozg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722220801; x=1722825601; 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=8rSpj4+mjUH2uIm/J6/DitPwV6C8fJ6cAywoYkpJrFY=; b=F08uL+g+G/9GXkNcJtgPd8q58U8q+ATC4xZ13wS23inMNs0YhlgRssLnmXDk00EvsQ 74nbGJq79hHL/HQ9ID98jI99qsog/b2jtoqYFUOKqZXv/CBEveEKogMMAfqqqJf+Qz4S QStI8oEogKCSk7CmHdFsphrMJgiWCduxAQF88prkOavUEINoAEeS8Svb2SV9vs8Pf0TL +WnxZSIqb0ga/UAvSYyp6VDJmtHHWtDIJKwwyYtjC8/Utdi2Zzsgjena7p5sabQT/1fT qG8UJWneQgGGYxVcqvfAApq04NtlWZv3eUFMt3sWxiJT2P2nNUVwGNs+CNe2gKQnHBN1 eUjg== X-Forwarded-Encrypted: i=1; AJvYcCUR9jMsV0b+PKQehXUeLzz6jwC+jpOZr/oUPzAtDONozazNr2HcQ05inr5h0w0qpQt+ZNrVpFNWFpVqPHmY5RJGfQr3e9K4G+T4hgfevxuLXu+Kxhd53N4Q5h2/duM+6Vc/zsj8SGU2382vO5nkywdnbxjjxIdL3N3PnRhPUrGW2nk1q4ReSn5BZ3LvaIumNE7fMPniAdxSSiSWvQsp1qqRZT6iARzyvIQgz2sJyGugD7oWO2/w6mMAM7dzgluxBOcMdQrBGon/COlH+teUzsQ1LJPd+0R4p1l6pViBn0CJQXKzFjzF/g72Mx/vBdMPMBM0KUgT1w== X-Gm-Message-State: AOJu0YwjE/5QeWA0qxrz05lY2BQVhSxFfB8obUGi/6vAu16MfPrCBMz8 2UFd2AO63kUMrVqoQVxv5uEmsTmpy9iSAyhQk8OAHXsevqXfrEsu X-Google-Smtp-Source: AGHT+IGTYP+13dgbaXUTcOaiyJPHukRZsfD9pPSgLtBCe0th4rvzpLqZDHRAsUFno3V6NU5AaOlIiQ== X-Received: by 2002:a05:6a20:a105:b0:1c0:f2d9:a452 with SMTP id adf61e73a8af0-1c4a12a2e61mr4874235637.13.1722220801130; Sun, 28 Jul 2024 19:40:01 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.31]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cf28c55a38sm7332247a91.10.2024.07.28.19.39.42 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Jul 2024 19:40:00 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, 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 v4 05/11] mm/util: Fix possible race condition in kstrdup() Date: Mon, 29 Jul 2024 10:37:13 +0800 Message-Id: <20240729023719.1933-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240729023719.1933-1-laoar.shao@gmail.com> References: <20240729023719.1933-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-security-module@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 | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/util.c b/mm/util.c index c9e519e6811f..41c7875572ed 100644 --- a/mm/util.c +++ b/mm/util.c @@ -60,8 +60,14 @@ 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); + /* During memcpy(), the string might be updated to a new value, + * which could be longer than the string when strlen() is + * called. Therefore, we need to add a null termimator. + */ + buf[len - 1] = '\0'; + } return buf; } EXPORT_SYMBOL(kstrdup); From patchwork Mon Jul 29 02:37:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13744289 X-Patchwork-Delegate: paul@paul-moore.com Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) (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 063F97D3E4; Mon, 29 Jul 2024 02:44:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722221071; cv=none; b=qYQu0EuSVqsKkMCOL36ascQ1KVqNfimW+XHUHyAjpx+VzeaJlxPo/CgJk9Z7sX3mQuVhUFGx8G/AizKjRmIQFrIkyyuVWoz0qirNd2sjUQ4DgL+WUpUzt4epwzwNEQXFJMluJSdtTWBSoSIiGfBPssubWanozPPSXBZsYKZEERY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722221071; c=relaxed/simple; bh=inPPFHuZm+7fEeywe6ogK4qmICCbgmhVjJQRSqf84EE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=piqLF5HxNhUNxgLSvnoNxpsQNkNhmtZqIc1mly/CfgjOwKZCcvfJISr7xqfzL06JCMrgkEokFB9WYPYTGhLgHaOgJ1x70GKcEFBFVJcxQuxDSrQmAkQBXUOveLGILg0T1KmARnYveQtUg99z6MZPNcwLVRa0EpbNo5fVredT3lI= 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=OsEzvySQ; arc=none smtp.client-ip=209.85.215.175 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="OsEzvySQ" Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-7a18ba4143bso1665714a12.2; Sun, 28 Jul 2024 19:44:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722221069; x=1722825869; 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=hxss6iSMiIilMwCNJtHyPtBSL44EBghXiK4U5v9w5a8=; b=OsEzvySQuN3fS7XRL/Ygzejo/N3gmMq++hmmtpcNMVLxuiPE7Xub8vDlZKy7KUCFv2 ohCXK4DGYtz2SecqCa2sO2uVaI2ZRnXZl3NMXaxTr7c2Vdsj2vpe4UBH7Dt2Zn3rORSS 9cr9PKoWUaVJn0y6wqCkXdmJe2ODZ4ZpUNMFg9Z9tuPqrNFVVSQ6y8YdOWZk3cIWdKJR tK66Mkuv38CGWkeFv6f22B9WxB3GCnGefGD1fy5xoSiIv6iHZ87lxim4ZtSRStnsJXsz RTXm9F/aIiM/yBZHWkCMiW4vuSQ4L/8O20HXFdQ7DrtT6B1Hb+boPfFwJFBTXPYc6eOG IPNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722221069; x=1722825869; 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=hxss6iSMiIilMwCNJtHyPtBSL44EBghXiK4U5v9w5a8=; b=qE+O/B8DIvOY2TyURxTTslsKRT9SceYLbR/ILUfYCfe/BTirP1YnBfhul88tsZjBei c1C4xHWfoIBhvAJY64inr7Ll2fPKMoSd+zxX/FtBJkQoxK25NG8Z/nzdPd+vBJVLVu8g a/rJN2Aze4XDSGvo+pBVGXbPUP0Ch3EyuLfJmU3gnk+8FEqjvQiZDQKRoLuZniiwBhaL WiPhPikJQX3u4AXoAVWuaD36BvYbQVVBPUPc1uXD/8u5HlmIDq2w2mV5z4XqYGiEVdBV Jmc7pYvASi4ZNjzoVVR3BA9hhrM9UNRpx4+RxgaUWh0538VvvcLd2NUHepb9jA6EKLyz 2wLQ== X-Forwarded-Encrypted: i=1; AJvYcCWPQzfg8zN/OGOzz/n+IBdIFaavfOoR+skAzWWLQKLsmcvkgK+qpnpM5RyQHEL5dvEFUt2ZaI1n3xodQZ5Gwyyv5Sg/un49KRm1IGGezTz5SpFItpd0LZ6FZKYfrR7hkPok0m+Q3Sk9BU14giWzZu3GrcAihYy0Bw0fvoJlR57sDqcm/abqPibrQ3I3pWuuGsG6Z1PgJEB/RfF1+Opge2mNSTKfjS2OX7OzqrCgvmvQ5gcvvsuoomlb5znpHfI4l2v44Tlul7qNggpI8tNLW/LxgNzrYK2J/MtupoJi5xk7ZJEi20QlCl6BJipn/Iq5nAQBkKpGcA== X-Gm-Message-State: AOJu0YxZmZiLAT/XeIs0qknPAZn9FpEhQvR2oqSIrrbGXBtTL3Tm4jx1 1Hq18lktu+LvRWJyadPX3mvCkrYol3ICt6R0VHvIHno4L46I45fg X-Google-Smtp-Source: AGHT+IG7/eKYXg7QfYOb1CdYS0BMtkGw6Mkjs56lIRKp/S/546Q1z5Hj0Wkc3TWWQHawrwJ+z6MQVw== X-Received: by 2002:a05:6a20:1582:b0:1c4:c7ae:ecea with SMTP id adf61e73a8af0-1c4c7aeefb4mr1212793637.11.1722221069087; Sun, 28 Jul 2024 19:44:29 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.31]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cf28c55a38sm7332247a91.10.2024.07.28.19.40.01 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Jul 2024 19:44:28 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, 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 , Simon Horman , Matthew Wilcox Subject: [PATCH v4 06/11] mm/util: Deduplicate code in {kstrdup,kstrndup,kmemdup_nul} Date: Mon, 29 Jul 2024 10:37:14 +0800 Message-Id: <20240729023719.1933-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240729023719.1933-1-laoar.shao@gmail.com> References: <20240729023719.1933-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 These three functions follow the same pattern. To deduplicate the code, let's introduce a common helper __kmemdup_nul(). Suggested-by: Andrew Morton Signed-off-by: Yafang Shao Cc: Simon Horman Cc: Matthew Wilcox --- mm/util.c | 67 +++++++++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 41 deletions(-) diff --git a/mm/util.c b/mm/util.c index 41c7875572ed..62a4686352b9 100644 --- a/mm/util.c +++ b/mm/util.c @@ -43,33 +43,40 @@ void kfree_const(const void *x) EXPORT_SYMBOL(kfree_const); /** - * kstrdup - allocate space for and copy an existing string - * @s: the string to duplicate + * __kmemdup_nul - Create a NUL-terminated string from @s, which might be unterminated. + * @s: The data to copy + * @len: The size of the data, including the null terminator * @gfp: the GFP mask used in the kmalloc() call when allocating memory * - * Return: newly allocated copy of @s or %NULL in case of error + * Return: newly allocated copy of @s with NUL-termination or %NULL in + * case of error */ -noinline -char *kstrdup(const char *s, gfp_t gfp) +static __always_inline char *__kmemdup_nul(const char *s, size_t len, gfp_t gfp) { - size_t len; char *buf; - if (!s) + buf = kmalloc_track_caller(len, gfp); + if (!buf) return NULL; - len = strlen(s) + 1; - buf = kmalloc_track_caller(len, gfp); - if (buf) { - memcpy(buf, s, len); - /* During memcpy(), the string might be updated to a new value, - * which could be longer than the string when strlen() is - * called. Therefore, we need to add a null termimator. - */ - buf[len - 1] = '\0'; - } + memcpy(buf, s, len); + /* Ensure the buf is always NUL-terminated, regardless of @s. */ + buf[len - 1] = '\0'; return buf; } + +/** + * kstrdup - allocate space for and copy an existing string + * @s: the string to duplicate + * @gfp: the GFP mask used in the kmalloc() call when allocating memory + * + * Return: newly allocated copy of @s or %NULL in case of error + */ +noinline +char *kstrdup(const char *s, gfp_t gfp) +{ + return s ? __kmemdup_nul(s, strlen(s) + 1, gfp) : NULL; +} EXPORT_SYMBOL(kstrdup); /** @@ -104,19 +111,7 @@ EXPORT_SYMBOL(kstrdup_const); */ char *kstrndup(const char *s, size_t max, gfp_t gfp) { - size_t len; - char *buf; - - if (!s) - return NULL; - - len = strnlen(s, max); - buf = kmalloc_track_caller(len+1, gfp); - if (buf) { - memcpy(buf, s, len); - buf[len] = '\0'; - } - return buf; + return s ? __kmemdup_nul(s, strnlen(s, max) + 1, gfp) : NULL; } EXPORT_SYMBOL(kstrndup); @@ -190,17 +185,7 @@ EXPORT_SYMBOL(kvmemdup); */ char *kmemdup_nul(const char *s, size_t len, gfp_t gfp) { - char *buf; - - if (!s) - return NULL; - - buf = kmalloc_track_caller(len + 1, gfp); - if (buf) { - memcpy(buf, s, len); - buf[len] = '\0'; - } - return buf; + return s ? __kmemdup_nul(s, len + 1, gfp) : NULL; } EXPORT_SYMBOL(kmemdup_nul); From patchwork Mon Jul 29 02:37:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13744290 X-Patchwork-Delegate: paul@paul-moore.com Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (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 9C90A1E49E; Mon, 29 Jul 2024 02:45:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722221149; cv=none; b=VXaWqMZrt4xm/QypMWhEnjfEegclDULEib0iEo/p2rrb4vf0IEWKxfwVYwe3N/ns8pUinjvsVM+tRJ8/pCfZ+55gMty1iCdd5aANOdaiOgrZ811cQnEutIvvbk2/1T/H3SFIruZ7kDKrEso/sxIColKZRi+ZyZop43c6C4heDd8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722221149; c=relaxed/simple; bh=bu1CYcoPH46NrrB7Vm16hCPiv2fWyMUG+bX0JU8hjFk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gV9c63Sx339scu1zVYwIgsIqNIYl4XBD7QRpKkP2jRTgqrrJeHoKxcrhgC+cbt0YOkmrx7YhNDiJOcVUiQZ+pmibuxzhmtMbGdQzVVlwmQVhAHwney08a47YtbXZB2HzFS+JQPjo1goRhVMYDMiUN2vNZKuV/Fm6MuVDpkAFTaw= 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=l1LU+YIr; arc=none smtp.client-ip=209.85.216.42 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="l1LU+YIr" Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2cb5b783c15so2068737a91.2; Sun, 28 Jul 2024 19:45:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722221146; x=1722825946; 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=K/nz1JHR4z8p+ajw1D912vGA5V6XqUcGmzMFKN4JA2M=; b=l1LU+YIrgxQ/w23/lHuZMEnVIIgeqXniqWP1GrVE6Wsu5uaYzKIKP3hZ96c647zyeh uMToPH/81S6ya2N/GfZttnPGQt/beMMMARjLY5j26Ic7tn5ayp56/vLneY71YKaj6ZqN 4hP/th9enb26x7VuTjyJ/tYaAC1U6Ry+wUgUF6mnXY125i7ppJn53hDhL0PDZQmxvAVw WppokQIYgiPnWA+yThUwcsFtprqr3OPZbnMWuPnEoM8V76v0RbISYMv6Ams+Q81YiFYl UTyCGJ/dmIUpoAUU+CwWdubkTUc3BwjPeT4uo45U45BXLbplPSbFoLJanWHy+M5bEqzc NmRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722221146; x=1722825946; 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=K/nz1JHR4z8p+ajw1D912vGA5V6XqUcGmzMFKN4JA2M=; b=oqZAW+BhCzJInWE9akTHzGzMoo8zfpPkIeEAKtN1i1MgYqbNmIphvB4We6p4KlBv4r Be9giyEWuHzPDLV/q1ag57C0m7AgdXYUwPU8eW8/rLNlQZ5SfnxRpi+FqGyU/CCNI+tD p4mfuujA6122t5of31l1LHqaZCJZ4e9B7HcAv5mSCgdfFtunH5DmR4kBRTBqMqwwljPi o6aq4G6XITNkQV9OuHaO9f4T1BS/gVEp4qoPb0TGXkchh8fK07ipA/hAndVgEBzGUFiZ aJ5mRzjSwQZUjzO4XORFo47MdCd4lT+GRDbghKzXPWEHjTYTJbFlQut7pIktLi3SOotL TNgA== X-Forwarded-Encrypted: i=1; AJvYcCXPjXRdXAXoIHOyl9LNhWCtKgjUkbL+aTCX5VtT8oKfr37yTssgMimu5FL+O5ktBVuNC/Psh3UlTkzy2lXF/F0ME9N9FvHAluzUYx7i0FKFVe3/DAokHeSYFAHVy8Gi6vbKWbLaOerp9TC4FAmmoWe7O16b6zZODoASOr9tEeiXXwCPgk977dqwe/QRQ2ZRblXhQYMUK5aBKBb5i+xRkyADTNZLrAn1wphOz/hzfVMwUazV1vOO+AXkJiF7WWYhE7XTsRRch4EHon8nCOZZyYaeNnDC3DmBljcPUjvTgGxAozJu/YtSyoUNfisx44xBXN8IoFMjnw== X-Gm-Message-State: AOJu0YxhsMT5jxTTpqiuSzq4lrTlU15AmvI/6xA1i4JqYRvS9wa4Cmmr s8s32CZU44nlDE9ArIzNf70voph6fTf2gRHet2Cjjz/mCCW6yW4s X-Google-Smtp-Source: AGHT+IENkAKdnpSBtIYs5HSWqyoiT1QcCfPMYBJH+JoZ3TUHyUdae/0TZfvoMrDMQbplggd9XcCiAw== X-Received: by 2002:a17:90b:4a03:b0:2ca:4fca:2892 with SMTP id 98e67ed59e1d1-2cf7e095c49mr6901128a91.7.1722221145808; Sun, 28 Jul 2024 19:45:45 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.31]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cf28c55a38sm7332247a91.10.2024.07.28.19.44.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Jul 2024 19:45:45 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, 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 v4 07/11] mm/kmemleak: Replace strncpy() with __get_task_comm() Date: Mon, 29 Jul 2024 10:37:15 +0800 Message-Id: <20240729023719.1933-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240729023719.1933-1-laoar.shao@gmail.com> References: <20240729023719.1933-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Since task lock was dropped from __get_task_comm(), it's safe to call it from kmemleak. 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: Catalin Marinas Cc: Andrew Morton --- 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 Mon Jul 29 02:37:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13744291 X-Patchwork-Delegate: paul@paul-moore.com Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (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 966321E49E; Mon, 29 Jul 2024 02:46:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722221189; cv=none; b=cD6vGuQj2oXBET8dSJIAIZVZr+A7YprWCS8GFUtuMsCxNI6zdUBRbWXLhaIfFdbMfdCoyzTNXf3I7HPknXwFOEWv4LNdYq2YeF9QpyOkUuSaR1n5VKlFrCenEYkvD9PmnLbVKbfXjwi3OELfTHzvHc+unzAx1bpuiwkCJeHSt2U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722221189; c=relaxed/simple; bh=TGYLyUCIjyALT4hs8afmO6j0YAO8SFsfcw/1VlLlPhw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=L6aiZsrOvjlyu9tctf34dePhQMi+ueLEyiqQVjd4U12Yb6i8hBCRMhxcG21TIIyOMzX+eIL5dpZFoHGjdLmCnf8dQM246k8423lz30/hq0J5D9KPs7AnsNfi326C3t/XSR1rf6DSkAvAgIPkcqVkOMM2kGmlsiPwAfstiH9yBDM= 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=fHNCX+FG; arc=none smtp.client-ip=209.85.210.178 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="fHNCX+FG" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-70eb73a9f14so2114994b3a.2; Sun, 28 Jul 2024 19:46:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722221187; x=1722825987; 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=aN2V+duoRLP5mjQclolxXRJl1PN7YZIwA2PLtFFVKQo=; b=fHNCX+FGUEkjI9tRR5OAmg2dJO/pc6ceuusNHSMVMt2bS3XFMclJy6HaM7v20A2MdP whp7LmQbmkrsyndlj+b34vW1UB0Pv26072ID1P7hP/7RTlTiADZvL1O4U12/uHe7GVi/ L/oTjU7q9pM7JLFq+LQlJHjLCl8jA5nnlMMa5kV6SPcCqEtpxHXu1Z69ktDE/J/Oem0W 2ybTtVvs3Hj0z8emxUhRs0USy8D6vbLq1BFv9jjTydEM2VrsM/TvtGACZYX/b3IgLDSN qa+nTr5AmtszMVNLgYT8TiHMU1/YSAYR/8i4MBN/w1rzKyEl9uf3xRDIWc5cwVR3hX5l 5C1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722221187; x=1722825987; 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=aN2V+duoRLP5mjQclolxXRJl1PN7YZIwA2PLtFFVKQo=; b=pjeXMwqUJnB5M8YzW/ksBd0WO3nuisazWBjLCKJahNMRWg2wfNHu2aXKtSmSA3L5JA +WcPFeSILBVbft9szh53iL1Mulal9XMZd/XIW+k1A+B9K+bcxutdp7NtRpslXR4JQbVS 6pFJWvRbKM65nwayYtsM1dI6d9XaSJZtdvkDkhoOoT0rBBLdp9hsRSB4IlF0hD4MeK43 KdBFdeq7PBWbr8hjfbGcEjRfX9FA4g23XE1XWAcQA9pvQTMUnN2SFWquJoYev7QUJCq2 sWiXDKgrH2k7CTaGh/+zF0hk2Tq2/CV7WZ2yyNGzF6bl1DBZEE1xEyaoC3XrFG65djMs pBbw== X-Forwarded-Encrypted: i=1; AJvYcCX+aKkxoKbJjxTAYTZTHvGqXiKcx153VHflK4QrXlXNzKQ7kGHXMnTZBU++MmJtZB/+t1ZHrEWdexQdLew4YZeBsC+M3esFu+xBkojP4mnmCLadzDhHzKzn9hAwIWGaIJsulJ3/tBObGl+fP99YJV5Qn4aHJrSuc9U+yavn9SsImvv3H9ZgyqyC1EWa3OCjtY4EGTOMwtC9GTZv0KGhK+zs5fENBH2sGWL0+v9YQCklJcKRYpbYXiQmxrZta9TO7nsVBxYVBZpGJVRxhqrs2/el/1E5XvRsjl2XHrH66BVX1fgEyzQPKaFBGCNh3k0ZP0Om02vzIA== X-Gm-Message-State: AOJu0YwfyYs0cIzRhiIzHbxSbD3zCK/K/HZW7HaED85vlNFK2g51sQv4 6MYCEGzBBXqiG+gUtfuIEApmjWkJIulF/T6oi9yZjI7blQCa0R7Y X-Google-Smtp-Source: AGHT+IFg6cYKlkoyAPcpyLktfLarITyb9Rzi/fq7hOtiqCqd30rMJ+pCgZRrHbxyCAgQM++WsL2P3A== X-Received: by 2002:a05:6a20:7fa6:b0:1c3:a55e:6199 with SMTP id adf61e73a8af0-1c4a14ded25mr9866605637.44.1722221186954; Sun, 28 Jul 2024 19:46:26 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.31]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cf28c55a38sm7332247a91.10.2024.07.28.19.45.48 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Jul 2024 19:46:26 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, 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 v4 08/11] tsacct: Replace strncpy() with __get_task_comm() Date: Mon, 29 Jul 2024 10:37:16 +0800 Message-Id: <20240729023719.1933-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240729023719.1933-1-laoar.shao@gmail.com> References: <20240729023719.1933-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-security-module@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 Mon Jul 29 02:37:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13744292 X-Patchwork-Delegate: paul@paul-moore.com Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.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 8E3711E49E; Mon, 29 Jul 2024 02:47:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722221241; cv=none; b=VICpSyrRzu/Av8tOZ7eKrHYMueEHY6N+3Qp5epw620yNn3pzLuMZ2AziEYownagodmVgm8bVvOAt3ncMS6RPUfcZuuht/49u7XOUVzoP1xm0Y9TJD/OSsq1dxzQ6amNaINFNMP+vzcSvkG3FTTSB2HwyR+TGZL6TtOHTFffWsdY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722221241; c=relaxed/simple; bh=MRhgjtwGX2vJNzrN2ayxMYZ3geY9fsSR5HOs6exTklc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mIEQdWSBqs1T8qSQjQzbFf6UZ49Z+VI3hayGh/+ZSe7aclPpVqY75ntLQ/U45YRrhxZ2OPCVZk+oC8qvij6rBZqAzDBQqwjPp+L/HqdMsP1/CaZ5d9s6ofLXN7zwHwkVyUPfQB560RG3gHoAjE+oUNYD/rO22wIWGGAMT8KeW5k= 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=cT2AE896; arc=none smtp.client-ip=209.85.215.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="cT2AE896" Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-7ae3d7222d4so622170a12.3; Sun, 28 Jul 2024 19:47:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722221239; x=1722826039; 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=/Cv3tjdGG97YCwgOlXgEBsrvlHrrgiTFYRDjMKYVYxc=; b=cT2AE896we4YA/QNY7C0+dsWjhrnCxZ0lczl56mHF/hSyUboedoehIKwhCjJW4S3jp zAHUVDhixRXgDiDCdQLK4yTg80DvN91YKu8bJvRWPR5WxvEUenUbDOdOCK880Cuo3q0P EJt0z3oSyxDIcMe4PnyAm+Rn+2QrgWGsJokH+P9PEC/CzTjN8mpoKoTgYPPfVizU6ecK csYDpHgKD1sYBu0VofenBHKHSdMwQLktwS+uvF1kzu2XKd9manv4Mb9wVJLU0Cpyfl6c g3yGv6uI7hSFefn/dh8mRMyvUBl6NLSeZlCAWWIcKkAOGALiveFaHJCpCoIyEEwBmVT+ EaZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722221239; x=1722826039; 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=/Cv3tjdGG97YCwgOlXgEBsrvlHrrgiTFYRDjMKYVYxc=; b=QUSNC94qpqzPbSQEccrXbc+jZtF+HQMCsTzenLMvqqCPeFH2N7FZRmEduP5ch478xA RDeu+DDvK5Vc0DNPOCDNpX0QW0WOQP27JGGZnPQiJwrwNUOMXoMfHZHrzwUg8Bq8PSc3 7vjOkr7iXtQcocAZg3b6qU0HqBWtFTW6hg2uvAxBC945G/jQBjqSSaqWibu7MPgg3E/j ILkdRThK0tOc1pPEt09VFLs7l2BfINBAdrfe2cGZlISio2yVxjVkOOesvdm2jKWBAGeJ dch9/sQEHduDRQHeaeq9sC+GkrSN283geGVU2BdkwMVChPKzDgYQLhpFtnymNK8PAvw1 oCzg== X-Forwarded-Encrypted: i=1; AJvYcCUqoLV6/aRteouGOnIYP90qFDJeWrDTEsiegH2NDXmVoF8ms2pfkAzRHOWWJafLzVrHLFDrZ8Lrgc5u38KHBLKmLim3+PjeYSb/W3vCdWs2GUuasfQz8mgfWeQhqkPFXJnEFa8YQTIhfAG2cToOPP7JbxomgU/nc0NbsQedf2CzrpyZM8F5QuL6qUkdYyzQy8lMDvhVk3nr/HIdl/2AA/6EVBKWaeQOfKmB5uptaJnnu1Lobj8VDqHqKtyYDydWcFUFNRdqhiTPZvVs+Zk/fjRJnZbtRwNKvDvfGqNN0wAkyehPVO+ezXZhIODI0cC1qZDmZR4BnQ== X-Gm-Message-State: AOJu0YzTby9Zy+bKvJQp+qgC5eMHI10vNzdZf0msECj4nG15ieIAFf/F JBJUPpzAKU3RxmYBg+kM1B2iAszxEfBK2HqxV3KMCTBjwjmflwxV X-Google-Smtp-Source: AGHT+IFj28K/vh2Wo2JpPTzYf/GZmJskWb9fJ0b0sRrDd73+gDsWQ9AUVDG1zRV3AS7zIGO4lNszYQ== X-Received: by 2002:a17:90b:4ecc:b0:2c9:6d07:18f4 with SMTP id 98e67ed59e1d1-2cf7e71b7b6mr7068182a91.35.1722221238743; Sun, 28 Jul 2024 19:47:18 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.31]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cf28c55a38sm7332247a91.10.2024.07.28.19.46.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Jul 2024 19:47:18 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, 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 v4 09/11] tracing: Replace strncpy() with __get_task_comm() Date: Mon, 29 Jul 2024 10:37:17 +0800 Message-Id: <20240729023719.1933-10-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240729023719.1933-1-laoar.shao@gmail.com> References: <20240729023719.1933-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-security-module@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: Masami Hiramatsu (Google) Cc: Steven Rostedt 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 Mon Jul 29 02:37:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13744293 X-Patchwork-Delegate: paul@paul-moore.com Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) (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 6C19C78C83; Mon, 29 Jul 2024 02:47:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722221276; cv=none; b=H9B+TFmq0mGl6L0DEEnLar69qh3bKgCqiNd4FaZIyZXwDpgdgxk1FBKvGP2dKVLMuPaG5TIgWMM+q9mLOXVH37kzitPHRzrWznBMXxlzcOE4Sq1zfuiASexEB5RUTiHRE2HR3c3zhz+hhQoyAMVdr+FPrlxGDVvNTGKUQPQRFCY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722221276; c=relaxed/simple; bh=wcSkD7vIjfQlJo/ird14KPUKFpdk4Q6HJbv4EbwFBc8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EbNtBNOdhWOl4QgWFo7UiHiIKrA364Wz91abg1+LdZbFanGVzas7D1LGD42LHYnKNTuCgBGOYkZJ5E4A/RNOcTH257zjVTb8uqmAja2/zm/vOhVnk3KpwBUY70FFXOLDybaZxsCSgo0e+0d+3xRFQb2kBH+MeSDvjYKBirwxoJI= 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=IJ7YE85K; arc=none smtp.client-ip=209.85.215.177 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="IJ7YE85K" Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-7aa7703cf08so2048307a12.2; Sun, 28 Jul 2024 19:47:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722221275; x=1722826075; 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=YS5JkKLpH7rBBlezu3RsuBopuhXkHEJwrCmaySM0BwQ=; b=IJ7YE85K7ldec4vIjDqt67I2IJry7BrRpdi3g7OB4CTkq77VxGZJzl73+ma1D75XoC rwU4d1f6apJbFzc1CwWqHtkOOm3TNB0JYKY59P6KFwsrhO9ZBORRYmdmWaplfm4YlFEG TVH6+gosJIvhEI1PMMhwjDBHsi5m7bL1sInVzjAM05Aw3HqcO2pYmm8OtmOkctCmKDo7 vUvjpgVl7vMDQsqiM7PMb7RQz8BDk4Y7oemlF8Xs0XPZzyyOXfXvvxNU7i2G21hhqeSB druu0hR4OSmwZ4+eYLxTPCoV1yrxPJXoYZN9U1I+s8A1QNrlJFgOB0+bn4gsPjT6QUFE TSHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722221275; x=1722826075; 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=YS5JkKLpH7rBBlezu3RsuBopuhXkHEJwrCmaySM0BwQ=; b=jrjRGdWraAyNLpIRHM+KRWJDt0eoxoq2mmq0j07ozzGUd+s6kH1IxS/UhJ6j1LOLlI D1SgLXCo+9Em2omrj8iXw8+iNYozrCA7ZPbYlFSXG0FB8nv5JTLvZ3PEZzGpY/XLpLEm ASDxUUBunUuxn2lZQueNYCNtYt3Jax3GbjVJ4kSmA15e5NngMs8NSPGjF5c+QXGA7Iuv 5BLnV2ZxNlVOgOIHyYLvk16vIiFT7NLjC+hGQvm+ALNjhysFCZcQvKnT/0kisLukG3x8 GbNVvtHO7fBqHCsLMna2aDeeWgotqdzE92EPv40X7n35LrOZcZLucnbQz//yv1BoIsid qD3A== X-Forwarded-Encrypted: i=1; AJvYcCVvsO17+tK6yBr3S7tkcpMFvTaOQB4dK8oq8h8cyKctgzB5PJKEQi/wQOXX7TbMoRCQhMNSfjDhBnotFL2/KkbKDtRBDxoT/xXrzQvpC0EiNVvNdvawJO83vKJCv0wL6dz5GImy1piGPrg3T9WUl1jgKHOIdh0IznmM9tJd5d+Rq2kLhakGCFApo9DBxn4RWSeBbnbdhbZgEU2ePl6tBLuh720YVzl10c2vXC0fBKY/53nDr07F4REcs2H2xuT0TSH0mtvFWNTa2G//B+pBCreEE54RbkldjNDQXjuaTvIb1XniyTzoLwbsLigXN9GSaY3A7TTpAQ== X-Gm-Message-State: AOJu0YwdwES9oZ+NyQifquIOjpCFuNfCsrkPuzUBXzJNuCht+shnq6I+ 08k5JLNfS3KwynpVbZ0afeaATl4h/2K9d/F70mnOapNf9SPhAULF X-Google-Smtp-Source: AGHT+IGXxWNnJLIJ5uPecbp6KsT2OOK8/ZuVg1FUEXn71WfglTXG9UPP1KLmHGuFVJ6pD1f6J8wciA== X-Received: by 2002:a17:90a:e00f:b0:2c9:7aa6:e15d with SMTP id 98e67ed59e1d1-2cf7e1fac4fmr7206984a91.20.1722221274655; Sun, 28 Jul 2024 19:47:54 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.31]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cf28c55a38sm7332247a91.10.2024.07.28.19.47.20 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Jul 2024 19:47:54 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, 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 v4 10/11] net: Replace strcpy() with __get_task_comm() Date: Mon, 29 Jul 2024 10:37:18 +0800 Message-Id: <20240729023719.1933-11-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240729023719.1933-1-laoar.shao@gmail.com> References: <20240729023719.1933-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-security-module@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 254b192c5705..10d8e8c6ca02 100644 --- a/net/ipv6/ndisc.c +++ b/net/ipv6/ndisc.c @@ -1942,7 +1942,7 @@ static void ndisc_warn_deprecated_sysctl(const 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 Mon Jul 29 02:37:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13744294 X-Patchwork-Delegate: paul@paul-moore.com Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) (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 56B4480633; Mon, 29 Jul 2024 02:48:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722221295; cv=none; b=hh5ICYjqkfv7BXPzQYDkhRPRgM/oFAnR1sEp2b7LNtOGEdODYnOISnRrwEdzQlAFc2MqhCEKfSp8/0HZka/u2uZBFz2EEnvZhzCZbd3G463ArnZQ4szv5asunCZvqpXkn5rf1aRhzGBZa6/w+v+eAdJDatHhBW2oMqfgF7C9ybI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722221295; c=relaxed/simple; bh=KFR6O5apdaE005aia/eSIy/aDnoPWMXgYn13tfLkB8g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NidKtHo6gsMpXLzeBZjBLcPU5m9ujV5N5PHO6oKqkPhB6Fo+Xwz2UQd97avGmCAP2faOOEcmMqK6SB3x1fPPChVZmGBjn+Pdn3cjKULJlpyEMeOcnd3PA4LU+KB4hBC0RzQGhYXjWiI9enRVRFpfiq6+wLSSq98tSVI3V6e1WNc= 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=B9lsDSIt; arc=none smtp.client-ip=209.85.216.45 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="B9lsDSIt" Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-2cb4b7fef4aso2146126a91.0; Sun, 28 Jul 2024 19:48:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722221294; x=1722826094; 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=uws88M38Us6mZwthahlWUK7fM6+CMa7f0lHdNpc5/ec=; b=B9lsDSIt3kPWhwb1rmbg7ZOkxPXGtOIGQSSGs1KNQlCHQPIjBdi9qxOxi533gdKPeU oK3p0XEHR7r8JJNx/mFPvMH26c1HX7LuRq6ZiaW54ONHbf0J54TK+4rKbzf227xsjWOi o/DlEOR1hyKBO+B94cXPKCzmnMreOBr6gJ+cQI6MRHwBcB1YLak4Vq7IhXOaVZOteFiP svMS2geFmg4wJcjlZ/QNGeIu9rARXfrYqjjJHink3MMzqOnjRJfJLt9mlzJKxvfVO/hP FlS6Uzxx/8zGdhMQIq+nRdPOoxUVZvfnlt1mwolgn0QI2kgChxML6ka9c/eQqI3wfG8Z 1DWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722221294; x=1722826094; 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=uws88M38Us6mZwthahlWUK7fM6+CMa7f0lHdNpc5/ec=; b=ZwbChy4sV//p3zAHNDAeL98jBTsRi3YHY4It4NjpEUZBKPsqhHrDH/++Sd2A9c7mOJ dPk8/0RysG6za2ylXLoySMYLUJA8kUp0r0ysEzd+Xlv/jDv1SODzRWgDmKR5Srwr+Q1E hSW7poC3ZIQjXlpq/X+kdjbuO0JMHiq1dOCf/SVJnxjm/yVAkPwqfM8MtSEUMD8uksvZ Vx0w9le+4GSLcxMxtMJUqeUMiE966WSFzD4IEBy8iVGpBJR0YGFHWMbWazssGgA4JJmp BXRG9d+p7tvIjiM05ChH46gIh8/FGW7/bix96n6iziChUqZhyLy5GLPJ6+giXPu4Vp/J jk4A== X-Forwarded-Encrypted: i=1; AJvYcCWY/BzFN4gMaxXa+/EjxIxb1y0aMk76mDb3BIwu7p/95UTWqCyi/PjiYzzhdjju7suoubMHdidvO3CczTPw7/8V/dmLblrQBx+ZtbuihRg1uH7/GB4ZrdgIhMUHstqUSl2J5ycqENJVaznk03+xmr+MEtb4uK5Bmc6xMK3OFFfWf7nxnZ9KVYZ5GpGcaOZdxhCjo9qpYCbufOXeEtTPcz6HaAsIkdKQcGJFJOLV2nxfcL4u90KTNhUgNQOJdCnHiPpAXzmxWaR1L6oiYuvV0tB0IuEcFB588epAsOD1lgikAkYPh46YemJMA6Lpp5yk+VUGi27mug== X-Gm-Message-State: AOJu0YzWEwkOBho51D5p5Ce0hWustFYoKMZhzv3ftu6SlarGmBJu/ths 6kvTbUYXz/2RvWAtQrTi2jq3qGr+xy4EMVr5ie473OYZizsH7BI5 X-Google-Smtp-Source: AGHT+IGofcCeds74VK5yzpBY91R2MnaRyI1ieEFwQ1nBe/Bs8+j0QLJHRaMLwHU9stQw4nuIemwGyw== X-Received: by 2002:a17:90a:888:b0:2cb:55f9:b7c5 with SMTP id 98e67ed59e1d1-2cf7e1df02dmr7584677a91.12.1722221293603; Sun, 28 Jul 2024 19:48:13 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.31]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cf28c55a38sm7332247a91.10.2024.07.28.19.47.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Jul 2024 19:48:13 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, 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 , Daniel Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie Subject: [PATCH v4 11/11] drm: Replace strcpy() with __get_task_comm() Date: Mon, 29 Jul 2024 10:37:19 +0800 Message-Id: <20240729023719.1933-12-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240729023719.1933-1-laoar.shao@gmail.com> References: <20240729023719.1933-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-security-module@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 Acked-by: Daniel Vetter Cc: Maarten Lankhorst Cc: Maxime Ripard Cc: Thomas Zimmermann Cc: David Airlie --- 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();