VAITP Dataset

← Back to the dataset

CVE-2022-23651

Local race condition in b2-sdk-python <= 1.14.0 exposes API keys

  • CVSS 4.7
  • CWE-367 Time-of-check Time-of-use (TOCTOU) Race Condition
  • Race Conditions
  • Local

b2-sdk-python is a python library to access cloud storage provided by backblaze. Linux and Mac releases of the SDK version 1.14.0 and below contain a key disclosure vulnerability that, in certain conditions, can be exploited by local attackers through a time-of-check-time-of-use (TOCTOU) race condition. SDK users of the SqliteAccountInfo format are vulnerable while users of the InMemoryAccountInfo format are safe. The SqliteAccountInfo saves API keys (and bucket name-to-id mapping) in a local database file ($XDG_CONFIG_HOME/b2/account_info, ~/.b2_account_info or a user-defined path). When first created, the file is world readable and is (typically a few milliseconds) later altered to be private to the user. If the directory containing the file is readable by a local attacker then during the brief period between file creation and permission modification, a local attacker can race to open the file and maintain a handle to it. This allows the local attacker to read the contents after the file after the sensitive information has been saved to it. Consumers of this SDK who rely on it to save data using SqliteAccountInfo class should upgrade to the latest version of the SDK. Those who believe a local user might have opened a handle using this race condition, should remove the affected database files and regenerate all application keys. Users should upgrade to b2-sdk-python 1.14.1 or later.

CVSS base score
4.7
Published
2022-02-23
OWASP
A08 Software and Data Integrity Failures
Orthogonal defect classification
Timing/Serialization
Code defect classification
Timing Issues
Category
Race Conditions
Subcategory
Race Condition in File Operations
Accessibility scope
Local
Impact
Information Disclosure
Fixed by upgrading
Yes

Solution

Upgrade to b2-sdk-python 1.14.1 or later.

Vulnerable code sample

import os
import sqlite3

class AccountInfo:
    def __init__(self, db_path):
        self.db_path = db_path
        self.create_database()

    def create_database(self):
        with open(self.db_path, 'w') as db_file:
            pass

        self.connection = sqlite3.connect(self.db_path)
        self.cursor = self.connection.cursor()
        self.cursor.execute('CREATE TABLE IF NOT EXISTS account_info (key TEXT, value TEXT)')

    def store_key(self, key, value):
        self.cursor.execute('INSERT INTO account_info (key, value) VALUES (?, ?)', (key, value))
        self.connection.commit()

    def close(self):
        self.connection.close()

db_path = os.path.expanduser('~/.b2_account_info')
account_info = AccountInfo(db_path)
account_info.store_key('api_key', 'your_api_key_here')
account_info.close()

Patched code sample

import os
import sqlite3
import stat
import time

class AccountInfo:
    def __init__(self, db_path):
        self.db_path = db_path
        self.create_database()

    def create_database(self):
        with open(self.db_path, 'w') as db_file:
            pass

        time.sleep(0.1)

        os.chmod(self.db_path, stat.S_IRUSR | stat.S_IWUSR)

        self.connection = sqlite3.connect(self.db_path)
        self.cursor = self.connection.cursor()
        self.cursor.execute('CREATE TABLE IF NOT EXISTS account_info (key TEXT, value TEXT)')

    def store_key(self, key, value):
        self.cursor.execute('INSERT INTO account_info (key, value) VALUES (?, ?)', (key, value))
        self.connection.commit()

    def close(self):
        self.connection.close()

db_path = os.path.expanduser('~/.b2_account_info')
account_info = AccountInfo(db_path)
account_info.store_key('api_key', 'your_api_key_here')
account_info.close()

Cite this entry

@misc{vaitp:cve202223651,
  title        = {{Local race condition in b2-sdk-python <= 1.14.0 exposes API keys}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2022},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2022-23651},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2022-23651/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.

The supreme art of war is to subdue the enemy without fighting.

Sun Tzu – “The Art of War”

:: Shaping the future through research and ingenuity ::