public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin] Cygwin: kill(1): disallow killing process using raw Windows PID
Date: Sat, 02 Feb 2019 14:01:00 -0000	[thread overview]
Message-ID: <20190202140121.122606.qmail@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=8de660271fe75a6993f1c9888d24b824bb7f999d

commit 8de660271fe75a6993f1c9888d24b824bb7f999d
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Sat Feb 2 15:00:39 2019 +0100

    Cygwin: kill(1): disallow killing process using raw Windows PID
    
    This may end up killing the wrong process.  Only allow Cygwin PID.
    
    Slightly clean up code: Remove outdated W95 considerations.  Fix
    a bug in commandline argument processing.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/utils/kill.cc | 41 ++++++++++++++++++++++-------------------
 1 file changed, 22 insertions(+), 19 deletions(-)

diff --git a/winsup/utils/kill.cc b/winsup/utils/kill.cc
index c6adaa2..19c19d0 100644
--- a/winsup/utils/kill.cc
+++ b/winsup/utils/kill.cc
@@ -152,29 +152,31 @@ get_debug_priv (void)
 }
 
 static void __stdcall
-forcekill (int pid, int sig, int wait)
+forcekill (pid_t pid, int sig, int wait)
 {
-  // try to acquire SeDebugPrivilege
+  /* try to acquire SeDebugPrivilege */
   get_debug_priv();
 
   external_pinfo *p = NULL;
-  /* cygwin_internal misinterprets negative pids (Win9x pids) */
-  if (pid > 0)
-    p = (external_pinfo *) cygwin_internal (CW_GETPINFO_FULL, pid);
-  DWORD dwpid = p ? p->dwProcessId : (DWORD) pid;
-  HANDLE h = OpenProcess (PROCESS_TERMINATE, FALSE, (DWORD) dwpid);
+  p = (external_pinfo *) cygwin_internal (CW_GETPINFO_FULL, pid);
+  if (!p)
+    {
+      fprintf (stderr, "%s: %d: No such process\n", prog_name, pid);
+      return;
+    }
+  DWORD dwpid = p->dwProcessId;
+  HANDLE h = OpenProcess (PROCESS_TERMINATE, FALSE, dwpid);
   if (!h)
     {
       if (!wait || GetLastError () != ERROR_INVALID_PARAMETER)
-	fprintf (stderr, "%s: couldn't open pid %u\n",
-		 prog_name, (unsigned) dwpid);
+	fprintf (stderr, "%s: couldn't open pid %u\n", prog_name, dwpid);
       return;
     }
   if (!wait || WaitForSingleObject (h, 200) != WAIT_OBJECT_0)
     if (sig && !TerminateProcess (h, sig << 8)
 	&& WaitForSingleObject (h, 200) != WAIT_OBJECT_0)
       fprintf (stderr, "%s: couldn't kill pid %u, %u\n",
-	       prog_name, (unsigned) dwpid, (unsigned int) GetLastError ());
+	       prog_name, dwpid, GetLastError ());
   CloseHandle (h);
 }
 
@@ -232,11 +234,8 @@ main (int argc, char **argv)
 	  print_version ();
 	  break;
 	case '?':
-	  if (gotasig)
-	    {
-	      --optind;
-	      goto out;
-	    }
+	  if (gotasig) /* this is a negative pid, go ahead */
+	    goto out;
 	  optreset = 1;
 	  optind = 1 + av - argv;
 	  gotasig = *av + 1;
@@ -252,18 +251,22 @@ out:
   test_for_unknown_sig (sig, gotasig);
 
   argv += optind;
+  if (*argv == 0)
+    {
+      fprintf (stderr, "%s: not enough arguments\n", prog_name);
+      return 1;
+    }
   while (*argv != NULL)
     {
       if (!pid)
 	pid = strtoll (*argv, &p, 10);
-      if (*p != '\0'
-	  || (!force && (pid < INT_MIN || pid > INT_MAX))
-	  || (force && (pid <= 0 || pid > UINT_MAX)))
+      /* INT_MIN <= pid <= INT_MAX.  -f only takes positive pids. */
+      if (*p != '\0' || pid < (force ? 1 : INT_MIN) || pid > INT_MAX)
 	{
 	  fprintf (stderr, "%s: illegal pid: %s\n", prog_name, *argv);
 	  ret = 1;
 	}
-      else if (pid <= INT_MAX && kill ((pid_t) pid, sig) == 0)
+      else if (kill ((pid_t) pid, sig) == 0)
 	{
 	  if (force)
 	    forcekill ((pid_t) pid, sig, 1);


                 reply	other threads:[~2019-02-02 14:01 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20190202140121.122606.qmail@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=cygwin-cvs@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).