From 7d8be1c1ffc31523e2de09d4829169b78dd391e4 Mon Sep 17 00:00:00 2001 From: Antoine Jacoutot Date: Fri, 17 May 2019 11:55:16 +0000 Subject: [PATCH] pid_get_parent: fix potential leak of kp --- gtk/gtkmountoperation-x11.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/gtk/gtkmountoperation-x11.c b/gtk/gtkmountoperation-x11.c index 1b7bff6aa1..268354f31b 100644 --- a/gtk/gtkmountoperation-x11.c +++ b/gtk/gtkmountoperation-x11.c @@ -736,25 +736,32 @@ pid_get_command_line (GPid pid) static GPid pid_get_parent (GPid pid) { - struct kinfo_proc *kp; + struct kinfo_proc *kp = NULL; size_t len; - GPid ppid; + GPid ppid = 0; + + /* fail if trying to get the parent of the init process (no such thing) */ + if (pid == 1) + goto out; int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid, sizeof(struct kinfo_proc), 0 }; if (sysctl (mib, G_N_ELEMENTS (mib), NULL, &len, NULL, 0) == -1) - return (-1); + goto out; + mib[5] = (len / sizeof(struct kinfo_proc)); kp = g_malloc0 (len); if (sysctl (mib, G_N_ELEMENTS (mib), kp, &len, NULL, 0) < 0) - return -1; + goto out; ppid = kp->p_ppid; - g_free (kp); +out: + if (kp) + g_free (kp); return ppid; }