Skip to contentSkip to conversation

patch.bbclass: increase security

PATCH Applied Constantin Musca 3 participants 3 messages Sep 14, 2012 – Sep 16, 2012

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 patch.bbclass: increase security

- Use mkdtemp for generating temp dir names
- Use bb.utils.remove for removing temp dirs
- Add comment for explaining the "patch" workaround

[YOCTO #3070]

---
 meta/classes/patch.bbclass |   16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
meta/classes/patch.bbclass +8 -8
index d010438..ed12802 100644
@@ -139,11 +139,11 @@ python patch_do_patch() {
     path = os.getenv('PATH')
     os.putenv('PATH', d.getVar('PATH', True))
 
-    import shutil
-    process_tmpdir = os.path.join('/tmp', str(os.getpid()))
-    if os.path.exists(process_tmpdir):
-        shutil.rmtree(process_tmpdir)
-    os.makedirs(process_tmpdir)
+    # We must use one TMPDIR per process so that the "patch" processes
+    # don't generate the same temp file name.
+
+    import tempfile
+    process_tmpdir = tempfile.mkdtemp()
     os.environ['TMPDIR'] = process_tmpdir
 
     for patch in src_patches(d):
@@ -168,15 +168,15 @@ python patch_do_patch() {
         try:
             patchset.Import({"file":local, "strippath": parm['striplevel']}, True)
         except Exception as exc:
-            shutil.rmtree(process_tmpdir)
+            bb.utils.remove(process_tmpdir, True)
             bb.fatal(str(exc))
         try:
             resolver.Resolve()
         except bb.BBHandledException as e:
-            shutil.rmtree(process_tmpdir)
+            bb.utils.remove(process_tmpdir, True)
             bb.fatal(str(e))
 
-    shutil.rmtree(process_tmpdir)
+    bb.utils.remove(process_tmpdir, True)
 }
 patch_do_patch[vardepsexclude] = "PATCHRESOLVE"
 
Signature · 1 line
1.7.9.5
Applied
#1 lore raw
Quoted 42 lines · Constantin Musca

- Use mkdtemp for generating temp dir names
- Use bb.utils.remove for removing temp dirs
- Add comment for explaining the "patch" workaround

[YOCTO #3070]

---
  meta/classes/patch.bbclass |   16 ++++++++--------
  1 file changed, 8 insertions(+), 8 deletions(-)
meta/classes/patch.bbclass +6 -6
index d010438..ed12802 100644
@@ -139,11 +139,11 @@ python patch_do_patch() {
      path = os.getenv('PATH')
      os.putenv('PATH', d.getVar('PATH', True))
 
-    import shutil
-    process_tmpdir = os.path.join('/tmp', str(os.getpid()))
-    if os.path.exists(process_tmpdir):
-        shutil.rmtree(process_tmpdir)
-    os.makedirs(process_tmpdir)
+    # We must use one TMPDIR per process so that the "patch" processes
+    # don't generate the same temp file name.
+
+    import tempfile
+    process_tmpdir = tempfile.mkdtemp()
      os.environ['TMPDIR'] = process_tmpdir
 
      for patch in src_patches(d):
@@ -168,15 +168,15 @@ python patch_do_patch() {
          try:
              patchset.Import({"file":local, "strippath": parm['striplevel']}, True)
          except Exception as exc:
-            shutil.rmtree(process_tmpdir)
+            bb.utils.remove(process_tmpdir, True)
              bb.fatal(str(exc))
          try:
              resolver.Resolve()

meta/classes/patch.bbclass python patch_do_patch() { · line 182

Quoted 10 lines
meta/classes/patch.bbclass +2 -2
          except bb.BBHandledException as e:
-            shutil.rmtree(process_tmpdir)
+            bb.utils.remove(process_tmpdir, True)
              bb.fatal(str(e))
 
-    shutil.rmtree(process_tmpdir)
+    bb.utils.remove(process_tmpdir, True)
  }
  patch_do_patch[vardepsexclude] = "PATCHRESOLVE"
 

Merged into OE-Core

Thanks
Sau!

#2 lore raw
In reply to Constantin Musca

Review

Quoted 2 lines · Constantin Musca
(quoted diff) +1 -1
-            shutil.rmtree(process_tmpdir)
+            bb.utils.remove(process_tmpdir, True)

In the future, I'd suggest leveraging the ability to pass arguments by name to clarify the meaning of the boolean argument:

bb.utils.remove(process_tmpdir, recurse=True).

This makes it a bit more clear when reading the code in the future.

Signature · 1 line
Christopher Larson