From patchwork Fri Jun 21 02:29:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13706726 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.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 8507684A5E; Fri, 21 Jun 2024 02:30:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937048; cv=none; b=kMaxsJsW9UPIdOal7sgKcguNs8ssEpZbQIwj7kPIK6OoMS8345rTvOUifaynpujUX52zXZbMnIA+cBZTnGCZG9sh6i+R95ReWSAzcslX2mdZOzajVTibWFYwSQf9WzZEyL8lPA69jNYeeJbejKH/KVxxic2kMGBRKKldZuwuDHU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937048; c=relaxed/simple; bh=LX61AqcJNexywhWKUem5c5kFXTCQtXdiKlDqbpWt6oE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=B78wbKl7AsbbbbIAM+9w86+zMqN8jOs/Tds87OpBFip3uiCWB5bZaDB0/lsfyaRZZUDRrpI6EtfY/sxfTLGQglrKGp0AF1eLFfvov/fa2aJv0CUeLrwrwF6+1E02+cGkd8zFceQ1eeXud8K7Km/A7c85zKazHDuypzzUT/uhxUs= 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=OgPhc6IH; arc=none smtp.client-ip=209.85.210.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="OgPhc6IH" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-70436048c25so1557849b3a.0; Thu, 20 Jun 2024 19:30:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718937046; x=1719541846; 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=OgPhc6IHdpjn/0c2Upp3ejw9WrJdsJdolcOgS/13YQSr0+pb/eGm7bmlMtqkZS0P9J Fiy3oiq/7dCqNroVcEFM1xrl14IDKMoCnJU+Lhupyr3GCRMDqiSkIj/gTlpz1ZTh8mSs hceAjNxykx25jYH8VRxAuQUhKQy005XClqxkGN1z2jut1pZh1pD1Ri0MclhLhlXV8Zi6 PbFvqXNMTfmhRQxp5Cni1oLc+/B3rIrA+jF0EkmkLQPCUO2f8CqlEUrZVVTbkUvUMllf kwzDrE37vwBLb692YCuqxurBjUZBFq87xINqufC+JTsGQQ1XmjATe9n/bFRewHDgk20S CREg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718937046; x=1719541846; 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=IUeGfmhzzhJ6omrIclY8Vqi1QpL2eGjWMPobmNb0BqFcQev/mrYfSiWQXvgt8BWtnq 8uFwffrReaZdbKudTcB8q90bsOW+B6Pkj9PfinPJ8DPIW7DxKHmeLV8YDqSrLNsnlT51 NwzlwC09k4PbwXj6DCGMu6K4o6t+9jRGoIPVewPvSjl+vsHV5xADK8l/fTlAzXMauN9C j/Lgxh4hIWFu5SB1hymHwRyL9+TLngu9CuTWNsm7rqaEFPdq4Ofp0hdRtHplJgTfwTF1 zmlpi8CJGSC59FwLzgO7iUoJyCpGxi/05WuGjvZ73EjFrVz6j1Abykl5oHujR6BxKMcT lEFQ== X-Forwarded-Encrypted: i=1; AJvYcCWcXnt5sgbCcowgje+LZf8JiiKE2PPMLU9aTdT9jploeHBJzhZQFw1pt2hq89X1IwhTOWDVuAISvJLAnsJDG41sXBRd3QGc3Xr5suTcH1DoB3x3KP+2nibMdHm0U0Y9NrKYa1O70TDxcXxNy1zm9DaElpPCOAbq7/x0zta9S60yZrIXxbQ6+/4xLEAXiSOWAs4NqHhVaNAoI6sU7keAf3fD4i3WYnzmvCwz9gQew1viC/oH8EpPp5IiCgdAkT/TT+b6TWuJ53fsNacevhvHHbYVtOH0iSZ1qmt/HrKW5iN1Tn0Wjl+6mevFr85PnypHgamrCgwmPg== X-Gm-Message-State: AOJu0YxxWp21w64ZrAlK53LkTslSs1U5NYYzYoavgIvDjv9nRuBV0yC/ 5vwrzNevzRcN4+Y5k6iy974dInvT/HvdoI2A2DLxN9NvrwEyxMVw X-Google-Smtp-Source: AGHT+IGtfjPn4RgUkASb+Xbc5n3FAl++/e74fQb3jmqCcMo9NRTzTfAqzNgZkCzFDeDT/EYbemTCfA== X-Received: by 2002:a05:6a00:22c5:b0:705:ddbf:5c05 with SMTP id d2e1a72fcca58-70629c4204cmr9525016b3a.11.1718937045659; Thu, 20 Jun 2024 19:30:45 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.24]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706511944d2sm332488b3a.70.2024.06.20.19.30.34 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Jun 2024 19:30:45 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, akpm@linux-foundation.org, 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 v3 01/11] fs/exec: Drop task_lock() inside __get_task_comm() Date: Fri, 21 Jun 2024 10:29:49 +0800 Message-Id: <20240621022959.9124-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240621022959.9124-1-laoar.shao@gmail.com> References: <20240621022959.9124-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 Fri Jun 21 02:29:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13706727 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.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 CA92753363; Fri, 21 Jun 2024 02:30:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937059; cv=none; b=kas8fcv/x/557g/0eZwG6hEIEnMEC+rysFc0lGlNwBM0z2l4ATWanDGCcIqppEU6igl8IFblntl5QcE2GGJATbFoXqePPOxyqj1KqfaXDX2pcFoT7hThKJpi97l+dyWN7SehE73haQ+njS5uc7dn3y5QPof5SgPeGXJcpjbwkv0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937059; c=relaxed/simple; bh=Cmml+druTyvH17AsxZ9+qWd188UAOiUJPJyv4QJyitc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ir7oh+4FrhqZa65nK/ULoWYX38++r/1sIsL4vnepeOswzWAy3BKJVaplCIEThs1AhsK3jn9J8uaSkr8kBEj+RGOMD+UEyrX2pGoN/MDUChfelsgnpaH+VI3cc2yjfRZvjhhxafL1KRStWZ8yBwyUaqgdakT6jg20jacTcTRxVxw= 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=KpCRnQ1U; arc=none smtp.client-ip=209.85.210.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="KpCRnQ1U" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-70627716174so1372419b3a.3; Thu, 20 Jun 2024 19:30:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718937057; x=1719541857; 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=KpCRnQ1UfSxoG633J8DR9JmR1MeZv8I08Kj97vI5YjNd1PiEYr4nxCt3//uqvZ8gkX vu1Uyt8xr9rQaAzrZnyaWxk1N2MrCrHamNHFNCXR8BjxGvPI3gDpOflQjGltnJavqYaq AVFZrq5m6yORCGHWG/iPs1M5L6XthMyWwwP6Tp4RBhSjbi1V7FnCPxGm4yXKf32t66oa 9Fv31mCy3nZpqKMqzGqi+IoVH3klhz3vS+x9TnZQrnVMoO64kOZ6wDYo0mph4wYYOT7T EMpqr2VYDHPW9HCkn4ZQ8Z2D2OSJMwX2Gf/loeEMpAwQNnxgDh8Gf7qIpCmk7L7tXX3w qi0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718937057; x=1719541857; 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=Nj9YsUuVxfs7h8btpZc36/W/bj2gfery45cbXZ6Bur5f5kucVyLwQBGlpgABYoL7Pv 7cE8aOQOpstjD1pV7PNB0MwOo/Z98CzbKWXUwR185G+yvrGXjfyOinvslfSveqkzs+q+ iCwm9E3sJAAJVM5x9tDv1Ky0M8IHwYG7Cek2edx0PzirTaQnIUePUep6cXULkTYLSR4T TNlNkrniq8NP2smMQ+xv9sIxO5BlvJ1G7qsBbj/yho1rOBfLpqLZEh7VOHXt1ID9TK6D rd5Sp9yyV5lcCWRUw+8vvEdtn5uB82hCAMjKdhAFKFK/p/qQaa+0oJyRytt/ojcLz7bb SIqQ== X-Forwarded-Encrypted: i=1; AJvYcCVwAO5bkBghE7zSr3TzREMbf4IXyDyeAhuAkahIrXgxfFG96jcRrYbVSLsFMgmEKniAFZJxkY7JLzx7yG+uaQU/nSgFD9kWA821gBvnjDbFQ/Z3MjstYu0HsmPo/Vz1SP9UThgStDf0A5tXN+9iP82WhEte1io877YsfY3D4jz/8CW9t/8UBTH3U3nnxtI4HL1bTkYt12DUO+av+WoBOd+leKdriM+AZnu4Kt+umyKSLFUbSBVc5J9tdo91yEgcSQdI+OLPQLhVuRIaxkzkcLlC+4DnNIAZ/XA7u4J1JJLwX/nItWdprTYdFTsG71hO/TQ1W3GcDQ== X-Gm-Message-State: AOJu0Yyosj9UDxKssQg7VrdNuuh9szSS2uNTrZxhEPsuLGdeJ60uIw/u 6ElpRKl8jtwtxL76t+IkcGltu1bi2dxi75xp4h1n6qPna8qCfnh/ X-Google-Smtp-Source: AGHT+IEXObDJFHHEU4ritIMxu9doHb5HpGV/yXlLGQ5OCeoryIQ6hRw+mWkpUWLLJXrRfBg1YmPPYg== X-Received: by 2002:a05:6a20:9313:b0:1b8:b517:9bf9 with SMTP id adf61e73a8af0-1bcbb574a9cmr6631143637.25.1718937057021; Thu, 20 Jun 2024 19:30:57 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.24]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706511944d2sm332488b3a.70.2024.06.20.19.30.46 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Jun 2024 19:30:56 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, akpm@linux-foundation.org, 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 v3 02/11] auditsc: Replace memcpy() with __get_task_comm() Date: Fri, 21 Jun 2024 10:29:50 +0800 Message-Id: <20240621022959.9124-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240621022959.9124-1-laoar.shao@gmail.com> References: <20240621022959.9124-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 Fri Jun 21 02:29:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13706728 Received: from mail-ot1-f43.google.com (mail-ot1-f43.google.com [209.85.210.43]) (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 68CAA84A5B; Fri, 21 Jun 2024 02:31:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937072; cv=none; b=lah41e5lRwtNG7buqlPf65K1dpJ8orS0gxc+jdNUyl3tGUnJNS+RmXOGu7pNzNYn54k7/nH0xrV2iY/Aq08ZMnvXGAdknHLLeFenC90djxmuBmkqP5FidmiXFT4pp83sxtVyBQpxmlhHqHvaA0yy2W6NskziKjD2VXmWKjgVh1A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937072; c=relaxed/simple; bh=yr+SpMmeHSi+vcjVeTr/i3O/gnrB5nRciCU2m0INHKA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OkjweaBf21e5tRiwyjB06Z1L+Ci2tzefWFhLvZjFdpL2sdjqINk6xeX1garc7jOSy5V9N7/mDZudM5/GEE3nMEY3T1YE9exWXGu09qEEdYap1hvCVIAACRBYRNuAzojMobgEgnklpDW+vtaxbySVoYHi6+wzhMuB/Uq7dno7+4A= 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=lGV1dpDf; arc=none smtp.client-ip=209.85.210.43 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="lGV1dpDf" Received: by mail-ot1-f43.google.com with SMTP id 46e09a7af769-6f8d0a00a35so1453616a34.2; Thu, 20 Jun 2024 19:31:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718937069; x=1719541869; 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=lGV1dpDf35Wne9DtyCr6Pu9VNJAQSDGz/+jf//5oYLY+Kxjz3ug6f5Pzsd7xmYomkJ ywCXaRydIYPBaVW3pkOInsR9Ugynyr8xsa5SFHToHm4GwOUXn7b8u048yO2vCKGe+hfK VG57diXoHD2Diyz6iznEyYb7nbvnSbRuXeKK5J8qs8f+594w7c9Uz7aTxYNT7fDoaimu sVvB/RNQk5KL2BoPDpcDTQps9hODVZYfNmjoFpCgCY0bj1wOluM0JaVssikachrL5ME7 5UMlfyIQqmHWrdQgDr7oTykgo5qhf3a6ckf98GhdhtoyCYSw5fbF4FK4U5mIPvowcTBo EDwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718937069; x=1719541869; 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=r5d91Loms4kvZzmW2FWNx/NT8BXIghnsEq7Moc3oXTG+JUzGMx970oQ0xfsHJ7F9FX cUEg5bl3XQWutSYxYEbVUGx0Eohbju5VUAaY+MSRw34smykpZAy7NzSabCCP4vWHBzHW MMbmnLB8CuEE0zwOEqEbqSb7CARUzHU8rjkwn5GbreCKRYRheIIQAIYj81luo5+Hkyy8 Dn5oZ96S7P5vtEAkpG7cXDB7rTiQlDWsMWqVGFs3GQvVbvgekDSQL8mLKPD34wsKkcQN V7QECxvbu8B2SGv/FU8ynxN7iOg8VTpXZtMOkLkbtB60BaWs/zyw53zdgMvcxtey1vOV NIyw== X-Forwarded-Encrypted: i=1; AJvYcCW6TM+bTmRx3cMcTyoIC3wXIza0qxESaOlnRJhineoFtOGsU9ZP+e/DIF3mo54s0hrtrawEmdgwCkziVMmcuiE/llIdmbp5w7CQZeVHw234e1vZu78Sgg3d9Qxbe/LNCKgpHN+16WR02Yakq9W7dadyyzGXY0gGVzk9LmPmO3Gf4Z5bjt09FKfT5txyv/k+reI5SCEs1tc2sPVm/MQ3cvOT3mFuZjX0SRFPOQAQvaejnL1PkZ81c9xLuKqKxWEyM0dllyPEiK0mW37tJ0T/7UZJ2BXYXVAZ3BWddYHBxneP21yWvmETtQrdPgX+RBruGhamn9KnPg== X-Gm-Message-State: AOJu0YwofpxlR2nj2fKDqdBfUPtK4UdxCJoCCQ4b4GA0HcLzvOUOty90 ETQB2kywnkKBaMmazPpaUT9nyT1ZvekO15iie9AppbQ4nUERd/2h X-Google-Smtp-Source: AGHT+IF9RQI6I8fwWnu/VEcjE+HIUegV9PWFNk6TPRJqj9VSP13v0pKuFsE10NjsKk43i459GXxRWg== X-Received: by 2002:a05:6870:b52c:b0:254:7a82:cb3f with SMTP id 586e51a60fabf-25c94a2368bmr7837289fac.25.1718937069341; Thu, 20 Jun 2024 19:31:09 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.24]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706511944d2sm332488b3a.70.2024.06.20.19.30.57 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Jun 2024 19:31:08 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, akpm@linux-foundation.org, 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 v3 03/11] security: Replace memcpy() with __get_task_comm() Date: Fri, 21 Jun 2024 10:29:51 +0800 Message-Id: <20240621022959.9124-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240621022959.9124-1-laoar.shao@gmail.com> References: <20240621022959.9124-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 Fri Jun 21 02:29:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13706729 Received: from mail-oi1-f170.google.com (mail-oi1-f170.google.com [209.85.167.170]) (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 5728D84A5B; Fri, 21 Jun 2024 02:31:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937081; cv=none; b=T3of2x7ot+AwRzNhO1pD8yjYw6lgaPeQwlPQIJ562p5/KCoh+LLv6EUN1aQuSt9foZJ2pRmcFM/TqCIqOGeWI7NDu/rqqA2NDx/qJB6U99YWt6/I+IH0VP/dZW/ZqXx7Z7+hL9SXwOr0slxhQptSnwjiuNHhm1Yb/rC10bQQMIU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937081; c=relaxed/simple; bh=a+jRmBNObp02hqUJFQx9ZHvGN1Vdde7wossr4Oq8bKQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FCfmAe7Q1WhYZR3VWDZmvg+THc7zLFFgeMnXY3o5Fz9QSk1UBnAL9YPy0DGDxaSxxoxpJRdzez40ukCy/H/ho99cRvI8oYWT3l/CUwjkLSqkRFJdymMkX7Mk1gXvtHJw4cD4fvpCOZAm739HGnowtQLR0Mt70JLydWqk3eX4Fy0= 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=g4fzS+ip; arc=none smtp.client-ip=209.85.167.170 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="g4fzS+ip" Received: by mail-oi1-f170.google.com with SMTP id 5614622812f47-3d22378c59eso888231b6e.1; Thu, 20 Jun 2024 19:31:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718937079; x=1719541879; 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=g4fzS+ip8eAN8OoA0sPVgfMtS7X5NeNZXUhNbWRvFHwhjkRKrexnp1qgMKHNtdsHbH NeirQBqwFa3exmtAvTiLoXAtrlohcFFXtXPqrVWXGQvv6IX0shoipdOzpYNh13EBaCez wUBlP7bSP4xSckBrgTSJLBB62/dVWYBjpRRpmR4urucFao9R3i5tWfPMB7+2mMBc8Ulc sTbO2jKNgcNlo/L3I9rh8LLZAQ9caSMi8GDDyTQu+lLrFQWpjlgG5d2+ODNzmRWuYroc ZJveNvIp2CPqNuTGD67Ofyhhfqs6ed7PV3SkeBxH9SV8ciCYmY7R91tjK85kDW/5G0Wt xNZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718937079; x=1719541879; 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=N96F1KLEnILGPQ5Ze3sGWuQLvbTrxnTivpYMiKeO0kfE0ki2UvvxiujAt7lzmXbmSD DVdYd+EivuLPtxSZWBRrsInXGxskQt0LsI9A/dguiWZtyCNbo5auEeJwfokytcyaMPim K1oV7nRPTjuEZPGLRs+bzEiQh+GGLW+Ba6qZpSDXmV1yMPEAT31oITDcWCeLuuOMMnF0 Z0Svs5FhWje5JafPb2UOwQFGq6BbA7Vn2k8v+69NhQjsV++q6VvKfnW1x7XnnsqTm0+I IozcV1CoTETnfxj8ntsSg9Is8tbOfC4OMDqCAq+LBSoGBWaMMWjEjdkV52qwlhgC/cOb wDfQ== X-Forwarded-Encrypted: i=1; AJvYcCUBSfjLL70f/YsZ/Y/RS3VXiGjmlxMVWSCrRHSR3pfMy+5Ki+V5bKBbgucD+Vd0JgZjqBl7i8gP/8o+H8RezhpShlqzh8eMio4Kwrl2M7RoHWkKlsP8cWp+/1koYNCfm0GKnJ8yWe0f64ND2xK2mJ+FPPqqFbRZPvK43Iyt24YAFHs2NmYt9jZznpxVZg9j5bNlkqVAmk7BIiith+2ThHV1plVkehQUaZbIMn1kYYCKVvrhOj7Ez0rJOYkLhYxfeln3G8KVg7+Zdb/QAsi7i4EAOw94MeDytSFFMo591zcxAysZZrmsL4GjVR6c7JXrUSnfzGZXhg== X-Gm-Message-State: AOJu0YyBQ2X/WfdqGuTbPXGDBDmIvMmGX1Zuj4e94Wq8Z44ohzzOEQhL SHgJ0jVsKIHV6L2Po/z74nvPH5WoxlLBg+tJe8l5Jv+Rv3jI4etw5an2Ka79 X-Google-Smtp-Source: AGHT+IED/biJ/4HZ3XKtIPvcAUdQBx9cm4PcU9a703LBYQtgmllriwsa8W5+N5BARKh/qYuDuJagiw== X-Received: by 2002:a05:6808:f07:b0:3d2:21a7:8629 with SMTP id 5614622812f47-3d51bac4b56mr7794777b6e.41.1718937079354; Thu, 20 Jun 2024 19:31:19 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.24]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706511944d2sm332488b3a.70.2024.06.20.19.31.09 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Jun 2024 19:31:18 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, akpm@linux-foundation.org, 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 v3 04/11] bpftool: Ensure task comm is always NUL-terminated Date: Fri, 21 Jun 2024 10:29:52 +0800 Message-Id: <20240621022959.9124-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240621022959.9124-1-laoar.shao@gmail.com> References: <20240621022959.9124-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 Fri Jun 21 02:29:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13706730 Received: from mail-oi1-f179.google.com (mail-oi1-f179.google.com [209.85.167.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 B782153363; Fri, 21 Jun 2024 02:31:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937092; cv=none; b=UZn4CmnPUuhruHovIrQkXYoxaDBiYqXp12LEhTVhpIHN/4Yqg51VHKvoc1cCucD7Hunt+v5XEC3K1WNud0bppozQcEzcUCeRyHyXoc8GdItluE5+85pWeFRVDM8P4i2tTReGk/BoJd92V2wbss0ieBfJLBSgtYhre7Sdm8Nm15E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937092; c=relaxed/simple; bh=S6xlHCUZ8RVj52nTEtyM7y4GWTBUgn3H8PypqmREZuw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YpCD2JU4RJ2TJh0DT1UXN2p46yPXKH3PJYUkyaa4/ihsIG7DoGyGL0uv+E6+dvUEAmgwyOUy6tt0O25rsoNCcDvATTeng/kRdcUJBQxYOGcs9heCAKpTYSJTnq2xO2uv7Dn+IQz9P17h2h2zSoazQgKLjeMeHmPbyb4yHuxQwxo= 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=bFMqnkof; arc=none smtp.client-ip=209.85.167.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="bFMqnkof" Received: by mail-oi1-f179.google.com with SMTP id 5614622812f47-3d218ea7750so832375b6e.3; Thu, 20 Jun 2024 19:31:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718937090; x=1719541890; 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=1K+hSkFBa/z9wm4YENh0APfdLPb/9OfvoDMj7Q9JegI=; b=bFMqnkoftcJATBB5G1orIk3RzC2QKIHHxJFG3K/Gys/y+WYK28OJtk+fxOKJKhA8LS Ca3Yo9OLxxocYjZSq08y0d09SrfynxRlEe7IW9eTcjy+67yR5fFzxy+TR8FUHA60hOSo eLyPvlXeR4/cSBHqWfNUwz7r1r8BGC9UbO2sxhWq8AZf81JthSmjn4yddVdtCSzBCoqS IIQnv8xu/0q4fC3f4lNSF6PCdY4VbQpPW+OW6PQ4cCx0rD6spCbpuEdYFCYYPm9cHDUa 9OI8ExqitjdM+lkchkObt/lt0tnYYUXE7xZpYRxvaHPkHaKIsFUDmehsaUttemSVwLXa A4/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718937090; x=1719541890; 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=1K+hSkFBa/z9wm4YENh0APfdLPb/9OfvoDMj7Q9JegI=; b=FDc2dFDi9YBxT8ZspGuWh22p6Wq75nnAHEJf+OJe++IFXU7w51hkepSgrDe+qVhh7D C6I9NiicrMDLHSsCMCkCSkQXVWHvICIi7xinqu086vQJsYHpUxSGSGbpE9avb7vS5Qku 1nEm4e9IFDB3N2uOmbAGPDNQfCCz8/mdqQ6SRPHIYUc9d74Uy3VENiR5inGzYCntEM6u bumdOTcWvRfR43Oj70BywnWBoQ58LxB/r9eE19c8/FuBD8ILRkQMjH3NrKAQQwYQdcZZ O2qv7TXUeoXyWSD90YcFexSqAAKxrMJxDAwc5t8XQnH5G20qP+HLHHD1TWly/uchHp3X atVw== X-Forwarded-Encrypted: i=1; AJvYcCXxuxyhvqI2J/PjueOpySFIoVQe63OloLy6GyrGlIyvbmlNkzACvzAKlJoagrGW9AatvZmVx4vPNt32/BTy/2xPfUmoLQgvn6BebtZ069tsQwP9PVJG6OYvW5gwiX7KkxGs5S2Cb3v7iGKVCG6UvkMl4PTNqHD6Cm4kqjRF7mPgHCPsYfkNAeRODzv2yWyammJJM2pyALrYgcOGzvl22gzRl5xvlo9vB4/6b345+rzwielhNckGY4uVDWgS4NTUu/ZQMF51KpySm/QmdKjTX85O5hKQ2VwFxR67Ix9rBD76BWYaWroUdA2Q+nXBG6zj1VjzazX62g== X-Gm-Message-State: AOJu0YwkwzE1U2ys8lCr4e6V4QE+SDI+rh4ZVOtt8WxZY34Lb4bEK4LX yJ3EbI78zu9ZYWlargvOHTxA48j0iPOfHUNtSwT7QxJMHb1zD6KJwW2WfpyU X-Google-Smtp-Source: AGHT+IGPBhm+odN2LJAX3QWj+Jlu2WkC5FUNop9iFT0Xeg6XFGC2MN7wYjSEAKCdXzo9nJ50WQHXKg== X-Received: by 2002:a05:6808:1789:b0:3d2:19da:9573 with SMTP id 5614622812f47-3d51b97ee20mr8962729b6e.15.1718937089513; Thu, 20 Jun 2024 19:31:29 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.24]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706511944d2sm332488b3a.70.2024.06.20.19.31.19 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Jun 2024 19:31:29 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, akpm@linux-foundation.org, 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 v3 05/11] mm/util: Fix possible race condition in kstrdup() Date: Fri, 21 Jun 2024 10:29:53 +0800 Message-Id: <20240621022959.9124-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240621022959.9124-1-laoar.shao@gmail.com> References: <20240621022959.9124-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 Fri Jun 21 02:29:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13706731 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) (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 8ED5284FBF; Fri, 21 Jun 2024 02:31:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937103; cv=none; b=HeQf9wACQvpA7HQcyEoT0EX82yHKQXDNAHLKJ2ilnmS0ta8qmIP2juKdPHRezqlmqN5z7n2/IK89qQMHa6d3PsIZ7C/16oPvDN38eD4nrgF7htvNuIyAvT1zzZFkwqZVDrfwmEE6OBbstsGiTXukuGfnDWJsxhQZ2rvcqCWDBuM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937103; c=relaxed/simple; bh=Kkeo6MG1a3qIIQSVqx8qXRklhPMiGFUJA/nbOkwLjy4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SAM7vlOQYwpD55QPIhglnj6l+LIYyoSMGuwxw8cp6QJwdonzYfsvZy89MJ8kthWkimB8U2B2biLwLIPcf4Q06pJTII8mmSfJZ6XsKeamf7UMYGKA1cCA1Q1aRocTK6uLiZXfjjN6bhuyaSUV6e3mrrxh2lEYJNHbbcrJosxP/7E= 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=XWHwxr5I; arc=none smtp.client-ip=209.85.210.170 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="XWHwxr5I" Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-704313fa830so1321029b3a.3; Thu, 20 Jun 2024 19:31:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718937101; x=1719541901; 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=gIxIpX3fSDij30wx/6pUEm1wGM6JrlT5xZ3d0lBG2nM=; b=XWHwxr5IJs5pY+6aUeh96ZLGYFnEGX4kNmZakghl4efSWIgcasnVezFzrcc1u2M0jG 4zppdszGMHZjwgDADgRTgjyr38/rtGzVQINL2szrz48vG6pR8DHt1iXl+Iq/JuAG3MHy UYsbLbPa/7GoN5CI0fCIxo5f2Yfpbw44XaVIDZLup9gvOWAPwOOy/k1/0314OJLqunwv ZEvj2KkzyiJiRGU5f/FvjUAGyKxPxVEaLIXhl6l+geJ0fu4BLkWm6yH+5n8eof8kxQBL S+u3vVlsiNo5YL/V3/tBHxiv9wDuL3wwpQwYLa8txuY3Lu3GnU12w/EfdL420cxZb5Rg erEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718937101; x=1719541901; 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=gIxIpX3fSDij30wx/6pUEm1wGM6JrlT5xZ3d0lBG2nM=; b=WOrqIejJubhdLHUmTPS8EWzHltCWQUHDEWqb9G/SssPbRu1kUmEKwywRLu4/gzr606 ygdz4e6mXmdT/dbFOpv+QpQrT++w7YP3WKgTSoh+gIcF31GczJNy3Eo5CSO65IrDQkly wQSoEoeiFmKeMB0LrUN1Uxy0g3P63jgA9NWn2Hp8xsJqlY8XRV4DKHfbSbI9lHyCLfvw NEYLrvlGOV9zTNjW4SWT8cA/sgZ1cbHiKPlBYI0p/xv89YR2dKYN0j4gMFhmSUd6gRqS sXRUPxrO010CydEfNFEywGgKGW3rlfbL8jUBCE/wBWQ98Y0MrFB9gDHRyHQI4ye57S2i n6sg== X-Forwarded-Encrypted: i=1; AJvYcCWooEQrfZMjOEoMekDBALm9DF7NZyyhvQ0wZ5XmOrl2Tx+OrnS6ct13Ok+2ckpoTVas4epdWfAHuXCBaA21h3+4WebMKJoM1fwqj+PZ3UfN1sinuMaCzijvZbjW3XYFLnmiv4E5nAy8aPiIgbNTpkeaaIO3oOsgPGtowmlNEhafKpuFy0LtlpynXFQc8emVmNij0+D63pgVyhBFMi+XSS1qg6uNTLycenkXR0ITpJWj7iWynYm/NmycuXOELMMENQBDaqQm7BoenonNDA338x9NkREMlgegSB1ioiJjCWzPfcfj8pbpw1sPOPuKnV7grhA6Q3a2Pw== X-Gm-Message-State: AOJu0YzkgX/VvHhGl2XB6dqK8dpiudreTSUieOa9yGbzcSiI659KrsR2 81Wzr+hMfuSgcePHqCXDzf9RAK7p1nZYOVkXHS05Tc/ribOAIZWH X-Google-Smtp-Source: AGHT+IGYNoV/DB/o6zRof24rJ6xmZEwxYI9LpBEDyg5OiYjfuVgaYmaCgemL5OUn4y/KnIdaoKGjig== X-Received: by 2002:a05:6a00:2f43:b0:704:2d64:747 with SMTP id d2e1a72fcca58-70629c1dcdbmr6929306b3a.7.1718937100840; Thu, 20 Jun 2024 19:31:40 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.24]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706511944d2sm332488b3a.70.2024.06.20.19.31.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Jun 2024 19:31:40 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, akpm@linux-foundation.org, 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 v3 06/11] mm/util: Deduplicate code in {kstrdup,kstrndup,kmemdup_nul} Date: Fri, 21 Jun 2024 10:29:54 +0800 Message-Id: <20240621022959.9124-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240621022959.9124-1-laoar.shao@gmail.com> References: <20240621022959.9124-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 help __kstrndup(). Suggested-by: Andrew Morton Signed-off-by: Yafang Shao --- mm/internal.h | 24 ++++++++++++++++++++++++ mm/util.c | 27 ++++----------------------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index b2c75b12014e..fd87f685739b 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -1521,4 +1521,28 @@ static inline void shrinker_debugfs_remove(struct dentry *debugfs_entry, void workingset_update_node(struct xa_node *node); extern struct list_lru shadow_nodes; +/** + * __kstrndup - Create a NUL-terminated string from @s, which might be unterminated. + * @s: The data to stringify + * @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 with NUL-termination or %NULL in + * case of error + */ +static __always_inline char *__kstrndup(const char *s, size_t len, gfp_t gfp) +{ + char *buf; + + buf = kmalloc_track_caller(len, gfp); + if (!buf) + return NULL; + + memcpy(buf, s, len); + /* Ensure the buf is always NUL-terminated, regardless of @s. */ + buf[len - 1] = '\0'; + return buf; +} + + #endif /* __MM_INTERNAL_H */ diff --git a/mm/util.c b/mm/util.c index 41c7875572ed..d9135c5fdf7f 100644 --- a/mm/util.c +++ b/mm/util.c @@ -58,17 +58,8 @@ char *kstrdup(const char *s, gfp_t gfp) if (!s) 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'; - } - return buf; + len = strlen(s); + return __kstrndup(s, len + 1, gfp); } EXPORT_SYMBOL(kstrdup); @@ -111,12 +102,7 @@ char *kstrndup(const char *s, size_t max, gfp_t gfp) 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 __kstrndup(s, len + 1, gfp); } EXPORT_SYMBOL(kstrndup); @@ -195,12 +181,7 @@ char *kmemdup_nul(const char *s, size_t len, gfp_t gfp) if (!s) return NULL; - buf = kmalloc_track_caller(len + 1, gfp); - if (buf) { - memcpy(buf, s, len); - buf[len] = '\0'; - } - return buf; + return __kstrndup(s, len + 1, gfp); } EXPORT_SYMBOL(kmemdup_nul); From patchwork Fri Jun 21 02:29:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13706732 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.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 587AC84E0D; Fri, 21 Jun 2024 02:31:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937113; cv=none; b=Yj4HIT8+OzFcyEnwzgZUFKnBGAnBMC8w7Mb3xvLLSxakq+/6cEUdK+hgjaCOWToVEQ4P06eUwoAdnGoUeWbttGtyL10ST8rt+DetKxesWWt1+G7aykQi1xRKotHtb8ztDg0LnPkgc2Xw1ZUErCdlTrGoEpOvEr7xRRvjVG4qOBE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937113; c=relaxed/simple; bh=ifRKs7FUaq2eQBo/kGf5I53pQ9emwiKOlYtEkCk86eQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UbuG92hwwN6ibMNcRRPC2RsmnRNtIRAxpTuPITc/SuOXDfiAV56NzuexZp09QXhLfusuRH+T1Cb45MfkKQO/rX/3N3kDUBktFFvdJB0rmZLtlZoBGm/fhYtuqajE2QOxo2dPETItQCx40RYY4L/155foo5KB9GQHFeUEnh1psx4= 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=RwQXlcE5; arc=none smtp.client-ip=209.85.210.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="RwQXlcE5" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-705b9a89e08so1382351b3a.1; Thu, 20 Jun 2024 19:31:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718937111; x=1719541911; 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=WYd5YNknE07k5DuwDR8qZLFs+IUn8eRucnjF7OV4ohA=; b=RwQXlcE5hrb35+VgfdvD75xPvv7ifa8RzpW2gX//atQDNifeR+uJf9I9seePfknuoB +3hzeJ/I0a6hZm6f6AiH+CvhCMjGTBjtRqXziGfhML0wWUhgwVJcEKoLWRfTFXoOMh49 WV+PZ7qVPOyFF4bkEdFC5wyF5n+VOzjJWs+6JTKzst4IdYt2xxS9qtdUZCPNSB4wvuGw Vm1d7v7F5ZIZ/Ili/nNG4MDVLjmee1R1hRJpb1emceC6pXO/AhqznhXIcktJHNsxSWic otIk8yVoetavtsR3zCKO91y2EhLLkAafOuI6SGInCLkAm/OU8s7HzR68ackWgBfBYyrv e2Tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718937111; x=1719541911; 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=WYd5YNknE07k5DuwDR8qZLFs+IUn8eRucnjF7OV4ohA=; b=jI+s1qjyBhPHIHU1hAuUIpx9MYGqWJ3ToYVb2jhCOj7IvnpRn0T+BJh+2gqncOw8/6 kq7dEerBDrA/ZRxWf3r6ZH9XaAaO1Hjj7iRa2qSt5xTABsjE1I2BUxBGNCQFjDVJEo7t AXsTRUJPheNIxhDUdsMINt2ujbz7hOBYp0xX+AqsogC7+O+9BWrSR1v4qmlqfod9YVPF beS0uza1qgqlpJ5+a4pz2Qh5S2GZXbSZbRlcQlRDbI99fe8WrGuKPO71PQ7YfEVMbCT8 2GsWR3RK1Bxg66O84koI30SI2t+Yqc9UN7zIbzDyZm9upb9FkeE3uJSQHllGFFRBYvtv /qSA== X-Forwarded-Encrypted: i=1; AJvYcCX/rWAUj8pVz7hxfq9s7gbwzdL9o/llonNY9CgJ6FPcRtq3gYLXV8AlJaq/UIkdJ1uprupzK0y6WB1pNcOfuQ6MvjENAyGBXwWVe0X10eHj3ZtqLIctkK7t9Q6ArE8yGAiM6DLJisNRPPqInGIWsNd6gzIBpvnFnx6j/ovhZ9U/PfgJ4TIN8DdFg/ujj2kFjQS4xF9qsTiQmN/K26ayxau0oYudma7LU/BESEFzoyOjJxIQhp3ICCVeWi2dFWV8AE6LzKoRRFCR2srzZYB77s2mb7cHg9+jgamgIYW0kjpLkW87ftkZr6eKOb6Dfu+YFXmCfzo0tQ== X-Gm-Message-State: AOJu0YxHWxDmBeM9RpoG2/avuR1MB3pu0ud7o04m6p1ycIpTRQOZQJNa dHZeXxXap6kwUJHL0+4832Xt73Qp5n2RRR/8sL+EGPxHUUC0DV/D X-Google-Smtp-Source: AGHT+IE3vqRU4TDjYUtGu1BjTuY8WxnNKb7eQY82UPvhwXZyWJtswQi4+ta6VB9SmiN5potLRkiFDg== X-Received: by 2002:a05:6a00:853:b0:705:ed06:cc64 with SMTP id d2e1a72fcca58-70629c521cfmr9225541b3a.16.1718937111523; Thu, 20 Jun 2024 19:31:51 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.24]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706511944d2sm332488b3a.70.2024.06.20.19.31.41 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Jun 2024 19:31:51 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, akpm@linux-foundation.org, 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 v3 07/11] mm/kmemleak: Replace strncpy() with __get_task_comm() Date: Fri, 21 Jun 2024 10:29:55 +0800 Message-Id: <20240621022959.9124-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240621022959.9124-1-laoar.shao@gmail.com> References: <20240621022959.9124-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 Fri Jun 21 02:29:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13706733 Received: from mail-oi1-f175.google.com (mail-oi1-f175.google.com [209.85.167.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 53ECA12C489; Fri, 21 Jun 2024 02:32:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937123; cv=none; b=aE01EBViQM8nI4MWzZO+13le9eNlvmAUkWbiJo3xJmWnNA+xuc4sFzO+N67RuPKT27UhrXuMtIwEdqnjEL7x5QPJZqC1FVeAGNEzulHClmMCoTg08G5tw1w6k49Fz0C7YIB6r+Kou4eYFcPqwj/GcnBT53Ng9JTXFyG2uBGxE9I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937123; c=relaxed/simple; bh=xJqJZWbEhk8Nib3lnX/uTBcWIy8/xwBU6awufruH+d0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Vv4ruh0wHH7enF7uJOIOTlSkgaQB5vkOvHfbnRpTflZg0shxHUmLGXIxnb2CMoo+agt2WrDWOKLkdMsqaD89EfhvVS/uAYx1Geh8E2TkInT1QuchB84fjcBIRn/aMqJJK71xYGEtsKfi3xOEZ49+McVWd+ra//PaFf2MEdLQDBA= 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=IMEQqzI8; arc=none smtp.client-ip=209.85.167.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="IMEQqzI8" Received: by mail-oi1-f175.google.com with SMTP id 5614622812f47-3d21c4eec2dso741769b6e.1; Thu, 20 Jun 2024 19:32:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718937121; x=1719541921; 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=IMEQqzI80+hsHAllY+c9YAXVitoMwSkV139Jswy2vVlO769OjdC/MITLAF4C+qs8q4 uE9IUNxMCjv2bE4PxytiDJ/5mg+WXo/08Y2IGxhOIs5dwxZHfiLQdNRrtrhqN8aSrUcZ +V5aD1t4bPF8cD61lUTy4ke/7KoDzrSY6OT+5Gq7MVzFGtfsPKUwnwhv1xQQqx+UKCSK U7TJeERRMV2HnHSyDCPj29ChLb2LAhWFhfxH9XO4qsMC+BQ+iiGkknt5cC4zrg3krsyz MuLtJrBurNJos2RtdCQHfz9ba1t4n7wmDwqzT5nBS5EenxJ9XPIHotEhNgqYamlZGQwM b7Cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718937121; x=1719541921; 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=Mc0/aHhx2FKxxfZvOe0tm2Sj9e9kYULGLaFk0kjFJrghVdxN2eJEq0BtZbZ2gWWOxP 2/DIZ6EEGZfqVbozRDXgJdB9MYwtu8MyIpgJJTXEUh9kVm+o1qcgT8qowsAs61p1BXW4 WaDIhFl9QmrQiGdXFqrsENz/fDkHM70zv8ntmvhofJAemu/SFahZArPclpoKluFNxmyd hLohmpzEXqX86Sm0ADDsMtZ8FuV25bhhGn3STxQ/rJqGVVI9A784hTy6vEU7KhtUDP0L xXDoZDT7pFXh8FLst/DkNsFsTxz6yuMIw7XyUfljIH9M9yxEVEzJjkcwZN+7U4wpt6jW F0/g== X-Forwarded-Encrypted: i=1; AJvYcCVbnFPCj+p+xXZ6m/azxpwRVREeNsEZ3eqAxIeSoHf8vVyT2d+BkeyZZ5/8t6oudi/PhLDaYOZdIv71KV2vAZ3DjOyzWCMJxxaBTXSR3lBvRUO3HudjnPjAqRg1M6TYCM9kxYmS85F4BjliVFxhLM1oLJ+FgowujWppac04NaMkhbpyX0cJuI4OzAVZmjtNLW4DPiqrsFdhJcseNL16zLb9Pl8PIYfkR71P2weG/l7D/GLT8O3/Z7k00tU+2v8ZSG+rOAheR6g+IjFvTBGaTHr2MxD94RuY/4wPEca8NaqVl/8OqpaFMXS7gTqBMeRnMoPYJ6k5qw== X-Gm-Message-State: AOJu0Yy8L+4d+l6Y5eHOW7VmC/Rrh275UE5m2M8QWE+6xnFWvt+4Wmnk HX1QpgieJHZfIfgOXyn56EjwFuvR2/73MRgXnOsgh5fcUbOVVHs8 X-Google-Smtp-Source: AGHT+IEDLpdYIw99Uz7LWzb9Zy/b+1+lbeIcbqra7aNgXOpLlSyDlDvG9wKvxKsFRnB2arbPqu9IVw== X-Received: by 2002:a05:6808:130b:b0:3d2:4a08:2cc8 with SMTP id 5614622812f47-3d51b9b4558mr8069073b6e.23.1718937121450; Thu, 20 Jun 2024 19:32:01 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.24]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706511944d2sm332488b3a.70.2024.06.20.19.31.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Jun 2024 19:32:00 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, akpm@linux-foundation.org, 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 v3 08/11] tsacct: Replace strncpy() with __get_task_comm() Date: Fri, 21 Jun 2024 10:29:56 +0800 Message-Id: <20240621022959.9124-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240621022959.9124-1-laoar.shao@gmail.com> References: <20240621022959.9124-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 Fri Jun 21 02:29:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13706771 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.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 4C16284A5B; Fri, 21 Jun 2024 02:32:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937147; cv=none; b=K0KBKwpe4qM0OhJ7PsF0D417IlaAF4vJkCEt8WErIeKenFrvdv8uCQhjDoTc7KRJ06aM4zAKaA+pU2cILuX4Kydv+aUITvm/A1bIdt9/8t0WwP4X7wwHRRv9KHatWxp3LTr8F7RAGBMKgaClvsTCirQySiJjpRpJn1zfYJQrqy4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937147; c=relaxed/simple; bh=/gCH7e28NIl/tMHPCXOAQ7VQOVOI1s0RashVXkQPLGY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uG5EqVj6yOelhgPkz7D6VcyA5cY4K/djYXNzNwmetV38pDWS7UwujUUz0X+bt1IKwPgG1PnXTwCMNlEoou9NI9XWZmxftSJFhpIGj4dTFt8hfE09DOgCZSscHApcrEdimxCV0Qtqrd0z7ZInE1pzl3NwG5T0GhHvgbVe3RWM+pk= 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=etnDdbay; arc=none smtp.client-ip=209.85.210.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="etnDdbay" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-7041ed475acso1456251b3a.2; Thu, 20 Jun 2024 19:32:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718937145; x=1719541945; 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=etnDdbayegwNamUecBGMT5hk8++KoNSMBt1RQmsSZ5IMNkbbnra959x+sdLt9ebQZe BM3dcDpezlRD+FvXG0ExYULCVI1AQgUQY2Yphq4Irn89kTXqx0shvTAKhXiHcv7RI79J wryCjJ8gsH6aKWCd/NX1VgHBEKUfg7WNcQeMZnnAZU40OG0KN0VvPsfxNUawKllfLlGJ 0Abg6Vd45dXB2OyMw/rHdsJpAOvRdstbF0a9QPMDxs4b4FhXOlrrXV4ImjF9hRqiPObV aLKAdYjeUXhlaZgBzvnL38Ek4C6LQSex4Yk1TUVcpc28CV4g7cVd/bJYtJjZ5f36h6vA aYcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718937145; x=1719541945; 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=d0GjwgsH4x+BacyNzt3zTtJ3pXj0YAj5QdcJll6MeUtYzRkQDPWfCocERVXhuoo9gx Nxc8TUvgR98n9nq8lHpwEIUXK6yIwkB1TPVwu3olCClOzI1ZFVU5141B2Y9yKOMQkVnj 5KWst/4cIhF3qa4pmZfiXifSmhYK0/2zL7ScBCKCc5l+SOkKpQh7gPJxwc0HkO6EqM46 x5ntm0NIDXrhG9k/bjUzlt9UtjI7ySSKGOVVpKGbybN0sivaTDVM9ADAZ0+qVsqnmxB1 W1Vpczcdw5XSiCOP57cwHCp9KsCkUc2FPQIUVTZNGyTWUaMIAWKszbCmROJ3INfANJgg f6zg== X-Forwarded-Encrypted: i=1; AJvYcCUwryFs2ctgfS6B/qRjUbvslj+JxO9qTyWu4vwH4von3ABISx4XXo5je5vQurvvw8KhqlhaiShd+GCAko8ywvMw5g3RWiZM40kYpaXEJYJjieuMAp6lVB1oZbN9GVu6+VMCGyxBQT2/NRkiqBzOfoqtduILGWGFjQC3mGB/d12otQAl2nGiugzK6Zx1YewSxUsX0GPtlvjnEy4d068vtXceLQKFc8o0AOUNqsJ9WwScE2glPlKcg/Ui0b/v2Yq+Ljt2H3h1Rk4R1ISz2BbEW3afSF50uO+6vSIeDiPUkW78gxAWcVdP35tCavl9uUGrgoJBtVDjHg== X-Gm-Message-State: AOJu0Yz4ekLuzxJz8j4uLlgRWj11H4wDQoBqP8ygcnh4HzRBa04A6H1V X1bOY5ag/FKFHCqSj4dhO3xzTsxbuFTJkyNfBkUBNQBMKouNtmdR X-Google-Smtp-Source: AGHT+IGhhQvd2U/sH2KdV0ZtBjJthL8CznXxKI/3OBzCZg9mpyXqgc1aLbbwbnNImYe317PDiBUknw== X-Received: by 2002:aa7:8b0f:0:b0:705:d9e3:6179 with SMTP id d2e1a72fcca58-7062bf98e16mr5991736b3a.26.1718937132481; Thu, 20 Jun 2024 19:32:12 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.24]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706511944d2sm332488b3a.70.2024.06.20.19.32.01 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Jun 2024 19:32:11 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, akpm@linux-foundation.org, 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 v3 09/11] tracing: Replace strncpy() with __get_task_comm() Date: Fri, 21 Jun 2024 10:29:57 +0800 Message-Id: <20240621022959.9124-10-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240621022959.9124-1-laoar.shao@gmail.com> References: <20240621022959.9124-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 Cc: Steven Rostedt Cc: Masami Hiramatsu Cc: Mathieu Desnoyers Acked-by: Masami Hiramatsu (Google) --- 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 Fri Jun 21 02:29:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13706772 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) (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 7006C824AF; Fri, 21 Jun 2024 02:32:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937159; cv=none; b=bezzjMJXyPMzEp9objaorFqGUVmMiP4hbr3/Y0E4YFFbBA2ul4B2BdR4Rm6eB5SF9L5BaQy3Pul93vtrjvrmmnK9BEHdD40cijv1vlLEaAxLhtzvv5szXg5F8vsGYE2xwa5vAPW2xmhdkgmEmGbXlcbuOiZjWN86FVZ6fNdxdQw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937159; c=relaxed/simple; bh=bcqj+z1NoYts4nxjKW9eq+NmMzQwTovBThDBaNsM4Y4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Jo6q/iWBb5CGhOVBTKEzHFYAd6p6lTNfBvTPjO6DgHojo9/a0ngPO6olMY6fxbxLIiyDCbFwtJIMUdU9fvDG63/l4X48Q5q827L+ssiSw0zuIKy+iFBseF9OfRRv5T55DeZghVN74VZpMZKhRDntVwLxNI01YCjtqDgJE9kHMUo= 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=I1X5K4oL; arc=none smtp.client-ip=209.85.210.170 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="I1X5K4oL" Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-7041e39a5beso1277983b3a.3; Thu, 20 Jun 2024 19:32:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718937158; x=1719541958; 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=mM/1S9T2tBYoZCgE31Q/lEbGdBggp1bwKT3yTD3UzMc=; b=I1X5K4oLYibIFzNVpAmBzaZkc5u/T8QjmPjdxrSNEBm55PReEOMEsb37cfayrJVM+E nTR1fECsdFA4JjE+9U/HYGHprjyJ1tWfZMyE/FNziVeWzzjNeCP5kHEbwtqNjMApnw18 hIw+F63K7g2VKRVzzeJFkLA/AlSLZVWN48qAvCgNzfCOxOgspVlbYwX23TDcS5MxwfbE 7jQoLNNbLMixOJyIIBOaD3P07wzyUkJewetnh6mL7aVYil4aD1eAV5g9X4B2P/MZ8FCo roGW6TMWKt1weInMEBeHkTdKmeOORSOdp8pMYpGnQCKCEs/TP2cCXjySxUmA+FSJg66e rc+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718937158; x=1719541958; 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=mM/1S9T2tBYoZCgE31Q/lEbGdBggp1bwKT3yTD3UzMc=; b=m+JEmjCzPzGxBMSwiMazt1g0+WV3AAETRsthM3PUUBRHLfqEAt7H8/j/XlwhrBhgSu vcL/ZmYsr92VRonhSj+pfkNQSrTbiDyB3JFb3Rht6AXdXjmRnteXCUgZxANhLzUOgLX6 XSU6DkGQjwMMLOMV+DlGAE2DHLd+qhgNoHZEjdMIvlun4jZzvRwknQ1FtZT80rZ+P6b4 HSW9vxnTibv718ECsWxOfcuGkia93pwSdoK6EcFszlgW3yk81HcT4OCGCqHefir1GZk2 fp+dcelkRNrEsdZogzpO8NgUK5cc12/tiXZ6MiZWG3k+k0kWKS88ZGMlG3jPuVW5F/Fa FYqg== X-Forwarded-Encrypted: i=1; AJvYcCW0uF+I+CMozQ7mn57RN8wGIuaM0n2RhBBEnqq2Ma7MEgaEBp5OIyRhKhBdcQvnJvW5/JsN8tzL8NCjpWh7qrT+APNfMSFNM7A4DO1EY9si7p64U/b60vvaRGUF4UgEZlX0cjgFzxieEJA5vj9DuqhZYadrsElGs3SezZjuoe8SVadLPHIK+Cj50lPGkwiAr5brkLE0WhTKEafUkOTXl9tmZ+apIgSIreSLl4NEytkcUZN2QNtnsJU3GseN29GBps/wX8Gyy2wL4t+WSck0OOn+yvsMogif4DtpDTQri7vDLz5jzmCw3MlJOMDRzV5003INnDep7Q== X-Gm-Message-State: AOJu0YxEgfRf2E41hFW7S7823WtJFPmV45he3M8YuVv6y+KoQrxw95L/ eTfWYtzbzPgUikTbN4CvRKvP7GF+k935DxAnsuYxKQhrq7uIchqz X-Google-Smtp-Source: AGHT+IHxOQrDg8mR1N6VheTp8u8eV+XoRh0OG7Yl/1zkogSK0YkzXLknkEls7rTyfvffHbYdw2ahAQ== X-Received: by 2002:a05:6a00:9289:b0:705:9992:d8ad with SMTP id d2e1a72fcca58-70629c6de86mr9102308b3a.19.1718937157754; Thu, 20 Jun 2024 19:32:37 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.24]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706511944d2sm332488b3a.70.2024.06.20.19.32.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Jun 2024 19:32:37 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, akpm@linux-foundation.org, 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 v3 10/11] net: Replace strcpy() with __get_task_comm() Date: Fri, 21 Jun 2024 10:29:58 +0800 Message-Id: <20240621022959.9124-11-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240621022959.9124-1-laoar.shao@gmail.com> References: <20240621022959.9124-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 Fri Jun 21 02:29:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13706773 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.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 9EF4F84A3B; Fri, 21 Jun 2024 02:32:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937172; cv=none; b=JfUWyuh39EWSOs4uj2dh8hNgyonTX0HwwcAcIWS/liYt0/c/w0JI9sX/U5ViFjxkJejef0mllOEB3gtQvro0wLn3jdTlx7WfCCDC5sY5RrmgY2eKsn5yv9VSZoPR+e5wkxJZVV0ZmfEA6mMtBRsPSpOC8sI9ZYpPWug33kJQdsw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718937172; c=relaxed/simple; bh=TrtFPtQa1k97XzAC7EiU7HM1PNrmylMnQBNN3TI+v6A=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=D1Ht26nTHrabQanIqf50GICzGhA+/jagUnwptKO+jpElIn3/XzGRTPSb352ESzPwYIyLl9eQSyWRb+Qk+hNKRp3VV/TWucebhpJDNDe3aEIg1UjOZE6fEq1H6hApK+tXIoC8IHVtTzakAL5QCO7Q0S/eSVAt+XLStrNM2e/msKQ= 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=AwMGlm/Q; arc=none smtp.client-ip=209.85.210.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="AwMGlm/Q" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-706354409e1so1413987b3a.2; Thu, 20 Jun 2024 19:32:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718937170; x=1719541970; 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=AwMGlm/QaSabm+eVByh6dNVRZnCGZIRYMA3RIkMITjLC7GOs7mMvCUR8Sggcb4oTnl T0lDbCJE7LWXeoIjhost5Gt+zbaU5PBeBsXnFimIdQasoJHbIKGnOElO4obmW+zvEdlB jpae5l1gz2xVxL2E3SSS03ubCygdmCf+rrelZydI7RP5CroVPSIa7wZvy8izH6QJNZ4M bV2keBigldkjYs67RBZB7SXVYqgUngL6+gRob4i/i84omfeHlSt80DRJwKWJ4gLX+9xR uSckTWFvIgtnaoK6WB3Kk3qt5SrqacFJXby2nyuu8ZZjUXqjGVLDnofolpu5rXWJ80SY CbMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718937170; x=1719541970; 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=Gn8ABcPjsNFf+usy2XO/qTVV8Y6KrVPcc1s8ku8AhRMvMMGbN9PTgshUZ6YlciOKUe pv0ArKjS1f8Z5p29UEQJptVlTp+iCxjJ0nVBahAWHGthG+ULd43pJ1Ou6HGDSTFf/OFZ T/AtZ4cMO5ef3+TYz0CGO5PIQ9I01rZZEcuJp1gFoArkbM5wjUS37aeNHhBfY17YGl0J PbN/qEb6zYKdxAmWnUFLQOzcEmPGc4ensX/roSbVPSxfnyglzZZ31gB3W4aMc89iUJbK muVRMK/sy6iCvHVJShT+Uj99XUfox7t4upwALMjp/44hNBSj532s6DO7d5QwpwAQDWpS /Pfg== X-Forwarded-Encrypted: i=1; AJvYcCXUUX8c8aBsXY0RmBQijfrHl5m68VII6PU3yLiNXV6FhqTBx30uYvsI2lenhhfYkEo0/2LX97/WsOrrydh71x0zV/e/LjbLcxajWlxMI6Htdsg699KDD3gFnv+UPQhonVIeBHq0GvV3/Wzhwk+6ZxjUVxhWiag2/e6m1bs2KvjrKpzGgvIf1Nk3//hmPd6nEiNzTpfnjqCoKXYwpEiIBztkxyhVM7YTDWF+MaQEurT20C2MqYSSz67341PyzkF8dMkUwo34UFCQ4ufZXiQkYQlIiDvhIYP9kMsIKSOjuO1vp0v/wAzFSTgtZIvcVz9zi0B2FLhG/A== X-Gm-Message-State: AOJu0YyNz3YDYyL++Vdubaj4CY0qVi/vw2Gsz2j6Xnq6SS8kBuX/gfpB tAy2bhFXBhYrJuDri05Rtyv+WOFSxmjzJEv5GSfwD9tgKliOfdxq X-Google-Smtp-Source: AGHT+IHwLzKazOZk9J+TnuaENF2GrO7T5JApXVTdzZDurk6fW60Sg+sw/brS6M3qBlKISuVvI9yc+A== X-Received: by 2002:a05:6a00:4d91:b0:6e7:20a7:9fc0 with SMTP id d2e1a72fcca58-70629d0d58cmr6849890b3a.34.1718937169948; Thu, 20 Jun 2024 19:32:49 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.24]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706511944d2sm332488b3a.70.2024.06.20.19.32.38 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Jun 2024 19:32:49 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, akpm@linux-foundation.org, 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 , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter Subject: [PATCH v3 11/11] drm: Replace strcpy() with __get_task_comm() Date: Fri, 21 Jun 2024 10:29:59 +0800 Message-Id: <20240621022959.9124-12-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240621022959.9124-1-laoar.shao@gmail.com> References: <20240621022959.9124-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 Cc: Maarten Lankhorst Cc: Maxime Ripard Cc: Thomas Zimmermann Cc: David Airlie Cc: Daniel Vetter Acked-by: 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();