Skip to contentSkip to conversation

Fix hash server passwords leaking in logs

PATCH New Joshua Watt 1 participant 3 messages Nov 26, 2024

lore
Share & tools
lore
#0 lore raw

cover Fix hash server passwords leaking in logs

bitbake prints out function arguments when an exception occurs to aid in debugging. However, several places in the hash equivalence code that pass a password as an argument which means that an exception in one of these code paths would cause the password to be printed out in plain text.

Fix this by implementing a mechanism that allows functions to opt out of having their arguments printed in backtraces and set the flag for the affected hash equivalence code.

Joshua Watt (2):
exceptions: Add option to hide frame arguments in exceptions
hashserv: Do not print passwords in exceptions

 bitbake/lib/bb/exceptions.py       | 24 +++++++++++-------------
 bitbake/lib/hashserv/__init__.py   |  6 ++++++
 bitbake/lib/hashserv/client.py     |  6 ++++++
 bitbake/lib/hashserv/server.py     |  2 ++
 bitbake/lib/hashserv/sqlalchemy.py |  2 ++
 5 files changed, 27 insertions(+), 13 deletions(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH 1/2 exceptions: Add option to hide frame arguments in exceptions

The exception code in bitbake prints out the arguments to functions calls, and while this is very useful, it means that if there are secret credentials in arguments (like the password for bitbake hashserver client), they will be shown in the logs and potentially leaked.

To prevent this, add code that checks for the local variable HIDE_FRAME_ARGS in the function. If present and set to True, the exception code will not print the value of any of the function arguments.

---
 bitbake/lib/bb/exceptions.py | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)
bitbake/lib/bb/exceptions.py +11 -13
index 60643bd6421..931140da0c6 100644
@@ -36,26 +36,24 @@ class TracebackEntry(namedtuple.abc):
 
 def _get_frame_args(frame):
     """Get the formatted arguments and class (if available) for a frame"""
-    arginfo = inspect.getargvalues(frame)
+    args, varargs, keywords, local = inspect.getargvalues(frame)
+    if not args:
+        return '', None
 
-    if not arginfo.args:
-            return '', None
-
-    firstarg = arginfo.args[0]
+    firstarg = args[0]
     if firstarg == 'self':
-        self = arginfo.locals['self']
+        self = local['self']
         cls = self.__class__.__name__
 
-        arginfo.args.pop(0)
-        try:
-            del arginfo.locals['self']
-        except TypeError:
-            # FIXME - python 3.13 FrameLocalsProxy can't be modified
-            pass
+        args.pop(0)
+        del local['self']
     else:
         cls = None
 
-    formatted = inspect.formatargvalues(*arginfo)
+    if local.get("HIDE_FRAME_ARGS", False):
+        return '(HIDDEN)', cls
+
+    formatted = inspect.formatargvalues(args, varargs, keywords, local)
     return formatted, cls
 
 def extract_traceback(tb, context=1):
Signature · 1 line
2.47.1
#2 lore raw

PATCH 2/2 hashserv: Do not print passwords in exceptions

In reply to Joshua Watt

If an exception occurred in some of the function calls in the hash server code, passwords could be leaked because bitbake tries to print function arguments in its backtrace.

Add the flag that prevents bitbake from showing argument values in these functions to prevent the password from leaking

---
 bitbake/lib/hashserv/__init__.py   | 6 ++++++
 bitbake/lib/hashserv/client.py     | 6 ++++++
 bitbake/lib/hashserv/server.py     | 2 ++
 bitbake/lib/hashserv/sqlalchemy.py | 2 ++
 4 files changed, 16 insertions(+)
bitbake/lib/hashserv/__init__.py +6 -0
index ac891e0174d..d2471fe7858 100644
@@ -28,6 +28,8 @@ def create_server(
     admin_password=None,
     reuseport=False,
 ):
+    HIDE_FRAME_ARGS = True  # Do not leak password on exception
+
     def sqlite_engine():
         from .sqlite import DatabaseEngine
 
@@ -70,6 +72,8 @@ def create_server(
 
 
 def create_client(addr, username=None, password=None):
+    HIDE_FRAME_ARGS = True  # Do not leak password on exception
+
     from . import client
 
     c = client.Client(username, password)
@@ -89,6 +93,8 @@ def create_client(addr, username=None, password=None):
 
 
 async def create_async_client(addr, username=None, password=None):
+    HIDE_FRAME_ARGS = True  # Do not leak password on exception
+
     from . import client
 
     c = client.AsyncClient(username, password)
bitbake/lib/hashserv/client.py +6 -0
index a510f3284fc..13a6e62e7fe 100644
@@ -80,6 +80,8 @@ class AsyncClient(bb.asyncrpc.AsyncClient):
     MODE_EXIST_STREAM = 2
 
     def __init__(self, username=None, password=None):
+        HIDE_FRAME_ARGS = True  # Do not leak password on exception
+
         super().__init__("OEHASHEQUIV", "1.1", logger)
         self.mode = self.MODE_NORMAL
         self.username = username
@@ -236,6 +238,8 @@ class AsyncClient(bb.asyncrpc.AsyncClient):
         return await self.invoke({"clean-unused": {"max_age_seconds": max_age}})
 
     async def auth(self, username, token):
+        HIDE_FRAME_ARGS = True  # Do not leak token on exception
+
         result = await self.invoke({"auth": {"username": username, "token": token}})
         self.username = username
         self.password = token
@@ -319,6 +323,8 @@ class AsyncClient(bb.asyncrpc.AsyncClient):
 
 class Client(bb.asyncrpc.Client):
     def __init__(self, username=None, password=None):
+        HIDE_FRAME_ARGS = True  # Do not leak password on exception
+
         self.username = username
         self.password = password
 
bitbake/lib/hashserv/server.py +2 -0
index 68f64f983b2..61ddeaad80b 100644
@@ -793,6 +793,8 @@ class Server(bb.asyncrpc.AsyncServer):
         admin_username=None,
         admin_password=None,
     ):
+        HIDE_FRAME_ARGS = True  # Do not leak password on exception
+
         if upstream and read_only:
             raise bb.asyncrpc.ServerError(
                 "Read-only hashserv cannot pull from an upstream server"
bitbake/lib/hashserv/sqlalchemy.py +2 -0
index f7b0226a7a3..54aec55b24b 100644
@@ -115,6 +115,8 @@ class UnihashesV2(DeprecatedBase):
 
 class DatabaseEngine(object):
     def __init__(self, url, username=None, password=None):
+        HIDE_FRAME_ARGS = True  # Do not leak password on exception
+
         self.logger = logging.getLogger("hashserv.sqlalchemy")
         self.url = sqlalchemy.engine.make_url(url)
 
Signature · 1 line
2.47.1