[PATCH] hurd: Check return value of mach_port_mod_refs() in the dup routine of fcntl()

Samuel Thibault samuel.thibault@gnu.org
Thu Mar 6 00:21:06 GMT 2025


Hello,

While at it, we'd better also fix sysdeps/mach/hurd/dup3.c the exact
same way (which will also fix dup2).

Thanks,
Samuel

Zhaoming Luo, le jeu. 06 mars 2025 06:39:04 +0800, a ecrit:
> Ignoring the return value of mach_port_mod_ref() causes the situation

> +	    if (err)
> +	      {
> +		/* When an error occurs during giving a user ref to the
> +		   io server port */
> +	        result = -1;
> +
> +		if (err == KERN_UREFS_OVERFLOW)
> +		  errno = EMFILE;
> +		else
> +		  errno = EINVAL;

The idiomatic way is to use __hurd_fail:
		if (err == KERN_UREFS_OVERFLOW)
		  result = __hurd_failed (EMFILE);
		else
		  result = __hurd_failed (EINVAL);

> +	      }
> +	    else
> +	      {
> +		if (ctty != MACH_PORT_NULL)
> +		  __mach_port_mod_refs (__mach_task_self (), ctty,
> +			  MACH_PORT_RIGHT_SEND, 1);

This one could fail too, we want to fail the whole thing in that case
(and __mach_port_mod_refs(port, -1) to clean the ref successfully
acquired above).

> +
> +		/* Install the ports and flags in the new descriptor.  */
> +		if (ctty != MACH_PORT_NULL)
> +		  _hurd_port_set (&new->ctty, ctty);
> +		new->flags = flags;
> +		_hurd_port_locked_set (&new->port, port); /* Unlocks NEW.  */
> +	      }
>  	  }
>  
>  	HURD_CRITICAL_END;


More information about the Libc-alpha mailing list