]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Update KillFilter to handle 'deleted' exe's.
authorDan Prince <dprince@redhat.com>
Thu, 1 Nov 2012 02:11:42 +0000 (22:11 -0400)
committerDan Prince <dprince@redhat.com>
Mon, 5 Nov 2012 20:24:39 +0000 (15:24 -0500)
Updates KillFilter so that it handles the case where the executable
linked to by /proc/PID/exe is updated or deleted.

Includes new test case with coverage for this fix as well.

Fixes LP Bug #1073768

Change-Id: If568693e0b491f8806ff0156701766929ca0b3e0

quantum/rootwrap/filters.py
quantum/tests/unit/test_rootwrap.py

index 2e0e12c9a3cc8147267c6d78fca1fd004467f476..06b38e1c0213a76ca18ea877d3db6c8dc9b7e6a0 100644 (file)
@@ -158,6 +158,10 @@ class KillFilter(CommandFilter):
 
         try:
             command = os.readlink("/proc/%d/exe" % int(args[1]))
+            # NOTE(dprince): /proc/PID/exe may have ' (deleted)' on
+            # the end if an executable is updated or deleted
+            if command.endswith(" (deleted)"):
+                command = command[:command.rindex(" ")]
             if command != self.args[0]:
                 # Affected executable doesn't match
                 return False
index 427c7da137c2781f4319e011afc5f55b5ceeeba1..1c5b696391328a097fd245dc55bf8958014653f2 100644 (file)
@@ -17,6 +17,7 @@
 import os
 import subprocess
 
+import mock
 import unittest2 as unittest
 
 from quantum.common import utils
@@ -35,6 +36,9 @@ class RootwrapTestCase(unittest.TestCase):
             filters.CommandFilter("/nonexistant/cat", "root"),
             filters.CommandFilter("/bin/cat", "root")]  # Keep this one last
 
+    def tearDown(self):
+        super(RootwrapTestCase, self).tearDown()
+
     def test_RegExpFilter_match(self):
         usercmd = ["ls", "/root"]
         filtermatch = wrapper.match_filter(self.filters, usercmd)
@@ -112,6 +116,16 @@ class RootwrapTestCase(unittest.TestCase):
         usercmd = ['kill', 'notapid']
         self.assertFalse(f.match(usercmd))
 
+    def test_KillFilter_deleted_exe(self):
+        """Makes sure deleted exe's are killed correctly"""
+        # See bug #1073768.
+        with mock.patch('os.readlink') as mock_readlink:
+            mock_readlink.return_value = '/bin/commandddddd (deleted)'
+            f = filters.KillFilter("root", "/bin/commandddddd")
+            usercmd = ['kill', 1234]
+            self.assertTrue(f.match(usercmd))
+            mock_readlink.assert_called_once_with("/proc/1234/exe")
+
     def test_ReadFileFilter(self):
         goodfn = '/good/file.name'
         f = filters.ReadFileFilter(goodfn)