[PATCH v7 1/5] PID namespace translation support

Dmitry V. Levin ldv at altlinux.org
Sat Aug 8 22:07:56 UTC 2020


On Sat, Aug 08, 2020 at 11:14:33PM +0200, Ákos Uzonyi wrote:
[...]
> --- a/NEWS
> +++ b/NEWS
> @@ -22,6 +22,7 @@ Noteworthy changes in release 5.8 (2020-08-06)
>      KEYCTL_*, KVM_*, LOOP_*, NDA_*, RTC_*, TCA_*, STATX_*, and *_MAGIC
>      constants.
>    * Updated lists of ioctl commands from Linux 5.8.
> +  * Added --pidns-translation option for PID namespace translation.
>  
>  * Bug fixes
>    * Added statx syscall to %fstat trace class.

This went to the old section of the NEWS.

> --- a/defs.h
> +++ b/defs.h
> @@ -983,6 +1005,28 @@ print_local_array_ex(struct tcb *tcp,
>  extern kernel_ulong_t *
>  fetch_indirect_syscall_args(struct tcb *, kernel_ulong_t addr, unsigned int n_args);
>  
> +extern void pidns_init(void);
> +
> +/**
> + * Returns the pid of the tracee as present in /proc of the tracer (can be
> + * different from tcp->pid if /proc and the tracee process are in different PID
> + * namespaces).
> + */
> +extern int get_proc_pid(struct tcb *);

I though it can be different only if /proc and the _tracer_
are in different PID namespaces.

> +
> +/**
> + * Translates a pid from tracee's namespace to our namepace.
> + *
> + * @param tcp             The tcb of the tracee
> + *                        (NULL: from_id is in strace's namespace. Useful for
> + *                         getting the proc PID of from_id)
> + * @param from_id         The id to be translated
> + * @param type            The PID type of from_id
> + * @param proc_pid_ptr    If not NULL, writes the proc PID to this location
> + */
> +extern int translate_pid(struct tcb *, int dest_id, enum pid_type type,
> +		    int *proc_pid_ptr);

Sometimes this function returns the translated pid, sometimes it doesn't.
Could you document the return value, please?

Also, could you check whether all users of this function do proper error
handling, please?  I'm afraid sometimes they just go on with 0 instead of
translated pid.

> @@ -1058,6 +1102,15 @@ printfd(struct tcb *tcp, int fd)
>   * of the tracee the descriptor tcp).  This is a stub.
>   */
>  extern void printfd_pid_tracee_ns(struct tcb *tcp, pid_t pid, int fd);
> +
> +/** Prints a PID in the tracee's PID namespace */
> +extern void printpid(struct tcb *, int pid, enum pid_type type);

This is slightly ambiguous.  One might think it translates a pid into the
tracee's PID namespace and prints it, which is certainly not the case.

I think this function prints a pid which is specified in the tracee's PID namespace.

[...]
> +int
> +translate_pid(struct tcb *tcp, int from_id, enum pid_type type,
> +              int *proc_pid_ptr)
> +{
> +	if (from_id <= 0 || type < 0 || type >= PT_COUNT)
> +		return 0;
> +
> +	/* If translation is trivial */
> +	if ((!tcp || get_ns(tcp) == get_our_ns()) &&
> +	    (is_proc_ours() || !proc_pid_ptr)) {

I suggest checking for !proc_pid_ptr first because its very cheap.


-- 
ldv


More information about the Strace-devel mailing list