From patchwork Sun Jun 2 02:37: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: 13682608 Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.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 46DAB8493; Sun, 2 Jun 2024 02:38:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717295902; cv=none; b=XbQtc6WuG34S94gI8QMCOuzYKzNPptxi5ZpTS9jxeDaBqBLZcsaRCM/WnoJgfeunxX4WJOMaRPu8eu+Py2veTOrgUwIlNDdLHyram4Fm2BqMRCuLFxc117q9IG89ZeWVwcuF/WCddIGFRcPfZvbD/s9RSnOhUUvJ/5+jQMi6vTQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717295902; c=relaxed/simple; bh=Uc7Edc2gqqzAFg0A6B27qQSCOHHel9nDUozWmg73GOI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OtTSHJA1BM6iZIKtIxq2ID+58ziQANF5x9mBa+RScDdJSw4MhWPBCLNQi6q2m8GB99fv9TdAL7w76rnb5r23+keCDVHn4ASTNwsZJt2BkBPl6WmqlZjYChMjwP3MfFiEbszj9rSGbQBwgMhklm7TsTXP0NHGHFVPTIk9jOS4m94= 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=FPLjG9Kj; arc=none smtp.client-ip=209.85.215.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="FPLjG9Kj" Received: by mail-pg1-f170.google.com with SMTP id 41be03b00d2f7-6c5bcb8e8edso1367912a12.2; Sat, 01 Jun 2024 19:38:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717295900; x=1717900700; 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=eDSXYBnkhoFlTFb/2ZctlhGfFi3vGVZVEbq0hz4eBkQ=; b=FPLjG9KjwQrD390Zrp3k0bmsKxmW6cktr5Ck657ujlOxL9pbihExOSlX2QF4SCFbEL aA8sN3O8Ga27BY35Dpt4pBwvQanZs2ZgKnY37f+GmiiLwY4pTm6vnVdluyKgyiazKjsl kVv9Uq5n8MdAF81s88sLHziqc9uSXjXwaTSKFpsDC1L8otD8gYZe1YpkUVmtjQEi5FRm DqidQ0FqYT5e2xcqLNpMXNa8jsxrpGwE1o51IPamighT0IdVpPAsvDwOdG1XTywoIts4 sjrTHiePQoh8c31ZsVZ3HiJFzbYrlCvEiJgzTX8PgTYkK0nopb5saqPoPHOml6UShfo0 Z7gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717295900; x=1717900700; 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=eDSXYBnkhoFlTFb/2ZctlhGfFi3vGVZVEbq0hz4eBkQ=; b=ZQeDHPiXahueXNYOvIvP9CQfMDsQF7lefOfZhAsufZljkp72g7+65bfwC5ed045IWi JEsjkvSPy5R0EuStqIlzoQE/nVOtAKh2wFgQu2gwi21T7PygEsBlreJ0Y2ijf7G1W654 Hbk0E7qJUojqx82+Cev9p0g6YV+2j3QGCh0b2NhC9IY/ii079i2UXJfwvwSr+B9PeV2M eE7zJVozyuvY1w7G5DjOcFyfidorZhvPGtC+YMepmP/LUUMbSjQ5jjcdq3RSpD5H5btT R+pTxlrnr0ApUkAndezWjjtqniEDhb+5YIDlHfgqz3kFY0vUePlWh0h5ftSbWz+XndaU 4fqg== X-Forwarded-Encrypted: i=1; AJvYcCX/X/fmV5cL6kvcabPvqLyJ9QY4PYqsGXPanizWMjH5dR1PsexchdL+O3Ruvrd1Aa2sWMAPG4srsrUAfm4EGqTQ2+Glr6ypo/0hMzOE5fw75KAGTVZQBMjnNIYIPZQJi6wLt/uFhlR7H476P4KRD1ZCxnJh6SJErX/aptPRUooiReiomf4P9fqLZdUyFZaH9TqnvGb203C17HkwkIJsjcKyyABbReld2RW0JYNnTdhJfY4SrqackDiRQYalH00SeLwnGRhwwy3OEq0gc/wiar8aGvSo/kEvHx5ntRTZog== X-Gm-Message-State: AOJu0YxOyBPi7nMwf44BTSCi3Mb1nVdgeASBs8g4iUvq3GDitGW4bbo5 oIMl5gGTk2rD36fBGN3JyflX4GD21n7+SEj5vaHeTzkTsO1vaBFG X-Google-Smtp-Source: AGHT+IGSkiCT1T8H8uAL0usG+RhmGH5uSZT9xuU+RtiT09NgmrJQRFy+Yk6pxuNFE1Yrq524r9yvYw== X-Received: by 2002:a17:903:30cd:b0:1f4:7d47:b889 with SMTP id d9443c01a7336-1f6370209femr47779675ad.30.1717295900374; Sat, 01 Jun 2024 19:38:20 -0700 (PDT) Received: from localhost.localdomain ([39.144.45.187]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f6323ea21csm39379575ad.202.2024.06.01.19.38.11 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 01 Jun 2024 19:38:19 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: 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, Yafang Shao , Alexander Viro , Christian Brauner , Jan Kara , Eric Biederman , Kees Cook Subject: [PATCH 1/6] fs/exec: Drop task_lock() inside __get_task_comm() Date: Sun, 2 Jun 2024 10:37:49 +0800 Message-Id: <20240602023754.25443-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240602023754.25443-1-laoar.shao@gmail.com> References: <20240602023754.25443-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Quoted from Linus [0]: Since user space can randomly change their names anyway, using locking was always wrong for readers (for writers it probably does make sense to have some lock - although practically speaking nobody cares there either, but at least for a writer some kind of race could have long-term mixed results Suggested-by: Linus Torvalds Link: https://lore.kernel.org/all/CAHk-=wivfrF0_zvf+oj6==Sh=-npJooP8chLPEfaFV0oNYTTBA@mail.gmail.com [0] Signed-off-by: Yafang Shao Cc: Alexander Viro Cc: Christian Brauner Cc: Jan Kara Cc: Eric Biederman Cc: Kees Cook --- fs/exec.c | 7 +++++-- include/linux/sched.h | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index b3c40fbb325f..b43992d35a8a 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1227,12 +1227,15 @@ 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. + */ 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 c75fd46506df..56a927393a38 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1083,7 +1083,7 @@ struct task_struct { * * - normally initialized setup_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 Sun Jun 2 02:37: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: 13682609 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 A7BC44C81; Sun, 2 Jun 2024 02:38:28 +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=1717295910; cv=none; b=ao9kH9JYCe0iNoapbIiQrtEU7ozMHqd8gnKHhmWpQQAlW8j/4KuIDn6hC2++OBYvUSuMUNuhN8ymJiUsInpZTCYkdrGEihCvMhQaDp5OdAVfYNqAiXdsO33wIBu0Zc33s1przFe3StgKAmu7BXTYlckOaKr8qEN2Rj7EN4QrOIY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717295910; c=relaxed/simple; bh=CGWh4S8+r91SSRuLn3E71JJWKgrT+nzupvKqmDjon1Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VXiQ6x6WkYiCP7HacmYX/gJRDbAvSQSsPwkOCq7kyfWlqtNhqvjzAHDAaT5KIiF6cgchyNBIc+oDqpbUKUsTKuVf9JXqrvfOamqSl0K23OlM+n8OX+ZKBpEQ7KgQ1hEzQJYLURltT7/XLBphL5BdoUBuFqHZGWIhzqBTi+0lbm0= 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=Z97RSFnx; 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="Z97RSFnx" Received: by mail-pg1-f173.google.com with SMTP id 41be03b00d2f7-6818e31e5baso2630174a12.1; Sat, 01 Jun 2024 19:38:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717295908; x=1717900708; 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=Y4yHgpWtrekLlyiVlHlJ07YnG43AZ2I7wcs9NGubtp0=; b=Z97RSFnxcD20zapViDGJfi1H6KOSLbTSKGmBazpGUxpYaSWVDtdog759Rkh7SRgrjR zUV/Dwp/7m1787ldPU3JigLzKrCdmiI9i+x9+kwbIIarzjTNkys6zZQLmt1cPvct2Pj/ NrHeJBRmK3Z10rjMsD2eeKy5MN5nFu+54C3qIi0LekOE/AEHC8wRplC9PkYfcgs4Cu+9 MX3xOfTI7FaUZcirXDJXDFZMaFAan2Lg8CF+BfNwF4Z+OQ2DbhP0m4+kVuOM2Fz9ssIR Y475Z/Hzqq4hNxKMXoVDidjK0d3Y3FQTridHMKa0oadVkzvefbjdZDYGxQ25N5mfxp4I Gnjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717295908; x=1717900708; 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=Y4yHgpWtrekLlyiVlHlJ07YnG43AZ2I7wcs9NGubtp0=; b=SCgQQL02R2VFlcNCYiv7kuAjBZlb7IDVs2NUuBtuYloKfP2S+pcGOsmCsAuyZv1G+9 KTGxrhe90/v7FuxEsbSCRnVILVSY4hsdubdFDrD8tRF3Ueue3jBf5lodQNOaswPDPTXL j9I1iV3qBA6FSmX6zr0PNiGVxknwNekbjkbWRSGVVMXGRJoSNI/qwv4ktUCNN/ijKI/M GqjdyupiYQLI9G4h6dFc/U/I+SmapEkh/eGAUCXLMvHJQ792z7+8usiSVgjpKLt6QTpP gnRjXu6k5hN5EW/eT+g4oL10sp30CjkwWXHKBOjIv6mBXVFzdd4jje7QYbb5/zNsATrK Kc+A== X-Forwarded-Encrypted: i=1; AJvYcCXfB/IpMjtpP9lOCemmuwMO0HFlukr0DCJp3bU1ilS1wrdW1yI7jO1UVR6SVUoX2ADBg/RJuQY/v9yh3saQ8UCSJ98mhk52JOY1n+8QHyrJ94FmyftNc6ajoD0P8rnBjlxqHsQ8RT3UGVZNU2IcN8dEMS6b03iIGH04el3AlTC8cBDw6D/f42QkpnC+ciFVrVZ+F+KJ8v43hAwOzfHClE8f2Z7MBpvWNMzmttrwtQhrOzt9RT89RQv9BfVRuqGsXkHEsLyNNteHyMcwWMr9aIoKTXDgvECKgPauYZ89CQ== X-Gm-Message-State: AOJu0YzUUHxH6yAx6GjILICyFvvS0QTYBapJu427O2Mqh54FxGP/NjYn a+3w4Dd6L/63YoRgbU+sA6CSqGESri1ChIpacY7FdyTNpyzyUcxZ X-Google-Smtp-Source: AGHT+IE+a6Agoj8+Gjr8xAx8NUyto1//finy+7nb6g1z3TiLFFz5Bgij41khKDrpJ1UuLaT9z0xZDA== X-Received: by 2002:a17:902:d4cc:b0:1f2:f1bf:cf44 with SMTP id d9443c01a7336-1f636fdebcamr71682625ad.6.1717295907785; Sat, 01 Jun 2024 19:38:27 -0700 (PDT) Received: from localhost.localdomain ([39.144.45.187]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f6323ea21csm39379575ad.202.2024.06.01.19.38.20 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 01 Jun 2024 19:38:27 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: 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, Yafang Shao , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers Subject: [PATCH 2/6] tracing: Replace memcpy() with __get_task_comm() Date: Sun, 2 Jun 2024 10:37:50 +0800 Message-Id: <20240602023754.25443-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240602023754.25443-1-laoar.shao@gmail.com> References: <20240602023754.25443-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Using __get_task_comm() to read the task comm ensures that the name is always NUL-terminated, regardless of the source string. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Cc: Steven Rostedt Cc: Masami Hiramatsu Cc: Mathieu Desnoyers --- include/linux/tracepoint.h | 4 ++-- include/trace/events/block.h | 10 +++++----- include/trace/events/oom.h | 2 +- include/trace/events/osnoise.h | 2 +- include/trace/events/sched.h | 27 ++++++++++++++------------- include/trace/events/signal.h | 2 +- include/trace/events/task.h | 4 ++-- 7 files changed, 26 insertions(+), 25 deletions(-) diff --git a/include/linux/tracepoint.h b/include/linux/tracepoint.h index 689b6d71590e..6381824d8107 100644 --- a/include/linux/tracepoint.h +++ b/include/linux/tracepoint.h @@ -519,10 +519,10 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p) * * * * TP_fast_assign( - * memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN); + * __get_task_comm(__entry->next_comm, TASK_COMM_LEN, next); * __entry->prev_pid = prev->pid; * __entry->prev_prio = prev->prio; - * memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN); + * __get_task_comm(__entry->prev_comm, TASK_COMM_LEN, prev); * __entry->next_pid = next->pid; * __entry->next_prio = next->prio; * ), diff --git a/include/trace/events/block.h b/include/trace/events/block.h index 0e128ad51460..6f8c5d0014e6 100644 --- a/include/trace/events/block.h +++ b/include/trace/events/block.h @@ -193,7 +193,7 @@ DECLARE_EVENT_CLASS(block_rq, blk_fill_rwbs(__entry->rwbs, rq->cmd_flags); __get_str(cmd)[0] = '\0'; - memcpy(__entry->comm, current->comm, TASK_COMM_LEN); + __get_task_comm(__entry->comm, TASK_COMM_LEN, current); ), TP_printk("%d,%d %s %u (%s) %llu + %u [%s]", @@ -328,7 +328,7 @@ DECLARE_EVENT_CLASS(block_bio, __entry->sector = bio->bi_iter.bi_sector; __entry->nr_sector = bio_sectors(bio); blk_fill_rwbs(__entry->rwbs, bio->bi_opf); - memcpy(__entry->comm, current->comm, TASK_COMM_LEN); + __get_task_comm(__entry->comm, TASK_COMM_LEN, current); ), TP_printk("%d,%d %s %llu + %u [%s]", @@ -415,7 +415,7 @@ TRACE_EVENT(block_plug, ), TP_fast_assign( - memcpy(__entry->comm, current->comm, TASK_COMM_LEN); + __get_task_comm(__entry->comm, TASK_COMM_LEN, current); ), TP_printk("[%s]", __entry->comm) @@ -434,7 +434,7 @@ DECLARE_EVENT_CLASS(block_unplug, TP_fast_assign( __entry->nr_rq = depth; - memcpy(__entry->comm, current->comm, TASK_COMM_LEN); + __get_task_comm(__entry->comm, TASK_COMM_LEN, current); ), TP_printk("[%s] %d", __entry->comm, __entry->nr_rq) @@ -485,7 +485,7 @@ TRACE_EVENT(block_split, __entry->sector = bio->bi_iter.bi_sector; __entry->new_sector = new_sector; blk_fill_rwbs(__entry->rwbs, bio->bi_opf); - memcpy(__entry->comm, current->comm, TASK_COMM_LEN); + __get_task_comm(__entry->comm, TASK_COMM_LEN, current); ), TP_printk("%d,%d %s %llu / %llu [%s]", diff --git a/include/trace/events/oom.h b/include/trace/events/oom.h index b799f3bcba82..f29be9ebcd4d 100644 --- a/include/trace/events/oom.h +++ b/include/trace/events/oom.h @@ -23,7 +23,7 @@ TRACE_EVENT(oom_score_adj_update, TP_fast_assign( __entry->pid = task->pid; - memcpy(__entry->comm, task->comm, TASK_COMM_LEN); + __get_task_comm(__entry->comm, TASK_COMM_LEN, task); __entry->oom_score_adj = task->signal->oom_score_adj; ), diff --git a/include/trace/events/osnoise.h b/include/trace/events/osnoise.h index 82f741ec0f57..50f480655722 100644 --- a/include/trace/events/osnoise.h +++ b/include/trace/events/osnoise.h @@ -20,7 +20,7 @@ TRACE_EVENT(thread_noise, ), TP_fast_assign( - memcpy(__entry->comm, t->comm, TASK_COMM_LEN); + __get_task_comm(__entry->comm, TASK_COMM_LEN, t); __entry->pid = t->pid; __entry->start = start; __entry->duration = duration; diff --git a/include/trace/events/sched.h b/include/trace/events/sched.h index 68973f650c26..2a9d7c62c58a 100644 --- a/include/trace/events/sched.h +++ b/include/trace/events/sched.h @@ -9,6 +9,7 @@ #include #include #include +#include /* * Tracepoint for calling kthread_stop, performed to end a kthread: @@ -25,7 +26,7 @@ TRACE_EVENT(sched_kthread_stop, ), TP_fast_assign( - memcpy(__entry->comm, t->comm, TASK_COMM_LEN); + __get_task_comm(__entry->comm, TASK_COMM_LEN, t); __entry->pid = t->pid; ), @@ -152,7 +153,7 @@ DECLARE_EVENT_CLASS(sched_wakeup_template, ), TP_fast_assign( - memcpy(__entry->comm, p->comm, TASK_COMM_LEN); + __get_task_comm(__entry->comm, TASK_COMM_LEN, p); __entry->pid = p->pid; __entry->prio = p->prio; /* XXX SCHED_DEADLINE */ __entry->target_cpu = task_cpu(p); @@ -239,11 +240,11 @@ TRACE_EVENT(sched_switch, ), TP_fast_assign( - memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN); + __get_task_comm(__entry->next_comm, TASK_COMM_LEN, next); __entry->prev_pid = prev->pid; __entry->prev_prio = prev->prio; __entry->prev_state = __trace_sched_switch_state(preempt, prev_state, prev); - memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN); + __get_task_comm(__entry->prev_comm, TASK_COMM_LEN, prev); __entry->next_pid = next->pid; __entry->next_prio = next->prio; /* XXX SCHED_DEADLINE */ @@ -286,7 +287,7 @@ TRACE_EVENT(sched_migrate_task, ), TP_fast_assign( - memcpy(__entry->comm, p->comm, TASK_COMM_LEN); + __get_task_comm(__entry->comm, TASK_COMM_LEN, p); __entry->pid = p->pid; __entry->prio = p->prio; /* XXX SCHED_DEADLINE */ __entry->orig_cpu = task_cpu(p); @@ -311,7 +312,7 @@ DECLARE_EVENT_CLASS(sched_process_template, ), TP_fast_assign( - memcpy(__entry->comm, p->comm, TASK_COMM_LEN); + __get_task_comm(__entry->comm, TASK_COMM_LEN, p); __entry->pid = p->pid; __entry->prio = p->prio; /* XXX SCHED_DEADLINE */ ), @@ -357,7 +358,7 @@ TRACE_EVENT(sched_process_wait, ), TP_fast_assign( - memcpy(__entry->comm, current->comm, TASK_COMM_LEN); + __get_task_comm(__entry->comm, TASK_COMM_LEN, current); __entry->pid = pid_nr(pid); __entry->prio = current->prio; /* XXX SCHED_DEADLINE */ ), @@ -383,9 +384,9 @@ TRACE_EVENT(sched_process_fork, ), TP_fast_assign( - memcpy(__entry->parent_comm, parent->comm, TASK_COMM_LEN); + __get_task_comm(__entry->parent_comm, TASK_COMM_LEN, parent); __entry->parent_pid = parent->pid; - memcpy(__entry->child_comm, child->comm, TASK_COMM_LEN); + __get_task_comm(__entry->child_comm, TASK_COMM_LEN, child); __entry->child_pid = child->pid; ), @@ -481,7 +482,7 @@ DECLARE_EVENT_CLASS_SCHEDSTAT(sched_stat_template, ), TP_fast_assign( - memcpy(__entry->comm, tsk->comm, TASK_COMM_LEN); + __get_task_comm(__entry->comm, TASK_COMM_LEN, tsk); __entry->pid = tsk->pid; __entry->delay = delay; ), @@ -539,7 +540,7 @@ DECLARE_EVENT_CLASS(sched_stat_runtime, ), TP_fast_assign( - memcpy(__entry->comm, tsk->comm, TASK_COMM_LEN); + __get_task_comm(__entry->comm, TASK_COMM_LEN, tsk); __entry->pid = tsk->pid; __entry->runtime = runtime; ), @@ -571,7 +572,7 @@ TRACE_EVENT(sched_pi_setprio, ), TP_fast_assign( - memcpy(__entry->comm, tsk->comm, TASK_COMM_LEN); + __get_task_comm(__entry->comm, TASK_COMM_LEN, tsk); __entry->pid = tsk->pid; __entry->oldprio = tsk->prio; __entry->newprio = pi_task ? @@ -596,7 +597,7 @@ TRACE_EVENT(sched_process_hang, ), TP_fast_assign( - memcpy(__entry->comm, tsk->comm, TASK_COMM_LEN); + __get_task_comm(__entry->comm, TASK_COMM_LEN, tsk); __entry->pid = tsk->pid; ), diff --git a/include/trace/events/signal.h b/include/trace/events/signal.h index 1db7e4b07c01..8f317a265392 100644 --- a/include/trace/events/signal.h +++ b/include/trace/events/signal.h @@ -67,7 +67,7 @@ TRACE_EVENT(signal_generate, TP_fast_assign( __entry->sig = sig; TP_STORE_SIGINFO(__entry, info); - memcpy(__entry->comm, task->comm, TASK_COMM_LEN); + __get_task_comm(__entry->comm, TASK_COMM_LEN, task); __entry->pid = task->pid; __entry->group = group; __entry->result = result; diff --git a/include/trace/events/task.h b/include/trace/events/task.h index 47b527464d1a..77c14707460e 100644 --- a/include/trace/events/task.h +++ b/include/trace/events/task.h @@ -21,7 +21,7 @@ TRACE_EVENT(task_newtask, TP_fast_assign( __entry->pid = task->pid; - memcpy(__entry->comm, task->comm, TASK_COMM_LEN); + __get_task_comm(__entry->comm, TASK_COMM_LEN, task); __entry->clone_flags = clone_flags; __entry->oom_score_adj = task->signal->oom_score_adj; ), @@ -46,7 +46,7 @@ TRACE_EVENT(task_rename, TP_fast_assign( __entry->pid = task->pid; - memcpy(entry->oldcomm, task->comm, TASK_COMM_LEN); + __get_task_comm(entry->oldcomm, TASK_COMM_LEN, task); strscpy(entry->newcomm, comm, TASK_COMM_LEN); __entry->oom_score_adj = task->signal->oom_score_adj; ), From patchwork Sun Jun 2 02:37: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: 13682610 Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) (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 695B44C8C; Sun, 2 Jun 2024 02:38:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717295917; cv=none; b=VqqMy0a4C5akdZKxP6+6+yPf/QhOtwQrNYHmLG47JuCujb5uGXqZiYwwl3R3aLHu+scIMpVs29uO4IsplmbPHnC5DHivyS3L6PSVwJ11FEpaIAuowXWEqIjXQml9wbsahnsxkXWQ6AGa3aAu6WVNMVKCCD+l0WDyF576rqI+W3A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717295917; c=relaxed/simple; bh=3RtLwIqj8y9FKL3we3+yUHHdPufkZZtSuz+OWtIZu5g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=u/mWrgSQuB6yK8FEiGWX6QJiErkuMHHn08yDN/OMwisfFwzk/VBIGNaDBFsLhOhzZ7aj3Uv/+LP6Ma8WyQSePWV7K1N2xDKhmWV2/E1t2FoMeC5oxixjBeowVV+jR99XD/sF9bYOp+JgQRnKmM3UosT08e4pd5mbvyd12m7W8Jc= 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=IpIKMHHX; arc=none smtp.client-ip=209.85.215.181 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="IpIKMHHX" Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-6c9d747be82so459127a12.3; Sat, 01 Jun 2024 19:38:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717295916; x=1717900716; 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=86BLVNhH4ic1Ff0npcvsPGdGMp4TM/B2Y17eUoaRoKo=; b=IpIKMHHXlk40M6HM7zVnEYf1vdIIJ0TDmjod7p+CQKUlfrtE/2t9tF10UynNhKuE60 BchTP+I+NfC2DODCxZf5Z06TBKHJ3bjJoiU2AJL7ZqZNWVBS37iwfO0HWB39tfG1Hv5b CQt1het7uaMps0gAGAm0V+ULmhn0MAXBb/NKgIkDamfmdw7wontDyZ98Q2WCoBCQM98e j55/fEJaV9oaqrqsOmyRrGeYx/wTyMfoXkRNiff+oancz4ENQTcrAOpBLjo41pLUFYex t9HSsdBDgHPsPOFcsvsw6W6a61zp0IMlCo/mBSwlZU04Tk6e12mUgQhgoAKbHn0lKvs1 zFAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717295916; x=1717900716; 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=86BLVNhH4ic1Ff0npcvsPGdGMp4TM/B2Y17eUoaRoKo=; b=jb7FUV9KPKzA2j8vkqeFMD1oDUeMeiabPIUzHwXoSOZYjv0EM/g7TFcT6gy6k8s+gb jWKYjp7UF/Y64hya/zONw4pjj9074p4WqDV3obxveOqPcz6axNP5dFS3Q3BqTHfpjwUl 0mjBtT1Ya0vQbPOhHkNNUBeKNP2KBPcUp95y4i+LcRXDe39OGBT+lg+1y5D5rIOLIzFB kNstN1FcYZ3iE1OXs2CRigGIYpr6dZNDfg4h2E7m60e+RH8Tr6R1PGRCiSHoPYRgIa+3 DJVrjqB1e5m0vhWvqk9+2s9hSvyeQCzE5KhI6Dj2Nf/ZIOwa8MGjoCHBHdMristkDPMp clTA== X-Forwarded-Encrypted: i=1; AJvYcCUIypwU72By1fR2oe9bjITC4GTD2/QGZ+C8Zi8mtS0hvAJQcCWFSx86Az3874N2R81k4JtOl2qWtRsVOhPPy7DFgWkESZx/HuYXNQQZvBLocbBsgeQMgVYgj8mv2ZFwjT9ZGrqKH5dtowm/bOSKS3FJjgyPfwunWZlL21g0I25d/53f23+jhjLXAaci1JUB1g85uyqoIs6mAYULTiY8SyWxWizEca4GQT9/UgH50Z/Wu6Yf6pV1jLQ4lgPo3ODbT9lc1cmDDWgWUXFdT442h6USXAvi88mGZdgMLdatxg== X-Gm-Message-State: AOJu0Yz+Z1MzGn0gkSyVTAGjKOGz5pIchTtI+x3nSJiA4GqfE9ixSe7n +87ivFb8HQC3WQbAq/yHYnvSn386kmyl7+pyrJRLByu32IaJSW/b X-Google-Smtp-Source: AGHT+IGNFv/boPhAUu2B0W3t33lg4GlGND9qco4AaBPr3IkdnpkGlhsaTOMWXxep7jl0e5AswLTzAA== X-Received: by 2002:a17:902:c946:b0:1f6:6946:ef72 with SMTP id d9443c01a7336-1f66946f176mr13505155ad.36.1717295915514; Sat, 01 Jun 2024 19:38:35 -0700 (PDT) Received: from localhost.localdomain ([39.144.45.187]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f6323ea21csm39379575ad.202.2024.06.01.19.38.28 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 01 Jun 2024 19:38:35 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: 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, Yafang Shao , Paul Moore , Eric Paris Subject: [PATCH 3/6] auditsc: Replace memcpy() with __get_task_comm() Date: Sun, 2 Jun 2024 10:37:51 +0800 Message-Id: <20240602023754.25443-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240602023754.25443-1-laoar.shao@gmail.com> References: <20240602023754.25443-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Using __get_task_comm() to read the task comm ensures that the name is always NUL-terminated, regardless of the source string. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Cc: Paul Moore Cc: Eric Paris Acked-by: Paul Moore --- 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 Sun Jun 2 02:37: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: 13682611 Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A1AE33207; Sun, 2 Jun 2024 02:38:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717295927; cv=none; b=GMsP0AlRPMZiwIgrHlbbm50z09XjHuAn5vQfXS/4B9J46+dVYwH/MOFEnLi/wJJ1hQLO0+YqB/PoWO/vhorqFBy4BPyU3WF9/C48z4SZkDUFnhIHRQUAIw0Q2KPEQU3Z/V4WsPeWST3unZu44BqdWM1cJisgOjSs2LFANPviymo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717295927; c=relaxed/simple; bh=BKOmOHopmzAxUYtSgNtlju0xV38CETbRh7m8PHKcY1E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=PbiRgEClESwfhu+J63riqej2I7HAjykOgpy2Oj6DpyfnbAcvxlEzMcyG3r//gOKJ9LaJwwPslqm/tsDpJD6Ad7ZyuayA2Zp1LCMT+C2cI1S2MCTq3xqTB0LdjKiWvQaei6irEyMiaQbGT0Lfjk9gzzFW666rx/doNTsZstrR0rY= 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=H3zLOiTs; arc=none smtp.client-ip=209.85.215.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="H3zLOiTs" Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-6bfd4b88608so2460944a12.1; Sat, 01 Jun 2024 19:38:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717295925; x=1717900725; 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=ZwXgac49vNiqQKiAaPBrbZsSURsGJANu7JDjc/AUCv8=; b=H3zLOiTs98x+2/cfYGhcidlyOj01Vx9POSenI6XLg7JU0gXvdmVGkayixnrA26tXQS MozY8kMDdY3kM9VwJR/9sIWDQM+nCNgVL1mUChAyDg+mSj+KCiAK0qq8sNsAdsnvnrst CpEgWk/TKOEFMlXgozX4kYQ0BCpK1Ntbxz7sD3Eo+EtZ5S/ttOSUpATSSU/uHr/RuWVg hBLxN1bBQDQeiHe9Gq+UDz2DgvJ6SHxZ+TKNn5tLOXsC5xJLL4jUdThuloz8cGcY30za n3RThRxExJ2TPqWYUCPb8IJ7197EynTVzUS2lLePTB6qy0egTFPHQO/ItV0oct3D1uWp ehCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717295925; x=1717900725; 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=ZwXgac49vNiqQKiAaPBrbZsSURsGJANu7JDjc/AUCv8=; b=e0DF0wau9ehTfB8Myy+xnjXwAZIfaZPXtXZnciwcJYXJ6AxpvB4HmhMQJc9tuVPzvh JAtfiaUlhi520kBXvlbjyfzGgOzzMTfwyEYcleF8Aq4rmCgiuAZC6/g7NsbYMjpsyPDO UGXdduD//jtMyA0jmhM2j/O9oUQcR/HRWS6CmF8roDlJPOLuVb+EoxY7V5Nz9XCtypqc sfekE5tK3/5Ws7Ma6lCaeS2xhRQB8BDQIwUj9KYnB1j7oTet8mKjs16giDBOrvrsMGbc 4Fm14MMB7Afojx5wiRu1XpvhICCh93cBVN+VpttIIf2ne1hLBL0WpBkCbywRpwrG0qsU +O/g== X-Forwarded-Encrypted: i=1; AJvYcCV3lYd+wo8pSqQy4dYbj+rWJF8spBg81D9ZZCOlIqF647Zcw6d+0Xla6YoufnwcMZduz9vpNBtPkrLyXPOUw65Nw0WoG8vbu6b1XuQiZevDw1jeFIt6yINrfDp4AADTaG/8GqOZgZYLvvp3Xbveh4psuMCDlHJPEULBpzXlGA7IanhQ+obymkZXkURrstd8VnVmFgL5maVrAmysHw09Wm9kPrRs9bviqy9q7nlhIeIpgF2KnKhjNa+kva+jrrK+AfDVURRVMSqlTYd4dO9FA5YJ63Oh/JIS13RrLTtJHQ== X-Gm-Message-State: AOJu0YzKWVMw+RglxPO9966QgjUxiz1G80xJWK938GI7/w4wA66tUGhM qCv/JCyTsiaTqE8ccM9MCqgcJQdpBvDnXkeE0S02b3u83fGZZqCl4D2IJyu2 X-Google-Smtp-Source: AGHT+IGxdz166Qj5hr7gPenbJxzI2r9mPKQqOG9IRmdP4Ad2EqUcAIvbF35C52RxuyHIZjzO8B2P0g== X-Received: by 2002:a17:902:7842:b0:1e6:7700:1698 with SMTP id d9443c01a7336-1f63706bba8mr56224725ad.35.1717295924813; Sat, 01 Jun 2024 19:38:44 -0700 (PDT) Received: from localhost.localdomain ([39.144.45.187]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f6323ea21csm39379575ad.202.2024.06.01.19.38.35 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 01 Jun 2024 19:38:44 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: 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, Yafang Shao , Paul Moore , James Morris , "Serge E. Hallyn" , Stephen Smalley , Ondrej Mosnacek Subject: [PATCH 4/6] security: Replace memcpy() with __get_task_comm() Date: Sun, 2 Jun 2024 10:37:52 +0800 Message-Id: <20240602023754.25443-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240602023754.25443-1-laoar.shao@gmail.com> References: <20240602023754.25443-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Quoted from Linus [0]: selinux never wanted a lock, and never wanted any kind of *consistent* result, it just wanted a *stable* result. Using __get_task_comm() to read the task comm ensures that the name is always NUL-terminated, regardless of the source string. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao LINK: https://lore.kernel.org/all/CAHk-=wivfrF0_zvf+oj6==Sh=-npJooP8chLPEfaFV0oNYTTBA@mail.gmail.com/ [0] Cc: Paul Moore Cc: James Morris Cc: "Serge E. Hallyn" Cc: Stephen Smalley Cc: Ondrej Mosnacek Acked-by: Paul Moore --- 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 Sun Jun 2 02:37: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: 13682612 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.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 B63A04C8D; Sun, 2 Jun 2024 02:38:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717295937; cv=none; b=smm7081fZM92+0f7C11oOtHAQFw/aNmOfgNgLWqu33jrGgOdEmPfWnuBKlTBj3f/c/0LqsvhcQRh6K/nUgm3rkuHAlZKKmFHV6k8Y5toxLJxh++YHuI/28D1S7yWITdnZLCHadYBUhK03y9/HTFeogG+iKPt83MqmUBFm5koG8U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717295937; c=relaxed/simple; bh=o8548Y6NN5BDp3sW7tIbS607pyx6X2zs9ez6UFvk858=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uD0U5bLZs5IYC0tEuzeVTdocN10WDZ/ejfYrzIjlil6uLjursrczWD5XrnDG67AwpCEgQQYXPxmCGFKYDa6sQIXAgf/DdH0K7TUOyJDQo0NVhgBoWw+Abz8bGFf8ipybQ2h1sn9ogkQqHeQbOQLSPHPtKgNI4bL1GjFjfcV/xZU= 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=iF/vtP6r; arc=none smtp.client-ip=209.85.214.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="iF/vtP6r" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1f44b5b9de6so17110685ad.3; Sat, 01 Jun 2024 19:38:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717295935; x=1717900735; 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=3TpGu6Oq1vjxccb8W6xWjhiTwznYEKWGAfAIwahkDiA=; b=iF/vtP6rB/nmhygQq4v+17TtLCFCWv14a0SYMLL6wwTQpZ6RUwIRD2I6Sa1igFO2T1 2wH//UvOR9LCyU5VJ8zgCCgEATK/awDC9ZXYqCOpb+Rg56st6YeoOFl3xYEi5+xh5Pbv UubxbhAH7xziATz+QihQLUBeoVhUNvbM6MW1Sziop0zHNe1SaSPTIqjlmRa+JepgQSXR xJVZDuwBOV/vRS+DPAmBIQeInk1XemuXcp3DKyb+f2CnosNHxwNg+EZ3Gw72Oro3WVmi Ui5rmy1TL7KNMkBuhoDnNDOKUrMsS0T2M2fSEyqim9hlrfTIJtM20Ci6u4DuejCTPFTA 7nmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717295935; x=1717900735; 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=3TpGu6Oq1vjxccb8W6xWjhiTwznYEKWGAfAIwahkDiA=; b=for2osDtBDbIBgsh5m6szJ2oqWO1wnJ2isGmiTr8gaPMGOUs67Q6GEn3WuuxT4C6ym ypvMZ3qjnSY7paIeiV0uVQZOV5aDuwjdFeZXjKB0aRhU5JQfebMQBVrbn38mHbFlp47N iEEaKGJgWxd9/2nWiRa6tdKEKvPFl3MGlKEE7L/9+RopgMekZZHmYWpe9vArk3cpLz5H xMaYJTpNPpdFLkYWYLpcuImFziCgR1hpOOeK+H8D+bXei0dbpjEaL0vxrgIYnxI9izg0 wslV1EfcSuhBKOucSkB39PM3Oy6ho8tFStLZgnis93ufUdywPrHXAEbvJUqjf8Y2zdlA VESg== X-Forwarded-Encrypted: i=1; AJvYcCUy1I5QGcYisMl0eB747E6m10fKPpXE77xNmPjwEBy1U8HYNrR6GGwqD/E1h0s0oyXcdw88BecbbI40HF5XXcxtZWGuZ26EJgP/WcivL8WwVNuTq0jGgO7GUWmOez3Qs34U7E/a47rpOBPnMp93A/HdNTvSkD2D1DFJGIiHxpNxDGt96R2xARX3SiYqEm1DWxaxRV2lnvw0ed0o52zdn6g+HAdINMpLcWXZFaoinf5ZCQMVVW9xUY55PIXGHGbuFhAHLFwDoVeLHZ3QlkI7j/j6v1l1UHInqh1W1TQvCA== X-Gm-Message-State: AOJu0YxwtZbbueYFwIvpzY7/KDpkATEMrGPV6jJ7ynjjkyOuyNpfcAyV BGZeHN/FeA3E+1n914JaOiyrOKhvQmUxbdNEBQNP0ywCeMhwNAO1 X-Google-Smtp-Source: AGHT+IGxvOn5+cLL6IQR7JJZSFxqN/pAIswbvkNRxUdWw/EDTeYezEeBw1uxYC432gXEUnzhdS2+uQ== X-Received: by 2002:a17:902:a38d:b0:1f3:903:5c9a with SMTP id d9443c01a7336-1f6370aa056mr64384745ad.58.1717295934923; Sat, 01 Jun 2024 19:38:54 -0700 (PDT) Received: from localhost.localdomain ([39.144.45.187]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f6323ea21csm39379575ad.202.2024.06.01.19.38.45 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 01 Jun 2024 19:38:54 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: 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, Yafang Shao , Quentin Monnet Subject: [PATCH 5/6] bpftool: Make task comm always be NUL-terminated Date: Sun, 2 Jun 2024 10:37:53 +0800 Message-Id: <20240602023754.25443-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240602023754.25443-1-laoar.shao@gmail.com> References: <20240602023754.25443-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Let's explicitly ensure the destination string is NUL-terminated. This way, it won't be affected by changes to the source string. Signed-off-by: Yafang Shao Cc: Quentin Monnet 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 Sun Jun 2 02:46: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: 13682644 Received: from mail-ot1-f41.google.com (mail-ot1-f41.google.com [209.85.210.41]) (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 C08511859; Sun, 2 Jun 2024 02:47:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717296439; cv=none; b=ZIT8vHVGHi1UeboLtSA7UFAJvsglPSMC7IMUiJdWhINswhu2aHIdYk5MawARVlF521ENcywam3iWQG5Vv7yXHfQSnpd+zB3wlozD91WCkPbVKhzHLj3stt6FCxdna2B49Q1gNSU3Gb38EDlIi2CszhSwNjHjQvDFiEKeY3LJ/F4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717296439; c=relaxed/simple; bh=c88VhkTo2sPOlFRrPrweVXH7AtkGnW5PCWNhKi/l810=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JflzSeNzv1gCwu3QLQrUK7IhYC/G8gCDse/Du+rBfN4l3lvQfXQX15ucA2Y9qBCqZDV8dMaKqaAQB2QPNaBaTgTnMewbQyvqCwK0/p6gTDUBwAstUrowY40jtzKWJM7LTF1pwS4hqErBfTXQbFSH3m6fnMr58B+sO8sZ0Ixj2ic= 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=ItFYeV9i; arc=none smtp.client-ip=209.85.210.41 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="ItFYeV9i" Received: by mail-ot1-f41.google.com with SMTP id 46e09a7af769-6f8d2ec8652so1988066a34.3; Sat, 01 Jun 2024 19:47:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717296437; x=1717901237; 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=bu3o/1wkpiEjxw3AW0N2UpaOJE3SQdUz6rVg/pLPlFs=; b=ItFYeV9ieaE51n7dzNZlB6kO4BqgAQKRiL/8br0Y62A6z6DfUA+fRhpcRDQLsUX3vU qpYYs8Cy64JdtA5wulLNM0aradWFlG/v6L06xB4/4Utnrq1ecZ4qgRooXEl0I2kghZXM 3xrTE7yWzAq4TfsjsDn+JP2vemz6LF5YugPbiS7Aznz1uaQepsJ+ly0Tn1wldWnoVSxT Mk2JnrUp0hmtHNY8emGIO+wPyIoK39ki8FSy3bhnnxGeyH6WZjCbOtGOMVr5uT4qibRs ztARHSmX9TNz5MYAFhbREhXDHpDXl68ZABIh3vDf6d1Kw+7xnvJK88Aj/kwcdH0msYbO 4gig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717296437; x=1717901237; 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=bu3o/1wkpiEjxw3AW0N2UpaOJE3SQdUz6rVg/pLPlFs=; b=WWAW4lXZ0XDtBw/hMdv2H8G80gMmR0LR3sOaGxMnD7KGJnPxJVA80CVDY/didzCliW rYW4e9SFrkmgdEWPiEW7fP9b4BS0jdORgjdZYVHSevww2ZMbdny8XEHfLlbYh2Oy8xNL Ff3JMtAIbEBuCYexh2mEQgo22xiqN+RkZukHm2PYu/qkMWR+IX3Xb9Y87GHUODJg7Z1W Usqb24ZolUhU24oNZFeIRVi2xu37qAgNyCvprg78yg/faCuIgQr075D/7K7aQMqhz+CV HElS9FCWbYCKW9wRWKTnD8icqYMXkyn4iV5qgokhjUWAuLqjCOfDbKrSZWRFS5IFlJnO NtkA== X-Forwarded-Encrypted: i=1; AJvYcCVriXAF4w3RiaVCNBR6yIDqxHc3RDau5/NyCVkKXTmGQZ4vcvQdAawZV9Gl4xJo8aIS50GV5/Q9DdAa9Qq3c1Gl1SMJ3+wUwOcqWhrtYuIdlSYRbC5ts5iWuUEwgLNjPzbC1dMm+9s/8db5ScxUlP/cdpU4nyzEYkIu9s8yqD3YwxPTb8AQCT5nmjbrxeuPPCFLk1/7bOtFj9SrzCu38hM+sgD4cLWqIkEPRlOClwIZoG50OEV847FLeP/QjobxUqAzA/GglmNWc2XHvDJkj1aXVSmKYlOnIEPW04gS0A== X-Gm-Message-State: AOJu0YzYLVQl6VaUK1BeFk55sZSGBDauDybmzq3aoODxoRO/D4+luS32 9nsFFN7SwzRkbmiS7Dy8IypZUG+8vgz6CrBn+yq0z/CBHw5i9B0/ X-Google-Smtp-Source: AGHT+IFc6pB9c2/xDQu9bENUVRyBXhBelq6v1sdhKRdECc7iHQz2zEq+tFq0IQc1xoKSQZnnKrMN/w== X-Received: by 2002:a9d:6c07:0:b0:6f9:91c:f275 with SMTP id 46e09a7af769-6f911fba0c7mr5694846a34.38.1717296436688; Sat, 01 Jun 2024 19:47:16 -0700 (PDT) Received: from localhost.localdomain ([39.144.45.187]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-6c353f0aef3sm3289959a12.10.2024.06.01.19.47.10 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 01 Jun 2024 19:47:16 -0700 (PDT) From: Yafang Shao To: laoar.shao@gmail.com, torvalds@linux-foundation.org Cc: 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, Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko Subject: [PATCH 6/6] selftests/bpf: Replace memcpy() with __get_task_comm() Date: Sun, 2 Jun 2024 10:46:58 +0800 Message-Id: <20240602024658.25922-1-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240602023754.25443-1-laoar.shao@gmail.com> References: <20240602023754.25443-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Using __get_task_comm() to read the task comm ensures that the name is always NUL-terminated, regardless of the source string. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Cc: Andrii Nakryiko Cc: Eduard Zingerman Cc: Mykola Lysenko --- tools/testing/selftests/bpf/bpf_testmod/bpf_testmod-events.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod-events.h b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod-events.h index 11ee801e75e7..e5df95b56c53 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod-events.h +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod-events.h @@ -20,7 +20,7 @@ TRACE_EVENT(bpf_testmod_test_read, ), TP_fast_assign( __entry->pid = task->pid; - memcpy(__entry->comm, task->comm, TASK_COMM_LEN); + __get_task_comm(__entry->comm, TASK_COMM_LEN, task); __entry->off = ctx->off; __entry->len = ctx->len; ),