All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] ptrace: add ability to attach a file descriptor to another process
@ 2011-12-16 10:46 Andrew Vagin
  2011-12-16 10:46 ` [PATCH 1/2] fs: add ability to attach and to allocate fd for non current tasks Andrew Vagin
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Andrew Vagin @ 2011-12-16 10:46 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-fsdevel, Alexander Viro, Roland McGrath, Oleg Nesterov,
	Steven Rostedt, Cyrill Gorcunov, Pavel Emelyanov, Tejun Heo

We need this functionality for checkpointing processes.  Now some parts
are dumped with help PTRACE_SEIZE.  Parasite code is injected to process
and it collects information.  This code should save data to somewhere.
I want to suggest a scheme, when a dumper creates file descriptor and
attaches it to a target process, then execute parasite code, which closes
this descriptor at the end.

We can't create unix sockets or open files, because a process may
be in another namespaces.

Signed-off-by: Andrew Vagin <avagin@openvz.org>

Andrew Vagin (2):
  fs: add ability attaching and allocating fd for non current tasks
  ptrace: add ability to attach a file descriptor to another process

 fs/file.c              |    9 +++++++--
 fs/open.c              |    9 +++++++--
 include/linux/file.h   |    4 ++++
 include/linux/ptrace.h |    1 +
 kernel/ptrace.c        |   27 +++++++++++++++++++++++++++
 5 files changed, 46 insertions(+), 4 deletions(-)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/2] fs: add ability to attach and to allocate fd for non current tasks
  2011-12-16 10:46 [PATCH 0/2] ptrace: add ability to attach a file descriptor to another process Andrew Vagin
@ 2011-12-16 10:46 ` Andrew Vagin
  2011-12-16 10:46 ` [PATCH 2/2] ptrace: add ability to attach a file descriptor to another task Andrew Vagin
  2011-12-16 16:05 ` [PATCH 0/2] ptrace: add ability to attach a file descriptor to another process Tejun Heo
  2 siblings, 0 replies; 5+ messages in thread
From: Andrew Vagin @ 2011-12-16 10:46 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-fsdevel, Alexander Viro, Roland McGrath, Oleg Nesterov,
	Steven Rostedt, Cyrill Gorcunov, Pavel Emelyanov, Tejun Heo


Signed-off-by: Andrew Vagin <avagin@openvz.org>
---
 fs/file.c            |    9 +++++++--
 fs/open.c            |    9 +++++++--
 include/linux/file.h |    4 ++++
 3 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/fs/file.c b/fs/file.c
index 4c6992d..fbf2879 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -428,9 +428,9 @@ struct files_struct init_files = {
 /*
  * allocate a file descriptor, mark it busy.
  */
-int alloc_fd(unsigned start, unsigned flags)
+int alloc_task_fd(struct task_struct *tsk, unsigned start, unsigned flags)
 {
-	struct files_struct *files = current->files;
+	struct files_struct *files = tsk->files;
 	unsigned int fd;
 	int error;
 	struct fdtable *fdt;
@@ -479,6 +479,11 @@ out:
 	return error;
 }
 
+int alloc_fd(unsigned start, unsigned flags)
+{
+	return alloc_task_fd(current, start, flags);
+}
+
 int get_unused_fd(void)
 {
 	return alloc_fd(0, 0);
diff --git a/fs/open.c b/fs/open.c
index 22c41b5..9d340cc 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -864,9 +864,9 @@ EXPORT_SYMBOL(put_unused_fd);
  * will follow.
  */
 
-void fd_install(unsigned int fd, struct file *file)
+void fd_task_install(struct task_struct *tsk, unsigned int fd, struct file *file)
 {
-	struct files_struct *files = current->files;
+	struct files_struct *files = tsk->files;
 	struct fdtable *fdt;
 	spin_lock(&files->file_lock);
 	fdt = files_fdtable(files);
@@ -875,6 +875,11 @@ void fd_install(unsigned int fd, struct file *file)
 	spin_unlock(&files->file_lock);
 }
 
+void fd_install(unsigned int fd, struct file *file)
+{
+	return fd_task_install(current, fd, file);
+}
+
 EXPORT_SYMBOL(fd_install);
 
 static inline int build_open_flags(int flags, int mode, struct open_flags *op)
diff --git a/include/linux/file.h b/include/linux/file.h
index 21a7995..0250cf4 100644
--- a/include/linux/file.h
+++ b/include/linux/file.h
@@ -34,10 +34,14 @@ extern struct file *fget_raw_light(unsigned int fd, int *fput_needed);
 extern void set_close_on_exec(unsigned int fd, int flag);
 extern void put_filp(struct file *);
 extern int alloc_fd(unsigned start, unsigned flags);
+struct task_struct;
+extern int alloc_task_fd(struct task_struct *tsk, unsigned start, unsigned flags);
 extern int get_unused_fd(void);
 #define get_unused_fd_flags(flags) alloc_fd(0, (flags))
 extern void put_unused_fd(unsigned int fd);
 
+extern void fd_task_install(struct task_struct *tsk,
+				unsigned int fd, struct file *file);
 extern void fd_install(unsigned int fd, struct file *file);
 
 #endif /* __LINUX_FILE_H */
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] ptrace: add ability to attach a file descriptor to another task
  2011-12-16 10:46 [PATCH 0/2] ptrace: add ability to attach a file descriptor to another process Andrew Vagin
  2011-12-16 10:46 ` [PATCH 1/2] fs: add ability to attach and to allocate fd for non current tasks Andrew Vagin
@ 2011-12-16 10:46 ` Andrew Vagin
  2011-12-18 18:17   ` Oleg Nesterov
  2011-12-16 16:05 ` [PATCH 0/2] ptrace: add ability to attach a file descriptor to another process Tejun Heo
  2 siblings, 1 reply; 5+ messages in thread
From: Andrew Vagin @ 2011-12-16 10:46 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-fsdevel, Alexander Viro, Roland McGrath, Oleg Nesterov,
	Steven Rostedt, Cyrill Gorcunov, Pavel Emelyanov, Tejun Heo


Signed-off-by: Andrew Vagin <avagin@openvz.org>
---
 include/linux/ptrace.h |    1 +
 kernel/ptrace.c        |   27 +++++++++++++++++++++++++++
 2 files changed, 28 insertions(+), 0 deletions(-)

diff --git a/include/linux/ptrace.h b/include/linux/ptrace.h
index 800f113..8439ce9 100644
--- a/include/linux/ptrace.h
+++ b/include/linux/ptrace.h
@@ -50,6 +50,7 @@
 #define PTRACE_SEIZE		0x4206
 #define PTRACE_INTERRUPT	0x4207
 #define PTRACE_LISTEN		0x4208
+#define PTRACE_DUPFD		0x4209
 
 /* flags in @data for PTRACE_SEIZE */
 #define PTRACE_SEIZE_DEVEL	0x80000000 /* temp flag for development */
diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index 24d0447..a646d01 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -24,6 +24,7 @@
 #include <linux/regset.h>
 #include <linux/hw_breakpoint.h>
 #include <linux/cn_proc.h>
+#include <linux/file.h>
 
 
 static int ptrace_trapping_sleep_fn(void *flags)
@@ -826,6 +827,32 @@ int ptrace_request(struct task_struct *child, long request,
 		break;
 	}
 #endif
+	case PTRACE_DUPFD:
+	{
+		struct file *file = fget_raw(data);
+		ret = -EBADF;
+
+		if (!file)
+			break;
+
+		task_lock(child);
+		if (!child->files)
+			goto out_getfd;
+
+		ret = alloc_task_fd(child, 0, 0);
+		if (ret < 0) {
+			fput(file);
+			goto out_getfd;
+		}
+
+		fd_task_install(child, ret, file);
+
+out_getfd:
+		task_unlock(child);
+
+		break;
+	}
+
 	default:
 		break;
 	}
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 0/2] ptrace: add ability to attach a file descriptor to another process
  2011-12-16 10:46 [PATCH 0/2] ptrace: add ability to attach a file descriptor to another process Andrew Vagin
  2011-12-16 10:46 ` [PATCH 1/2] fs: add ability to attach and to allocate fd for non current tasks Andrew Vagin
  2011-12-16 10:46 ` [PATCH 2/2] ptrace: add ability to attach a file descriptor to another task Andrew Vagin
@ 2011-12-16 16:05 ` Tejun Heo
  2 siblings, 0 replies; 5+ messages in thread
From: Tejun Heo @ 2011-12-16 16:05 UTC (permalink / raw)
  To: Andrew Vagin
  Cc: linux-kernel, linux-fsdevel, Alexander Viro, Roland McGrath,
	Oleg Nesterov, Steven Rostedt, Cyrill Gorcunov, Pavel Emelyanov

Hello,

On Fri, Dec 16, 2011 at 01:46:21PM +0300, Andrew Vagin wrote:
> We need this functionality for checkpointing processes.  Now some parts
> are dumped with help PTRACE_SEIZE.  Parasite code is injected to process
> and it collects information.  This code should save data to somewhere.
> I want to suggest a scheme, when a dumper creates file descriptor and
> attaches it to a target process, then execute parasite code, which closes
> this descriptor at the end.
> 
> We can't create unix sockets or open files, because a process may
> be in another namespaces.

I really dislike this.  This doesn't belong in ptrace at all.  With
parasite, you have full control of the process, there gotta be some
other way to talk back.  Can't you create a management process w/ open
UNIX socket when spawning a namespace?  If that doesn't work, wouldn't
letting it dump to filesystem and retrieving it afterwards work?

Thanks.

-- 
tejun

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] ptrace: add ability to attach a file descriptor to another task
  2011-12-16 10:46 ` [PATCH 2/2] ptrace: add ability to attach a file descriptor to another task Andrew Vagin
@ 2011-12-18 18:17   ` Oleg Nesterov
  0 siblings, 0 replies; 5+ messages in thread
From: Oleg Nesterov @ 2011-12-18 18:17 UTC (permalink / raw)
  To: Andrew Vagin
  Cc: linux-kernel, linux-fsdevel, Alexander Viro, Roland McGrath,
	Steven Rostedt, Cyrill Gorcunov, Pavel Emelyanov, Tejun Heo

On 12/16, Andrew Vagin wrote:
>
> Signed-off-by: Andrew Vagin <avagin@openvz.org>

Nice changelog ;)

I agree with Tejun, this doesn't look like the good idea to me...



As for the patch itself,

> @@ -826,6 +827,32 @@ int ptrace_request(struct task_struct *child, long request,
>  		break;
>  	}
>  #endif
> +	case PTRACE_DUPFD:
> +	{
> +		struct file *file = fget_raw(data);
> +		ret = -EBADF;
> +
> +		if (!file)
> +			break;
> +
> +		task_lock(child);
> +		if (!child->files)
> +			goto out_getfd;
> +
> +		ret = alloc_task_fd(child, 0, 0);

alloc_fdtable() does kmalloc(GFP_KERNEL)/vmalloc(), not good under
spin_lock().

> +		if (ret < 0) {
> +			fput(file);

We can't do this under task_lock() too.

A tracer's sub-thread (or any CLONE_FILES task) can close this file,
it is possible that we have the last reference.


Probably, instead of the new *_task_* helpers, you can add
alloc_file_fd/fd_files_install which take the result of
get_files_struct() as the additional argument. This way you can
avoid task_lock().

Oleg.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-12-18 18:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-16 10:46 [PATCH 0/2] ptrace: add ability to attach a file descriptor to another process Andrew Vagin
2011-12-16 10:46 ` [PATCH 1/2] fs: add ability to attach and to allocate fd for non current tasks Andrew Vagin
2011-12-16 10:46 ` [PATCH 2/2] ptrace: add ability to attach a file descriptor to another task Andrew Vagin
2011-12-18 18:17   ` Oleg Nesterov
2011-12-16 16:05 ` [PATCH 0/2] ptrace: add ability to attach a file descriptor to another process Tejun Heo

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.