samba-tool: replace use of os.popen
authorUri Simchoni <uri@samba.org>
Sun, 15 Nov 2015 11:34:03 +0000 (13:34 +0200)
committerUri Simchoni <uri@samba.org>
Mon, 23 Nov 2015 21:19:34 +0000 (22:19 +0100)
The netcmd/domain.py module uses os.popen() on user-supplied
parameters. This opens up the way to code injection.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=11601

Signed-off-by: Uri Simchoni <uri@samba.org>
Reviewed-by: Alexander Bokovoy <ab@samba.org>
Autobuild-User(master): Uri Simchoni <uri@samba.org>
Autobuild-Date(master): Mon Nov 23 22:19:34 CET 2015 on sn-devel-104

python/samba/netcmd/domain.py

index 6726538fc1b8f0fa26c2455b0840b273be69df66..62f8bfa8153cf90919e7aa9d08ddc286a4f1381e 100644 (file)
@@ -31,6 +31,7 @@ import ctypes
 import random
 import tempfile
 import logging
+import subprocess
 from getpass import getpass
 from samba.net import Net, LIBNET_JOIN_AUTOMATIC
 import samba.ntacls
@@ -87,9 +88,16 @@ from samba.provision.common import (
 )
 
 def get_testparm_var(testparm, smbconf, varname):
-    cmd = "%s -s -l --parameter-name='%s' %s 2>/dev/null" % (testparm, varname, smbconf)
-    output = os.popen(cmd, 'r').readline()
-    return output.strip()
+    errfile = open(os.devnull, 'w')
+    p = subprocess.Popen([testparm, '-s', '-l',
+                          '--parameter-name=%s' % varname, smbconf],
+                         stdout=subprocess.PIPE, stderr=errfile)
+    (out,err) = p.communicate()
+    errfile.close()
+    lines = out.split('\n')
+    if lines:
+        return lines[0].strip()
+    return ""
 
 try:
    import samba.dckeytab