linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Richard Guy Briggs <rgb@redhat.com>
To: linux-audit@redhat.com, linux-kernel@vger.kernel.org
Cc: Richard Guy Briggs <rgb@redhat.com>,
	Eric Paris <eparis@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Oleg Nesterov <oleg@redhat.com>
Subject: [PATCH] pid: change task_struct::pid to read-only
Date: Mon, 16 Dec 2013 16:03:38 -0500	[thread overview]
Message-ID: <ae3d74e79649d9e4962d0da030fb4852ddc5b8d1.1387227114.git.rgb@redhat.com> (raw)
In-Reply-To: <8aa73d2b884439496f87d5f34c12ba9b4b40f7e5.1377032086.git.rgb@redhat.com>

task->pid is only ever assigned once (well ok, twice).  For system health and
secure logging confidence, make it const to make it much more intentional when
it is being changed.
---

Peter, as you had suggested, does this approach work for you in terms of making
task_struct::pid a lot more difficult to accidentally change to try to preserve
its integrity?

Is the use of memcpy() significantly different from *p = *q ?


 arch/x86/kernel/process.c |    2 +-
 fs/exec.c                 |    4 +++-
 include/linux/sched.h     |    2 +-
 kernel/fork.c             |    8 ++++++--
 4 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c
index c83516b..4170026 100644
--- a/arch/x86/kernel/process.c
+++ b/arch/x86/kernel/process.c
@@ -66,7 +66,7 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
 {
 	int ret;
 
-	*dst = *src;
+	memcpy(dst, src, sizeof(struct task_struct));
 	if (fpu_allocated(&src->thread.fpu)) {
 		memset(&dst->thread.fpu, 0, sizeof(dst->thread.fpu));
 		ret = fpu_alloc(&dst->thread.fpu);
diff --git a/fs/exec.c b/fs/exec.c
index 47d7edb..c8d0159 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -908,6 +908,8 @@ static int de_thread(struct task_struct *tsk)
 	 */
 	if (!thread_group_leader(tsk)) {
 		struct task_struct *leader = tsk->group_leader;
+		/* tast_struct::pid is const pid_t, hence the ugly cast */
+		pid_t *pid_p = (pid_t*)&(tsk->pid);
 
 		sig->notify_count = -1;	/* for exit_notify() */
 		for (;;) {
@@ -950,7 +952,7 @@ static int de_thread(struct task_struct *tsk)
 		 * Note: The old leader also uses this pid until release_task
 		 *       is called.  Odd but simple and correct.
 		 */
-		tsk->pid = leader->pid;
+		*pid_p = leader->pid;
 		change_pid(tsk, PIDTYPE_PID, task_pid(leader));
 		transfer_pid(leader, tsk, PIDTYPE_PGID);
 		transfer_pid(leader, tsk, PIDTYPE_SID);
diff --git a/include/linux/sched.h b/include/linux/sched.h
index d9ada71..45069c0 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1112,7 +1112,7 @@ struct task_struct {
 	unsigned sched_reset_on_fork:1;
 	unsigned sched_contributes_to_load:1;
 
-	pid_t pid;
+	const pid_t pid;
 	pid_t tgid;
 
 #ifdef CONFIG_CC_STACKPROTECTOR
diff --git a/kernel/fork.c b/kernel/fork.c
index 086fe73..ec0683d 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -286,7 +286,7 @@ void __init fork_init(unsigned long mempages)
 int __attribute__((weak)) arch_dup_task_struct(struct task_struct *dst,
 					       struct task_struct *src)
 {
-	*dst = *src;
+	memcpy(dst, src, sizeof(struct task_struct));
 	return 0;
 }
 
@@ -1137,6 +1137,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 {
 	int retval;
 	struct task_struct *p;
+	pid_t *pid_p;
 
 	if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
 		return ERR_PTR(-EINVAL);
@@ -1392,7 +1393,10 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 	clear_all_latency_tracing(p);
 
 	/* ok, now we should be set up.. */
-	p->pid = pid_nr(pid);
+
+	/* tast_struct::pid is const pid_t, hence the ugly cast */
+	pid_p = (pid_t*)&(p->pid);
+	*pid_p = pid_nr(pid);
 	if (clone_flags & CLONE_THREAD) {
 		p->exit_signal = -1;
 		p->group_leader = current->group_leader;
-- 
1.7.1


  reply	other threads:[~2013-12-16 21:13 UTC|newest]

Thread overview: 107+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-20 21:31 [PATCH 00/12] RFC: steps to make audit pid namespace-safe Richard Guy Briggs
2013-08-20 21:31 ` [PATCH 01/12] audit: Kill the unused struct audit_aux_data_capset Richard Guy Briggs
2013-08-20 21:31 ` [PATCH 02/12] audit: fix netlink portid naming and types Richard Guy Briggs
2013-08-20 21:31 ` [PATCH 03/12] pid: get ppid pid_t of task in init_pid_ns safely Richard Guy Briggs
2013-08-27 17:21   ` Oleg Nesterov
2013-08-30 19:56     ` Richard Guy Briggs
2013-08-30 20:37       ` John Johansen
2013-08-30 22:41         ` [PATCH 1/3] apparmor: fix capability to not use the current task, during reporting John Johansen
2013-08-30 22:42         ` [PATCH 2/3] apparmor: remove tsk field from the apparmor_audit_struct John Johansen
2013-08-30 22:43         ` [PATCH 03/3] apparmor: remove parent task info from audit logging John Johansen
2013-09-03 18:31         ` [PATCH 03/12] pid: get ppid pid_t of task in init_pid_ns safely Richard Guy Briggs
2013-12-11 14:47           ` Richard Guy Briggs
2013-12-11 16:44             ` John Johansen
2013-12-11 17:19               ` Richard Guy Briggs
2013-08-20 21:31 ` [PATCH 04/12] audit: convert PPIDs to the inital PID namespace Richard Guy Briggs
2013-08-20 21:31 ` [PATCH 05/12] pid: get pid_t of task in init_pid_ns correctly Richard Guy Briggs
2013-08-20 21:31 ` [PATCH 06/12] audit: Simplify and correct audit_log_capset Richard Guy Briggs
2013-08-20 21:31 ` [PATCH 07/12] audit: store audit_pid as a struct pid pointer Richard Guy Briggs
2013-08-20 21:32 ` [PATCH 08/12] audit: anchor all pid references in the initial pid namespace Richard Guy Briggs
2013-08-20 21:32 ` [PATCH 09/12] pid: modify task_pid_nr to work without task->pid Richard Guy Briggs
2013-12-16 21:03   ` Richard Guy Briggs [this message]
2013-12-17  9:58     ` [PATCH] pid: change task_struct::pid to read-only Peter Zijlstra
2013-12-20  4:48       ` Richard Guy Briggs
2013-12-20  8:58         ` Peter Zijlstra
2013-12-20 14:04           ` Richard Guy Briggs
2014-01-23 19:32           ` [PATCH 0/7][RFC] pid: changes to support audit Richard Guy Briggs
2014-01-23 19:32             ` [PATCH 1/7] pid: change task_struct::pid to read-only Richard Guy Briggs
2014-01-23 19:32             ` [PATCH 2/7] compiler: CONST_CAST makes writing const vars easier and obvious Richard Guy Briggs
2014-01-23 19:32             ` [PATCH 3/7] pid: use the CONST_CAST macro instead to write to const task_struct::pid Richard Guy Briggs
2014-01-23 19:32             ` [PATCH 4/7] pid: modify task_tgid_nr to work without task->tgid Richard Guy Briggs
2014-02-20 18:35               ` Oleg Nesterov
2014-02-21 20:47                 ` Richard Guy Briggs
2014-02-24 18:40                   ` Oleg Nesterov
2014-01-23 19:32             ` [PATCH 5/7] pid: rewrite task helper function is_global_init() avoiding task->pid Richard Guy Briggs
2014-02-20 18:39               ` Oleg Nesterov
2014-02-21 16:10                 ` Richard Guy Briggs
2014-01-23 19:32             ` [PATCH 6/7] pid: mark struct task const in helper functions Richard Guy Briggs
2014-01-23 19:32             ` [PATCH 7/7] pid: get pid_t ppid of task in init_pid_ns Richard Guy Briggs
2014-02-20 19:01               ` Oleg Nesterov
2014-02-21 18:10                 ` Richard Guy Briggs
2014-02-24 18:32                   ` Oleg Nesterov
2014-03-17 20:14               ` Tony Luck
2014-03-17 20:15                 ` Eric Paris
2014-01-23 21:25             ` [PATCH 0/7][RFC] pid: changes to support audit Peter Zijlstra
2014-01-24  6:14               ` Richard Guy Briggs
2014-01-24  8:52                 ` Peter Zijlstra
2014-01-24 14:31                   ` Richard Guy Briggs
2014-02-19 16:18             ` Richard Guy Briggs
2014-02-19 17:47               ` Oleg Nesterov
2014-02-19 18:15                 ` Richard Guy Briggs
2014-02-20 19:08                   ` Oleg Nesterov
2013-12-17  9:59     ` [PATCH] pid: change task_struct::pid to read-only Peter Zijlstra
2013-12-17 15:36     ` Oleg Nesterov
2013-12-17 15:40       ` Oleg Nesterov
2013-12-20 19:01         ` Oleg Nesterov
2013-12-20 20:19           ` Richard Guy Briggs
2013-12-20 21:33           ` Peter Zijlstra
2013-12-22 16:03             ` Oleg Nesterov
2014-01-23 19:24               ` Richard Guy Briggs
2013-08-20 21:32 ` [PATCH 10/12] pid: modify task_tgid_nr to work without task->tgid Richard Guy Briggs
2013-08-20 21:32 ` [PATCH 11/12] pid: rewrite task helper functions avoiding task->pid and task->tgid Richard Guy Briggs
2013-08-22 19:08   ` Oleg Nesterov
2013-08-26 22:07     ` Richard Guy Briggs
2013-08-27 16:15       ` Oleg Nesterov
2013-12-16 17:35       ` Richard Guy Briggs
2013-12-16 21:05         ` Oleg Nesterov
2013-12-16 22:20           ` Richard Guy Briggs
2013-12-17  9:34             ` Peter Zijlstra
2013-12-17  9:48               ` Peter Zijlstra
2013-12-20  4:54               ` Richard Guy Briggs
2013-08-22 20:05   ` Peter Zijlstra
2013-08-22 21:43     ` Richard Guy Briggs
2013-08-23  6:36       ` Peter Zijlstra
2013-08-27  2:37         ` Richard Guy Briggs
2013-08-27 12:11           ` Peter Zijlstra
2013-08-27 21:35             ` Eric W. Biederman
2013-08-28  8:16               ` Peter Zijlstra
2013-08-23 19:28       ` Oleg Nesterov
2013-08-27  3:04         ` Richard Guy Briggs
2013-08-27 17:11           ` Oleg Nesterov
2013-08-30 19:06             ` audit looks unmaintained? [was: Re: [PATCH 11/12] pid: rewrite task helper functions avoiding task->pid and task->tgid] Richard Guy Briggs
2013-08-30 19:54               ` Steve Grubb
2013-09-08 15:54                 ` Oleg Nesterov
2013-09-10 17:20                   ` Oleg Nesterov
2013-09-13 18:42                     ` Steve Grubb
2013-09-14 18:10                       ` Oleg Nesterov
2013-09-13 18:28                   ` Steve Grubb
2013-09-14 18:08                     ` Oleg Nesterov
2013-08-20 21:32 ` [PATCH 12/12] pid: mark struct task const in helper functions Richard Guy Briggs
2013-12-23 22:27 ` [PATCH 0/5][RFC][v2] steps to make audit pid namespace-safe Richard Guy Briggs
2013-12-23 22:27   ` [PATCH 1/5] pid: get pid_t ppid of task in init_pid_ns Richard Guy Briggs
2013-12-30 17:04     ` Oleg Nesterov
2013-12-23 22:27   ` [PATCH 2/5] audit: convert PPIDs to the inital PID namespace Richard Guy Briggs
2013-12-30 17:07     ` Oleg Nesterov
2013-12-23 22:27   ` [PATCH 3/5] audit: store audit_pid as a struct pid pointer Richard Guy Briggs
2013-12-30 17:51     ` Oleg Nesterov
2014-01-21 23:37       ` Richard Guy Briggs
2013-12-23 22:27   ` [PATCH 4/5] audit: anchor all pid references in the initial pid namespace Richard Guy Briggs
2013-12-30 18:06     ` Oleg Nesterov
2014-02-19 20:28       ` Richard Guy Briggs
2013-12-23 22:27   ` [PATCH 5/5] audit: allow user processes to log from another PID namespace Richard Guy Briggs
2014-02-19 20:57   ` [PATCH 0/5][RFC][v3] steps to make audit pid namespace-safe Richard Guy Briggs
2014-02-19 20:57     ` [PATCH 1/5] pid: get pid_t ppid of task in init_pid_ns Richard Guy Briggs
2014-02-19 20:57     ` [PATCH 2/5] audit: convert PPIDs to the inital PID namespace Richard Guy Briggs
2014-02-19 20:57     ` [PATCH 3/5] audit: store audit_pid as a struct pid pointer Richard Guy Briggs
2014-02-19 20:57     ` [PATCH 4/5] audit: anchor all pid references in the initial pid namespace Richard Guy Briggs
2014-02-19 20:57     ` [PATCH 5/5] audit: allow user processes to log from another PID namespace Richard Guy Briggs

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ae3d74e79649d9e4962d0da030fb4852ddc5b8d1.1387227114.git.rgb@redhat.com \
    --to=rgb@redhat.com \
    --cc=eparis@redhat.com \
    --cc=linux-audit@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).