public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Samuel Thibault <samuel.thibault@gnu.org>
To: Sergey Bugaev <bugaevc@gmail.com>
Cc: libc-alpha@sourceware.org, bug-hurd@gnu.org
Subject: Re: [PATCH 5/7] hurd: Don't leak the auth port in msg* RPCs
Date: Sat, 29 Apr 2023 16:56:43 +0200	[thread overview]
Message-ID: <20230429145643.nevw6wqnzjebvyrn@begin> (raw)
In-Reply-To: <20230429131354.2507443-5-bugaevc@gmail.com>

Applied, thanks!

Sergey Bugaev, le sam. 29 avril 2023 16:13:52 +0300, a ecrit:
> The leak can be easily reproduced (and observed) using the portinfo
> tool:
> 
> $ portinfo -v $$ | grep task
>     36: send task(1577)(self) (refs: 127)
> $ portinfo -v $$ | grep task
>     36: send task(1577)(self) (refs: 253)
> $ portinfo -v $$ | grep task
>     36: send task(1577)(self) (refs: 379)
> $ portinfo -v $$ | grep task
>     36: send task(1577)(self) (refs: 505)
> $ portinfo -v $$ | grep task
>     36: send task(1577)(self) (refs: 631)
> 
> Checked on i686-gnu.
> 
> Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
> ---
>  hurd/hurdmsg.c | 67 +++++++++++++++++++++++++++++++++++++++++++-------
>  1 file changed, 58 insertions(+), 9 deletions(-)
> 
> diff --git a/hurd/hurdmsg.c b/hurd/hurdmsg.c
> index 4bedd292..896fb87c 100644
> --- a/hurd/hurdmsg.c
> +++ b/hurd/hurdmsg.c
> @@ -35,11 +35,17 @@ kern_return_t
>    _S_msg_get_init_port (mach_port_t msgport, mach_port_t auth, int which,
>  			mach_port_t *result, mach_msg_type_name_t *result_type)
>  {
> +  error_t err;
> +
>    AUTHCHECK;
> +
>    *result_type = MACH_MSG_TYPE_MOVE_SEND;
>    /* This function adds a new user reference for the *RESULT it gives back.
>       Our reply message uses a move-send right that consumes this reference.  */
> -  return _hurd_ports_get (which, result);
> +  err = _hurd_ports_get (which, result);
> +  if (!err && MACH_PORT_VALID (auth))
> +    __mach_port_deallocate (__mach_task_self (), auth);
> +  return err;
>  }
>  
>  kern_return_t
> @@ -51,10 +57,13 @@ _S_msg_set_init_port (mach_port_t msgport, mach_port_t auth,
>    AUTHCHECK;
>  
>    err = _hurd_ports_set (which, port);
> -  if (err == 0)
> +
> +  if (!err && MACH_PORT_VALID (port))
>      __mach_port_deallocate (__mach_task_self (), port);
> +  if (!err && MACH_PORT_VALID (auth))
> +    __mach_port_deallocate (__mach_task_self (), auth);
>  
> -  return 0;
> +  return err;
>  }
>  
>  kern_return_t
> @@ -88,6 +97,8 @@ _S_msg_get_init_ports (mach_port_t msgport, mach_port_t auth,
>        }
>  
>    *ports_type = MACH_MSG_TYPE_MOVE_SEND;
> +  if (MACH_PORT_VALID (auth))
> +    __mach_port_deallocate (__mach_task_self (), auth);
>    return 0;
>  }
>  
> @@ -108,6 +119,8 @@ _S_msg_set_init_ports (mach_port_t msgport, mach_port_t auth,
>  	__mach_port_deallocate (__mach_task_self (), ports[i]);
>      }
>  
> +  if (MACH_PORT_VALID (auth))
> +    __mach_port_deallocate (__mach_task_self (), auth);
>    return 0;
>  }
>  \f
> @@ -152,9 +165,16 @@ kern_return_t
>  _S_msg_get_init_int (mach_port_t msgport, mach_port_t auth,
>  		     int which, int *value)
>  {
> +  error_t err;
> +
>    AUTHCHECK;
>  
> -  return get_int (which, value);
> +  err = get_int (which, value);
> +  if (err)
> +    return err;
> +  if (MACH_PORT_VALID (auth))
> +    __mach_port_deallocate (__mach_task_self (), auth);
> +  return 0;
>  }
>  
>  kern_return_t
> @@ -185,6 +205,8 @@ _S_msg_get_init_ints (mach_port_t msgport, mach_port_t auth,
>  	return err;
>        }
>  
> +  if (MACH_PORT_VALID (auth))
> +    __mach_port_deallocate (__mach_task_self (), auth);
>    return 0;
>  }
>  
> @@ -236,9 +258,16 @@ kern_return_t
>  _S_msg_set_init_int (mach_port_t msgport, mach_port_t auth,
>  		     int which, int value)
>  {
> +  error_t err;
> +
>    AUTHCHECK;
>  
> -  return set_int (which, value);
> +  err = set_int (which, value);
> +  if (err)
> +    return err;
> +  if (MACH_PORT_VALID (auth))
> +    __mach_port_deallocate (__mach_task_self (), auth);
> +  return 0;
>  }
>  
>  kern_return_t
> @@ -261,6 +290,8 @@ _S_msg_set_init_ints (mach_port_t msgport, mach_port_t auth,
>  	return err;
>        }
>  
> +  if (MACH_PORT_VALID (auth))
> +    __mach_port_deallocate (__mach_task_self (), auth);
>    return 0;
>  }
>  \f
> @@ -278,6 +309,8 @@ _S_msg_get_fd (mach_port_t msgport, mach_port_t auth, int which,
>      return errno;
>    *result_type = MACH_MSG_TYPE_MOVE_SEND;
>  
> +  if (MACH_PORT_VALID (auth))
> +    __mach_port_deallocate (__mach_task_self (), auth);
>    return 0;
>  }
>  
> @@ -285,17 +318,25 @@ kern_return_t
>  _S_msg_set_fd (mach_port_t msgport, mach_port_t auth,
>  	       int which, mach_port_t port)
>  {
> +  error_t err;
> +
>    AUTHCHECK;
>  
>    /* We consume the reference if successful.  */
> -  return HURD_FD_USE (which, (_hurd_port2fd (descriptor, port, 0), 0));
> +  err = HURD_FD_USE (which, (_hurd_port2fd (descriptor, port, 0), 0));
> +  if (err)
> +    return err;
> +
> +  if (MACH_PORT_VALID (auth))
> +    __mach_port_deallocate (__mach_task_self (), auth);
> +  return 0;
>  }
>  \f
>  /* Snarfing and frobbing environment variables.  */
>  
>  kern_return_t
>  _S_msg_get_env_variable (mach_port_t msgport,
> -			 const_string_t variable, //
> +			 const_string_t variable,
>  			 char **data, mach_msg_type_number_t *datalen)
>  {
>    error_t err;
> @@ -322,14 +363,17 @@ _S_msg_get_env_variable (mach_port_t msgport,
>  
>  kern_return_t
>  _S_msg_set_env_variable (mach_port_t msgport, mach_port_t auth,
> -			 const_string_t variable, //
> -			 const_string_t value, //
> +			 const_string_t variable,
> +			 const_string_t value,
>  			 int replace)
>  {
>    AUTHCHECK;
>  
>    if (__setenv (variable, value, replace)) /* XXX name space */
>      return errno;
> +
> +  if (MACH_PORT_VALID (auth))
> +    __mach_port_deallocate (__mach_task_self (), auth);
>    return 0;
>  }
>  
> @@ -381,6 +425,9 @@ _S_msg_set_environment (mach_port_t msgport, mach_port_t auth,
>      return errno;
>    __argz_extract (data, datalen, envp);
>    __environ = envp;		/* XXX cooperate with loadenv et al */
> +
> +  if (MACH_PORT_VALID (auth))
> +    __mach_port_deallocate (__mach_task_self (), auth);
>    return 0;
>  }
>  \f
> @@ -433,6 +480,8 @@ _S_msg_get_dtable (mach_port_t process,
>  out:
>    __mutex_unlock (&_hurd_dtable_lock);
>    HURD_CRITICAL_END;
> +  if (!err && MACH_PORT_VALID (auth))
> +    __mach_port_deallocate (__mach_task_self (), auth);
>    return err;
>  }
>  
> -- 
> 2.40.1
> 
> 

-- 
Samuel
---
Pour une évaluation indépendante, transparente et rigoureuse !
Je soutiens la Commission d'Évaluation de l'Inria.

  reply	other threads:[~2023-04-29 14:56 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-29 13:13 [PATCH 1/7] hurd: Simplify _hurd_critical_section_lock a bit Sergey Bugaev
2023-04-29 13:13 ` [PATCH 2/7] hurd: Move libc_hidden_def's around Sergey Bugaev
2023-04-29 14:52   ` Samuel Thibault
2023-04-29 13:13 ` [PATCH 3/7] hurd: Mark various conditions as unlikely Sergey Bugaev
2023-04-29 14:53   ` Samuel Thibault
2023-04-29 13:13 ` [PATCH 4/7] hurd: Make _exit work during early boot-up Sergey Bugaev
2023-04-29 14:54   ` Samuel Thibault
2023-04-29 13:13 ` [PATCH 5/7] hurd: Don't leak the auth port in msg* RPCs Sergey Bugaev
2023-04-29 14:56   ` Samuel Thibault [this message]
2023-04-29 13:13 ` [PATCH 6/7] hurd: Respect existing FD_CLOEXEC in S_msg_set_fd Sergey Bugaev
2023-04-29 14:57   ` Samuel Thibault
2023-04-29 13:13 ` [PATCH 7/7] hurd: Fix FS_RETRY_MAGICAL "machtype" handling Sergey Bugaev
2023-04-29 15:01   ` Samuel Thibault
2023-04-29 14:50 ` [PATCH 1/7] hurd: Simplify _hurd_critical_section_lock a bit Samuel Thibault

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=20230429145643.nevw6wqnzjebvyrn@begin \
    --to=samuel.thibault@gnu.org \
    --cc=bug-hurd@gnu.org \
    --cc=bugaevc@gmail.com \
    --cc=libc-alpha@sourceware.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 read-only IMAP folder(s) and NNTP newsgroup(s).