[1/3] crit: Add anonymize option.

Submitted by Harshavardhan Unnibhavi on Sept. 20, 2019, 8:15 a.m.

Details

Message ID 20190920081522.9591-2-hvubfoss@gmail.com
State New
Series "Issue 360: Anonymize image files"
Headers show

Commit Message

Harshavardhan Unnibhavi Sept. 20, 2019, 8:15 a.m.
When reporting a bug, it may not be acceptable for the reporter to send
raw images, as they may contain sensitive data. Hence, we need to teach
crit to "anonymize" images for publication.

Resolve Issue #360.

Signed-off-by: Harshavardhan Unnibhavi <hvubfoss@gmail.com>
---
 lib/py/cli.py | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

Patch hide | download patch | download mbox

diff --git a/lib/py/cli.py b/lib/py/cli.py
index da343022..980d7fcc 100755
--- a/lib/py/cli.py
+++ b/lib/py/cli.py
@@ -3,6 +3,7 @@  import argparse
 import sys
 import json
 import os
+import glob
 
 import pycriu
 
@@ -329,6 +330,30 @@  def explore_rss(opts):
             print('%-24s%s' % (pstr, vstr))
 
 
+def anonymize(opts):
+    try:
+        os.makedirs(opts['out'])
+    except OSError as err:
+        if err.errno != os.errno.EEXIST:
+            raise
+
+    img_files = glob.glob(os.path.join(opts['in'], '*.img'))
+
+    for i in img_files:
+        inf_opts = {
+            'in': i,
+            'out': os.path.join(
+                opts['out'],
+                os.path.basename(i))}
+
+        try:
+            img = pycriu.images.load(inf(inf_opts))
+        except pycriu.images.MagicException as exc:
+            print("Unknown magic %#x.\n"
+                "Found a raw image" % exc.magic, file=sys.stderr)
+            print("Skipping over %s, continuing ..." % os.path.basename(i))
+
+
 explorers = {
     'ps': explore_ps,
     'fds': explore_fds,
@@ -400,6 +425,19 @@  def main():
                              action='store_true')
     show_parser.set_defaults(func=decode, pretty=True, out=None)
 
+    # Anonymize
+    anon_parser = subparsers.add_parser(
+        'anonymize', help="anonymize binary images to remove sensitive information")
+    anon_parser.add_argument(
+        '-i',
+        '--in',
+        help="path to directory containing images to be anonymized")
+    anon_parser.add_argument(
+        '-o',
+        '--out',
+        help="path to directory where the anonymized images are to be stored")
+    anon_parser.set_defaults(func=anonymize)
+
     opts = vars(parser.parse_args())
 
     if not opts: