Hello,

Here are a few places where, in my view, it would be advisable to check the
return value of the mach_port_mod_refs function.  In some instances, it might
be preferable to terminate execution by calling assert_perror_backtrace
rather than returning an error, though it is not yet clear to me which
approach is better in any given situation.

Thanks,

--
Mikhail Karpov
From 4d56c91d165ec1798fd29135104439ee971ed57a Mon Sep 17 00:00:00 2001
From: Mikhail Karpov <[email protected]>
Date: Mon, 26 Oct 2026 00:11:06 +0700
Subject: [PATCH 2/2] Added checks for mach_port_mod_refs return value in
 several place

---
 console/console.c               |  9 ++++-
 exec/exec.c                     | 18 +++++++--
 exec/hashexec.c                 | 15 ++++++--
 libdiskfs/boot-start.c          |  5 ++-
 libdiskfs/io-map.c              | 10 ++++-
 libdiskfs/peropen-make.c        | 32 +++++++++++-----
 libfshelp/fetch-root.c          | 13 ++++---
 libfshelp/translator-list.c     |  8 +++-
 libirqhelp/irqhelp.c            |  5 +--
 libnetfs/iostubs.c              | 10 ++++-
 libnetfs/make-peropen.c         | 38 +++++++++++--------
 libports/complete-deallocate.c  |  5 ++-
 libshouldbeinlibc/exec-reauth.c | 40 +++++++++++---------
 libshouldbeinlibc/portxlate.c   | 66 ++++++++++++++++-----------------
 libstore/file.c                 |  8 +++-
 libstore/map.c                  |  8 +++-
 libstore/unknown.c              | 16 ++++----
 libtrivfs/dir-lookup.c          | 11 +++++-
 libtrivfs/protid-dup.c          | 11 ++++--
 proc/host.c                     | 26 ++++++++++---
 proc/main.c                     |  4 +-
 proc/mgt.c                      | 20 +++++++---
 proc/pgrp.c                     | 32 ++++++++++++++--
 utils/rpctrace.c                |  6 ++-
 24 files changed, 280 insertions(+), 136 deletions(-)

diff --git a/console/console.c b/console/console.c
index efc83da..7157588 100644
--- a/console/console.c
+++ b/console/console.c
@@ -1234,6 +1234,7 @@ netfs_S_io_map (struct protid *cred,
 
   flags = cred->po->openstat & (O_READ | O_WRITE);
 
+  error_t err;
   pthread_mutex_lock (&np->lock);
   switch (flags)
     {
@@ -1242,7 +1243,13 @@ netfs_S_io_map (struct protid *cred,
 					     VM_PROT_READ | VM_PROT_WRITE);
       if (*wrobj == MACH_PORT_NULL)
         goto error;
-      mach_port_mod_refs (mach_task_self (), *rdobj, MACH_PORT_RIGHT_SEND, 1);
+      err = mach_port_mod_refs (mach_task_self (), *rdobj,
+				MACH_PORT_RIGHT_SEND, 1);
+      if (err)
+	{
+	  pthread_mutex_unlock (&np->lock);
+	  return err;
+	}
       break;
     case O_READ:
       *rdobj = display_get_filemap (vcons->display, VM_PROT_READ);
diff --git a/exec/exec.c b/exec/exec.c
index a79b483..3b8f052 100644
--- a/exec/exec.c
+++ b/exec/exec.c
@@ -910,8 +910,12 @@ do_exec (file_t file,
 	else
 	  {
 	    if (!consume && new != MACH_PORT_NULL)
-	      mach_port_mod_refs (mach_task_self (),
-				  new, MACH_PORT_RIGHT_SEND, 1);
+	      {
+		e.error = mach_port_mod_refs (mach_task_self (),
+					      new, MACH_PORT_RIGHT_SEND, 1);
+		if (e.error)
+		  return;
+	      }
 	  }
 
 	boot->portarray[idx] = new;
@@ -1181,8 +1185,14 @@ do_exec (file_t file,
 		  errno = EBADF;
 		  return MACH_PORT_NULL;
 		}
-	      mach_port_mod_refs (mach_task_self (), boot->dtable[fd],
-				  MACH_PORT_RIGHT_SEND, +1);
+	      error_t err = mach_port_mod_refs (mach_task_self (),
+						boot->dtable[fd],
+						MACH_PORT_RIGHT_SEND, +1);
+	      if (err)
+		{
+		  errno = err;
+		  return MACH_PORT_NULL;
+		}
 	      return boot->dtable[fd];
 	    }
 				/* XXX/fault */
diff --git a/exec/hashexec.c b/exec/hashexec.c
index 0953679..1ee2ed7 100644
--- a/exec/hashexec.c
+++ b/exec/hashexec.c
@@ -66,8 +66,13 @@ check_hashbang (struct execdata *e,
 	  const file_t dport = dtable[fd];
 	  if (dport != MACH_PORT_NULL)
 	    {
-	      mach_port_mod_refs (mach_task_self (), dport,
-				  MACH_PORT_RIGHT_SEND, +1);
+	      error_t err = mach_port_mod_refs (mach_task_self (), dport,
+						MACH_PORT_RIGHT_SEND, +1);
+	      if (err)
+		{
+		  errno = err;
+		  return MACH_PORT_NULL;
+		}
 	      return dport;
 	    }
 	}
@@ -344,8 +349,10 @@ check_hashbang (struct execdata *e,
 		}
 	      else
 		dtable[fd] = file;
-	      mach_port_mod_refs (mach_task_self (), file,
-				  MACH_PORT_RIGHT_SEND, +1);
+	      e->error = mach_port_mod_refs (mach_task_self (), file,
+					     MACH_PORT_RIGHT_SEND, +1);
+	      if (e->error)
+		goto end_setup_args;
 
 	      char *fd_file_name = alloca (100);
 	      sprintf (fd_file_name, "/dev/fd/%d", fd);
diff --git a/libdiskfs/boot-start.c b/libdiskfs/boot-start.c
index 93738b4..2f5a79c 100644
--- a/libdiskfs/boot-start.c
+++ b/libdiskfs/boot-start.c
@@ -608,8 +608,9 @@ diskfs_S_fsys_init (struct diskfs_control *pt,
   ports_port_deref (rootpi);
 
   /* We need two send rights, for the crdir and cwdir slots.  */
-  mach_port_mod_refs (mach_task_self (), root_pt,
-		      MACH_PORT_RIGHT_SEND, +1);
+  err = mach_port_mod_refs (mach_task_self (), root_pt,
+			    MACH_PORT_RIGHT_SEND, +1);
+  assert_perror_backtrace (err);
 
   if (_hurd_ports)
     {
diff --git a/libdiskfs/io-map.c b/libdiskfs/io-map.c
index 843b6b1..bf34c84 100644
--- a/libdiskfs/io-map.c
+++ b/libdiskfs/io-map.c
@@ -39,6 +39,7 @@ diskfs_S_io_map (struct protid *cred,
   node = cred->po->np;
   flags = cred->po->openstat & (O_READ | O_WRITE);
 
+  error_t err;
   pthread_mutex_lock (&node->lock);
   switch (flags)
     {
@@ -46,7 +47,14 @@ diskfs_S_io_map (struct protid *cred,
       *wrobj = *rdobj = diskfs_get_filemap (node, VM_PROT_READ |VM_PROT_WRITE);
       if (*wrobj == MACH_PORT_NULL)
 	goto error;
-      mach_port_mod_refs (mach_task_self (), *rdobj, MACH_PORT_RIGHT_SEND, 1);
+      err = mach_port_mod_refs (mach_task_self (), *rdobj,
+				MACH_PORT_RIGHT_SEND, 1);
+      if (err)
+	{
+	  pthread_mutex_unlock (&node->lock);
+	  mach_port_deallocate (mach_task_self (), *rdobj);
+	  return err;
+	}
       break;
     case O_READ:
       *rdobj = diskfs_get_filemap (node, VM_PROT_READ);
diff --git a/libdiskfs/peropen-make.c b/libdiskfs/peropen-make.c
index b4564a9..5b0f316 100644
--- a/libdiskfs/peropen-make.c
+++ b/libdiskfs/peropen-make.c
@@ -30,7 +30,7 @@ diskfs_make_peropen (struct node *np, int flags, struct peropen *context,
   struct peropen *po = *ppo = malloc (sizeof (struct peropen));
 
   if (! po)
-    return ENOMEM;
+    return errno;
 
   err = fshelp_rlock_po_init (&po->lock_status);
   if (err)
@@ -52,16 +52,19 @@ diskfs_make_peropen (struct node *np, int flags, struct peropen *context,
 	  po->path = strdup (context->path);
 	  if (! po->path)
 	    {
-	      fshelp_rlock_po_fini (&po->lock_status);
-	      free (po);
-	      return ENOMEM;
+	      err = errno;
+	      goto error;
 	    }
 	}
 
       po->root_parent = context->root_parent;
       if (po->root_parent != MACH_PORT_NULL)
-	mach_port_mod_refs (mach_task_self (), po->root_parent, 
-			    MACH_PORT_RIGHT_SEND, 1);
+	{
+	  err = mach_port_mod_refs (mach_task_self (), po->root_parent,
+				    MACH_PORT_RIGHT_SEND, 1);
+	  if (err)
+	    goto free_path;
+	}
 
       po->shadow_root = context->shadow_root;
       if (po->shadow_root)
@@ -69,8 +72,12 @@ diskfs_make_peropen (struct node *np, int flags, struct peropen *context,
 
       po->shadow_root_parent = context->shadow_root_parent;
       if (po->shadow_root_parent != MACH_PORT_NULL)
-	mach_port_mod_refs (mach_task_self (), po->shadow_root_parent, 
-			    MACH_PORT_RIGHT_SEND, 1);
+	{
+	  err = mach_port_mod_refs (mach_task_self (), po->shadow_root_parent,
+				    MACH_PORT_RIGHT_SEND, 1);
+	  if (err)
+	    goto free_path;
+	}
     }
   else
     {
@@ -80,6 +87,13 @@ diskfs_make_peropen (struct node *np, int flags, struct peropen *context,
     }
 
   diskfs_nref (np);
-
   return 0;
+
+ free_path:
+  free (po->path);
+
+ error:
+  fshelp_rlock_po_fini (&po->lock_status);
+  free (po);
+  return err;
 }
diff --git a/libfshelp/fetch-root.c b/libfshelp/fetch-root.c
index 1c09973..0950334 100644
--- a/libfshelp/fetch-root.c
+++ b/libfshelp/fetch-root.c
@@ -180,15 +180,16 @@ fshelp_fetch_root (struct transbox *box, void *cookie,
     }
 
   control = box->active;
-  mach_port_mod_refs (mach_task_self (), control,
-		      MACH_PORT_RIGHT_SEND, 1);
+  err = mach_port_mod_refs (mach_task_self (), control,
+			    MACH_PORT_RIGHT_SEND, 1);
   pthread_mutex_unlock (box->lock);
 
   /* Cancellation point XXX */
-  err = fsys_getroot (control, dotdot, MACH_MSG_TYPE_COPY_SEND,
-		      user->uids->ids, user->uids->num,
-		      user->gids->ids, user->gids->num,
-		      flags, retry, retryname, root);
+  if (!err)
+    err = fsys_getroot (control, dotdot, MACH_MSG_TYPE_COPY_SEND,
+			user->uids->ids, user->uids->num,
+			user->gids->ids, user->gids->num,
+			flags, retry, retryname, root);
 
   pthread_mutex_lock (box->lock);
 
diff --git a/libfshelp/translator-list.c b/libfshelp/translator-list.c
index 6d4f941..6a42549 100644
--- a/libfshelp/translator-list.c
+++ b/libfshelp/translator-list.c
@@ -147,8 +147,12 @@ fshelp_set_active_translator (struct port_info *pi,
 
       if (MACH_PORT_VALID (t->active))
 	mach_port_deallocate (mach_task_self (), t->active);
-      mach_port_mod_refs (mach_task_self (), transbox->active,
-			  MACH_PORT_RIGHT_SEND, +1);
+
+      err = mach_port_mod_refs (mach_task_self (), transbox->active,
+				MACH_PORT_RIGHT_SEND, +1);
+      if (err)
+	goto out;
+
       t->active = transbox->active;
     }
   else if (! MACH_PORT_VALID (transbox->active))
diff --git a/libirqhelp/irqhelp.c b/libirqhelp/irqhelp.c
index b94fc61..e6eeafc 100644
--- a/libirqhelp/irqhelp.c
+++ b/libirqhelp/irqhelp.c
@@ -373,8 +373,5 @@ irqhelp_remove_interrupt_handler(struct irq *irq)
 
   err = mach_port_mod_refs (mach_task_self (), irq->port,
 			    MACH_PORT_RIGHT_RECEIVE, -1);
-  if (err)
-    return err;
-
-  return 0;
+  return err;
 }
diff --git a/libnetfs/iostubs.c b/libnetfs/iostubs.c
index 09d8077..10110a8 100644
--- a/libnetfs/iostubs.c
+++ b/libnetfs/iostubs.c
@@ -38,6 +38,7 @@ netfs_S_io_map (struct protid *user,
   node = user->po->np;
   flags = user->po->openstat & (O_READ | O_WRITE);
 
+  error_t err;
   pthread_mutex_lock (&node->lock);
   switch (flags)
     {
@@ -45,7 +46,14 @@ netfs_S_io_map (struct protid *user,
       *wrobj = *rdobj = netfs_get_filemap (node, VM_PROT_READ |VM_PROT_WRITE);
       if (*wrobj == MACH_PORT_NULL)
 	goto error;
-      mach_port_mod_refs (mach_task_self (), *rdobj, MACH_PORT_RIGHT_SEND, 1);
+      err = mach_port_mod_refs (mach_task_self (), *rdobj,
+				MACH_PORT_RIGHT_SEND, 1);
+      if (err)
+	{
+	  pthread_mutex_unlock (&node->lock);
+	  mach_port_deallocate (mach_task_self (), *rdobj);
+	  return err;
+	}
       break;
     case O_READ:
       *rdobj = netfs_get_filemap (node, VM_PROT_READ);
diff --git a/libnetfs/make-peropen.c b/libnetfs/make-peropen.c
index 9bb4892..1ebe5ee 100644
--- a/libnetfs/make-peropen.c
+++ b/libnetfs/make-peropen.c
@@ -46,28 +46,25 @@ netfs_make_peropen (struct node *np, int flags, struct peropen *context)
   if (netfs_peropen_create_hook)
     err = (*netfs_peropen_create_hook) (po);
   if (err)
-    {
-      fshelp_rlock_po_fini (&po->lock_status);
-      free (po);
-      return NULL;
-    }
+    goto error;
 
   if (context)
     {
       if (context->path)
 	{
 	  po->path = strdup (context->path);
-	  if (! po->path) {
-	    fshelp_rlock_po_fini (&po->lock_status);
-	    free (po);
-	    return NULL;
-	  }
+	  if (! po->path)
+	    goto error;
 	}
 
       po->root_parent = context->root_parent;
       if (po->root_parent != MACH_PORT_NULL)
-	mach_port_mod_refs (mach_task_self (), po->root_parent,
-			    MACH_PORT_RIGHT_SEND, 1);
+	{
+	  err = mach_port_mod_refs (mach_task_self (), po->root_parent,
+				    MACH_PORT_RIGHT_SEND, 1);
+	  if (err)
+	    goto free_path;
+	}
 
       po->shadow_root = context->shadow_root;
       if (po->shadow_root)
@@ -75,11 +72,22 @@ netfs_make_peropen (struct node *np, int flags, struct peropen *context)
 
       po->shadow_root_parent = context->shadow_root_parent;
       if (po->shadow_root_parent != MACH_PORT_NULL)
-	mach_port_mod_refs (mach_task_self (), po->shadow_root_parent,
-			    MACH_PORT_RIGHT_SEND, 1);
+	{
+	  err = mach_port_mod_refs (mach_task_self (), po->shadow_root_parent,
+				    MACH_PORT_RIGHT_SEND, 1);
+	  if (err)
+	    goto free_path;
+	}
     }
 
   netfs_nref (np);
-
   return po;
+
+ free_path:
+  free (po->path);
+
+ error:
+  fshelp_rlock_po_fini (&po->lock_status);
+  free (po);
+  return NULL;
 }
diff --git a/libports/complete-deallocate.c b/libports/complete-deallocate.c
index b7d5f76..a6fe0ee 100644
--- a/libports/complete-deallocate.c
+++ b/libports/complete-deallocate.c
@@ -47,8 +47,9 @@ _ports_complete_deallocate (struct port_info *pi)
       refcounts_deref_weak (&pi->refcounts, &result);
       pthread_rwlock_unlock (&_ports_htable_lock);
 
-      mach_port_mod_refs (mach_task_self (), pi->port_right,
-			  MACH_PORT_RIGHT_RECEIVE, -1);
+      error_t err = mach_port_mod_refs (mach_task_self (), pi->port_right,
+					MACH_PORT_RIGHT_RECEIVE, -1);
+      assert_perror_backtrace (err);
       pi->port_right = MACH_PORT_NULL;
     }
 
diff --git a/libshouldbeinlibc/exec-reauth.c b/libshouldbeinlibc/exec-reauth.c
index b830f22..c04fe05 100644
--- a/libshouldbeinlibc/exec-reauth.c
+++ b/libshouldbeinlibc/exec-reauth.c
@@ -59,25 +59,29 @@ exec_reauth (auth_t auth, int secure, int must_reauth,
 	  if (!err)
 	    err = auth_user_authenticate (auth, ref, MACH_MSG_TYPE_MAKE_SEND,
 					  &newport);
-	  mach_port_mod_refs (mach_task_self (), ref, MACH_PORT_RIGHT_RECEIVE, -1);
-          /* If they gave us nothing, make sure not to proceed.  */
-          if (!err && newport == MACH_PORT_NULL)
-            err = KERN_INVALID_ARGUMENT;
+	  error_t refs_err = mach_port_mod_refs (mach_task_self (), ref,
+						 MACH_PORT_RIGHT_RECEIVE, -1);
+	  if (!err && refs_err)
+	    err = refs_err;
+
+	  /* If they gave us nothing, make sure not to proceed.  */
+	  if (!err && newport == MACH_PORT_NULL)
+	    err = KERN_INVALID_ARGUMENT;
 	  if (err)
-            return must_reauth ? err : 0;
-
-          if (isproc)
-            {
-              err = proc_reauthenticate_complete (newport);
-              if (err)
-                {
-                  mach_port_deallocate (mach_task_self (), newport);
-                  return must_reauth ? err : 0;
-                }
-            }
-
-          mach_port_deallocate (mach_task_self (), *port);
-          *port = newport;
+	    return must_reauth ? err : 0;
+
+	  if (isproc)
+	    {
+	      err = proc_reauthenticate_complete (newport);
+	      if (err)
+		{
+		  mach_port_deallocate (mach_task_self (), newport);
+		  return must_reauth ? err : 0;
+		}
+	    }
+
+	  mach_port_deallocate (mach_task_self (), *port);
+	  *port = newport;
 	}
       return 0;
     }
diff --git a/libshouldbeinlibc/portxlate.c b/libshouldbeinlibc/portxlate.c
index eb6abfb..2ff8b7a 100644
--- a/libshouldbeinlibc/portxlate.c
+++ b/libshouldbeinlibc/portxlate.c
@@ -30,55 +30,53 @@ port_name_xlator_create (mach_port_t from_task, mach_port_t to_task,
 			 struct port_name_xlator **xlator)
 {
   error_t err;
-  struct port_name_xlator *x = malloc (sizeof (struct port_name_xlator));
+  struct port_name_xlator *x = calloc (1, sizeof (struct port_name_xlator));
 
   if (! x)
-    return ENOMEM;
+    return errno;
+
+  err = mach_port_mod_refs (mach_task_self (), from_task,
+			    MACH_PORT_RIGHT_SEND, +1);
+  if (err)
+    goto error;
 
-  mach_port_mod_refs (mach_task_self (), from_task, MACH_PORT_RIGHT_SEND, +1);
   x->from_task = from_task;
-  mach_port_mod_refs (mach_task_self (), to_task, MACH_PORT_RIGHT_SEND, +1);
+  err = mach_port_mod_refs (mach_task_self (), to_task,
+			    MACH_PORT_RIGHT_SEND, +1);
+  if (err)
+    goto error;
+
   x->to_task = to_task;
-  x->to_names = 0;
-  x->to_types = 0;
-  x->to_names_len = 0;
-  x->to_types_len = 0;
 
   /* Cache a list of names in TO_TASK.  */
   err = mach_port_names (to_task,
 			 &x->to_names, &x->to_names_len,
 			 &x->to_types, &x->to_types_len);
 
-  if (! err)
-    /* Make an array to hold ports from TO_TASK which have been translated
-       into our namespace.  */
-    {
-      x->ports = malloc (sizeof (mach_port_t) * x->to_names_len);
-      if (x->ports)
-	{
-	  unsigned int i;
-	  for (i = 0; i < x->to_names_len; i++)
-	    x->ports[i] = MACH_PORT_NULL;
-	}
-      else
-	{
-	  munmap ((caddr_t) x->to_names,
-		  x->to_names_len * sizeof (mach_port_t));
-	  munmap ((caddr_t) x->to_types,
-		  x->to_types_len * sizeof (mach_port_type_t));
-
-	  mach_port_deallocate (mach_task_self (), x->to_task);
-	  mach_port_deallocate (mach_task_self (), x->from_task);
+  if (err)
+    goto error;
 
-	  err = ENOMEM;
-	}
+  /* Make an array to hold ports from TO_TASK which have been translated
+     into our namespace.  */
+  x->ports = calloc (1, sizeof (mach_port_t) * x->to_names_len);
+  if (!x->ports)
+    {
+      err = errno;
+      munmap ((caddr_t) x->to_names,
+	      x->to_names_len * sizeof (mach_port_t));
+      munmap ((caddr_t) x->to_types,
+	      x->to_types_len * sizeof (mach_port_type_t));
+
+      mach_port_deallocate (mach_task_self (), x->to_task);
+      mach_port_deallocate (mach_task_self (), x->from_task);
+      goto error;
     }
 
-  if (err)
-    free (x);
-  else
-    *xlator = x;
+  *xlator = x;
+  return 0;
 
+ error:
+  free (x);
   return err;
 }
 
diff --git a/libstore/file.c b/libstore/file.c
index 55f97d0..df7f295 100644
--- a/libstore/file.c
+++ b/libstore/file.c
@@ -211,8 +211,12 @@ file_map (const struct store *store, vm_prot_t prot, mach_port_t *memobj)
   else if (rd_memobj == wr_memobj)
     {
       if (rd_memobj != MACH_PORT_NULL)
-	mach_port_mod_refs (mach_task_self (), rd_memobj,
-			    MACH_PORT_RIGHT_SEND, -1);
+	{
+	  err = mach_port_mod_refs (mach_task_self (), rd_memobj,
+				    MACH_PORT_RIGHT_SEND, -1);
+	  if (err)
+	    mach_port_deallocate (mach_task_self (), rd_memobj);
+	}
     }
   else
     {
diff --git a/libstore/map.c b/libstore/map.c
index b7533cb..a7833f1 100644
--- a/libstore/map.c
+++ b/libstore/map.c
@@ -60,8 +60,12 @@ store_map (const struct store *store, vm_prot_t prot,
 	      if (rd_memobj == wr_memobj)
 		{
 		  if (rd_memobj != MACH_PORT_NULL)
-		    mach_port_mod_refs (mach_task_self (), rd_memobj,
-					MACH_PORT_RIGHT_SEND, -1);
+		    {
+		      err = mach_port_mod_refs (mach_task_self (), rd_memobj,
+						MACH_PORT_RIGHT_SEND, -1);
+		      if (err)
+			mach_port_deallocate (mach_task_self (), rd_memobj);
+		    }
 		}
 	      else
 		{
diff --git a/libstore/unknown.c b/libstore/unknown.c
index 624c184..058285c 100644
--- a/libstore/unknown.c
+++ b/libstore/unknown.c
@@ -60,10 +60,7 @@ noflags (struct store *store, int flags)
 static struct store_enc *
 duplicate_encoding (struct store_enc *enc)
 {
-  struct store_enc *us;
-  size_t i;
-
-  us = calloc (1, sizeof *us);
+  struct store_enc *us = calloc (1, sizeof *us);
   if (us == NULL)
     return NULL;
 
@@ -98,9 +95,14 @@ duplicate_encoding (struct store_enc *enc)
   us->num_offsets = enc->num_offsets;
   us->data_len = enc->data_len;
 
-  for (i = 0; i < us->num_ports; ++i)
-    mach_port_mod_refs (mach_task_self (), us->ports[i],
-			MACH_PORT_RIGHT_SEND, +1);
+  error_t err;
+  for (size_t i = 0; i < us->num_ports; ++i)
+    {
+      err = mach_port_mod_refs (mach_task_self (), us->ports[i],
+				MACH_PORT_RIGHT_SEND, +1);
+      if (err)
+	goto no_memory;
+    }
 
   return us;
 }
diff --git a/libtrivfs/dir-lookup.c b/libtrivfs/dir-lookup.c
index 0a62d49..dfa4beb 100644
--- a/libtrivfs/dir-lookup.c
+++ b/libtrivfs/dir-lookup.c
@@ -74,8 +74,15 @@ trivfs_S_dir_lookup (struct trivfs_protid *cred,
       if (err)
 	iohelp_free_iouser (user);
       else
-	mach_port_mod_refs (mach_task_self (), cred->realnode,
-			    MACH_PORT_RIGHT_SEND, +1);
+	{
+	  err = mach_port_mod_refs (mach_task_self (), cred->realnode,
+				    MACH_PORT_RIGHT_SEND, +1);
+	  if (err)
+	    {
+	      iohelp_free_iouser (user);
+	      ports_port_deref (newcred);
+	    }
+	}
     }
   if (err)
     return err;
diff --git a/libtrivfs/protid-dup.c b/libtrivfs/protid-dup.c
index 75f3ca8..d985564 100644
--- a/libtrivfs/protid-dup.c
+++ b/libtrivfs/protid-dup.c
@@ -41,14 +41,19 @@ trivfs_protid_dup (struct trivfs_protid *cred, struct trivfs_protid **dup)
 
       err = iohelp_dup_iouser (&new->user, cred->user);
       if (err)
-        {
+	{
 	  ports_port_deref (new);
 	  return err;
 	}
 
       new->realnode = cred->realnode;
-      mach_port_mod_refs (mach_task_self (), new->realnode,
-			  MACH_PORT_RIGHT_SEND, 1);
+      err = mach_port_mod_refs (mach_task_self (), new->realnode,
+				MACH_PORT_RIGHT_SEND, 1);
+      if (err)
+	{
+	  ports_port_deref (new);
+	  return err;
+	}
 
       new->hook = cred->hook;
 
diff --git a/proc/host.c b/proc/host.c
index adda74d..9d3f204 100644
--- a/proc/host.c
+++ b/proc/host.c
@@ -139,7 +139,6 @@ S_proc_getexecdata (struct proc *p,
 		    int **ints,
 		    mach_msg_type_number_t *nints)
 {
-  int i;
   int ports_allocated = 0;
   /* No need to check P here; we don't use it. */
 
@@ -151,28 +150,45 @@ S_proc_getexecdata (struct proc *p,
       *ports = mmap (0, round_page (n_std_ports * sizeof (mach_port_t)),
 		     PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
       if (*ports == MAP_FAILED)
-        return ENOMEM;
+        return errno;
       ports_allocated = 1;
     }
   memcpy (*ports, std_port_array, n_std_ports * sizeof (mach_port_t));
   *nports = n_std_ports;
 
+  error_t err;
+  int ints_allocated = 0;
   if (*nints < n_std_ints)
     {
       *ints = mmap (0, round_page (n_std_ints * sizeof (int)),
 		    PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
       if (*ints == MAP_FAILED)
 	{
+	  err = errno;
 	  if (ports_allocated)
 	    munmap (*ports, round_page (n_std_ports * sizeof (mach_port_t)));
-	  return ENOMEM;
+	  return err;
 	}
+
+      ints_allocated = 1;
     }
   memcpy (*ints, std_int_array, n_std_ints * sizeof (int));
   *nints = n_std_ints;
 
-  for (i = 0; i < n_std_ports; i++)
-    mach_port_mod_refs (mach_task_self (), std_port_array[i], MACH_PORT_RIGHT_SEND, 1);
+  for (int i = 0; i < n_std_ports; i++)
+    {
+      err = mach_port_mod_refs (mach_task_self (), std_port_array[i],
+				MACH_PORT_RIGHT_SEND, 1);
+      if (err)
+	{
+	  if (ports_allocated)
+	    munmap (*ports, round_page (n_std_ports * sizeof (mach_port_t)));
+	  if (ints_allocated)
+	    munmap (*ints, round_page (n_std_ints * sizeof (int)));
+	  return err;
+	}
+    }
+
   *portspoly = MACH_MSG_TYPE_MOVE_SEND;
 
   return 0;
diff --git a/proc/main.c b/proc/main.c
index 56ba3d3..e2c5f1a 100644
--- a/proc/main.c
+++ b/proc/main.c
@@ -216,7 +216,9 @@ main (int argc, char **argv, char **envp)
      to panic or something.  */
   open_console (_hurd_device_master);
 
-  mach_port_mod_refs (mach_task_self (), authserver, MACH_PORT_RIGHT_SEND, 1);
+  err = mach_port_mod_refs (mach_task_self (), authserver,
+			    MACH_PORT_RIGHT_SEND, 1);
+  assert_perror_backtrace (err);
   _hurd_port_set (&_hurd_ports[INIT_PORT_AUTH], authserver);
   mach_port_deallocate (mach_task_self (), boot);
 
diff --git a/proc/mgt.c b/proc/mgt.c
index 8223bf5..18ee1e5 100644
--- a/proc/mgt.c
+++ b/proc/mgt.c
@@ -283,8 +283,12 @@ S_proc_child (struct proc *parentp,
 
   if (MACH_PORT_VALID (parentp->p_task_namespace))
     {
-      mach_port_mod_refs (mach_task_self (), parentp->p_task_namespace,
-			  MACH_PORT_RIGHT_SEND, +1);
+      error_t err = mach_port_mod_refs (mach_task_self (),
+					parentp->p_task_namespace,
+					MACH_PORT_RIGHT_SEND, +1);
+      if (err)
+	return err;
+
       childp->p_task_namespace = parentp->p_task_namespace;
     }
 
@@ -1386,19 +1390,25 @@ S_mach_notify_new_task (struct port_info *notify,
   if (! parentp)
     return ESRCH;
 
+  error_t err;
   childp = task_find_nocreate (task);
   if (! childp)
     {
-      mach_port_mod_refs (mach_task_self (), task, MACH_PORT_RIGHT_SEND, +1);
+      err = mach_port_mod_refs (mach_task_self (), task,
+				MACH_PORT_RIGHT_SEND, +1);
+      if (err)
+	return err;
+
       childp = new_proc (task);
     }
 
   if (MACH_PORT_VALID (parentp->p_task_namespace))
     {
-      error_t err;
       /* Tasks in a task namespace are not expected to call
 	 proc_child, so we do it on their behalf.  */
-      mach_port_mod_refs (mach_task_self (), task, MACH_PORT_RIGHT_SEND, +1);
+      err = mach_port_mod_refs (mach_task_self (), task,
+				MACH_PORT_RIGHT_SEND, +1);
+      assert_perror_backtrace (err);
       err = S_proc_child (parentp, task);
       assert_perror_backtrace (err);
 
diff --git a/proc/pgrp.c b/proc/pgrp.c
index e2b497e..81129c2 100644
--- a/proc/pgrp.c
+++ b/proc/pgrp.c
@@ -82,8 +82,12 @@ static inline void
 free_session (struct session *s)
 {
   if (s->s_sessionid)
-    mach_port_mod_refs (mach_task_self (), s->s_sessionid,
-			MACH_PORT_RIGHT_RECEIVE, -1);
+    {
+      error_t err = mach_port_mod_refs (mach_task_self (), s->s_sessionid,
+					MACH_PORT_RIGHT_RECEIVE, -1);
+      assert_perror_backtrace (err);
+    }
+
   remove_session_from_hash (s);
   free (s);
 }
@@ -116,7 +120,17 @@ S_proc_setsid (struct proc *p)
   leave_pgrp (p);
 
   sess = new_session (p);
-  p->p_pgrp= new_pgrp (p->p_pid, sess);
+  if (!sess)
+    return errno;
+
+  p->p_pgrp = new_pgrp (p->p_pid, sess);
+  if (!p->p_pgrp)
+    {
+      error_t err = errno;
+      free (sess);
+      return err;
+    }
+
   join_pgrp (p);
 
   return 0;
@@ -129,6 +143,7 @@ boot_setsid (struct proc *p)
   struct session *sess;
 
   sess = new_session (p);
+  assert_backtrace (sess);
   p->p_pgrp = new_pgrp (p->p_pid, sess);
   assert_backtrace (p->p_pgrp);
   join_pgrp (p);
@@ -443,7 +458,16 @@ S_proc_setpgrp (struct proc *callerp,
 	 the last process in that group.  Likewise, if p->p_pgrp was
 	 the last group in p->p_pgrp->pg_session, the session is
 	 deallocated.  */
-      struct pgrp *new = pg ? pg : new_pgrp (pgid, p->p_pgrp->pg_session);
+      struct pgrp *new;
+      if (pg)
+	new = pg;
+      else
+	{
+	  new = new_pgrp (pgid, p->p_pgrp->pg_session);
+	  if (!new)
+	    return errno;
+	}
+
       leave_pgrp (p);
       p->p_pgrp = new;
       join_pgrp (p);
diff --git a/utils/rpctrace.c b/utils/rpctrace.c
index d9f4f07..d8dc0ca 100644
--- a/utils/rpctrace.c
+++ b/utils/rpctrace.c
@@ -677,8 +677,10 @@ rewrite_right (mach_port_t *right, mach_msg_type_name_t *type,
 	    }
 	  /* The receive right is owned by an unknown task. */
 	  receiver_info = new_receiver_info (*right, unknown_task);
-	  mach_port_mod_refs (mach_task_self (), *right,
-			      MACH_PORT_RIGHT_SEND, 1);
+	  err = mach_port_mod_refs (mach_task_self (), *right,
+				    MACH_PORT_RIGHT_SEND, 1);
+	  if (err)
+	    error (2, err, "mach_port_mod_refs");
 	}
       /* If the send right is moved to the task with the receive right,
        * don't do anything. 
-- 
2.43.0

Reply via email to