Skip to main content

Securely clear secrets from memory. Built on stable Rust primitives which guarantee memory is zeroed using an operation will not be 'optimized away' by the compiler.

Project description

zeroize

PyPI version CI

Securely clear secrets from memory. Built on stable Rust primitives which guarantee memory is zeroed using an operation will not be 'optimized away' by the compiler.

It uses zeroize crate under the hood to zeroize and memsec for mlock() and munlock(). Maximum you can mlock is 2662 KB.
It can work with bytearray and numpy array.

[!WARNING]
In the case of Copy-on-write fork you need to zeroize the memory before forking the child process, see example below.
Also by itself it doesn't work if memory is moved or moved to swap. You can use zeroize.mlock() to lock the memory, see example below.

Caveats of mlock()

mlock works on pages, so two variables could reside in the same page and if you munlock one it will munlock the whole page and also the memory for the other variable. Ideally you could munlock all your vars at same time so it would not be affected by the overlap. One strategy could be to expire your vars that store credentials when not used and to reload them again when needed. Like that you could mlock when you load them and munlock on expire and keep all vars under the same expire policy. Like this all var will be munlocked at the same time.

Examples

On Windows you can mlock up to 128 KB by default. If you need more you need to first call SetProcessWorkingSetSize to increase the dwMinimumWorkingSetSize. Will have an example below.

Lock and zeroize memory

from zeroize import zeroize1, mlock, munlock
import numpy as np


if __name__ == "__main__":
    try:
        print("allocate memory")

        # regular array
        # Maximum you can mlock is 2662 KB
        arr = bytearray(b"1234567890")

        # numpy array
        # Maximum you can mlock is 2662 KB
        arr_np = np.array([0] * 10, dtype=np.uint8)
        arr_np[:] = arr
        assert arr_np.tobytes() == b"1234567890"

        print("locking memory")

        mlock(arr)
        mlock(arr_np)

        print("zeroize'ing...: ")
        zeroize1(arr)
        zeroize1(arr_np)

        print("checking if is zeroized")
        assert arr == bytearray(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
        assert all(arr_np == 0)

        print("all good, bye!")

    finally:
        # Unlock the memory
        print("unlocking memory")
        munlock(arr)
        munlock(arr_np)

Zeroing memory before forking child process

This mitigates the problems that appears on Copy-on-write fork. You need to zeroize the data before forking the child process.

import os
from zeroize import zeroize1, mlock, munlock


if __name__ == "__main__":
    try:
        # Maximum you can mlock is 2662 KB
        sensitive_data = bytearray(b"Sensitive Information")
        mlock(sensitive_data)

        print("Before zeroization:", sensitive_data)

        zeroize1(sensitive_data)
        print("After zeroization:", sensitive_data)

        # Forking after zeroization to ensure no sensitive data is copied
        pid = os.fork()
        if pid == 0:
            # This is the child process
            print("Child process memory after fork:", sensitive_data)
        else:
            # This is the parent process
            os.wait()  # Wait for the child process to exit
        
        print("all good, bye!")

    finally:
        # Unlock the memory
        print("unlocking memory")
        munlock(sensitive_data)

Locking more than 128 KB

On Windows if you need to mlock more than 128 KB you need to first call SetProcessWorkingSetSize to increase the dwMinimumWorkingSetSize.

Here is an example, set min_size to the size you want to mlock + some overhead.

import platform


def setup_memory_limit():
    if not platform.system() == "Windows":
        return

    import ctypes
    from ctypes import wintypes

    # Define the Windows API functions
    kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)

    GetCurrentProcess = kernel32.GetCurrentProcess
    GetCurrentProcess.restype = wintypes.HANDLE

    SetProcessWorkingSetSize = kernel32.SetProcessWorkingSetSize
    SetProcessWorkingSetSize.restype = wintypes.BOOL
    SetProcessWorkingSetSize.argtypes = [wintypes.HANDLE, ctypes.c_size_t, ctypes.c_size_t]

    # Get the handle of the current process
    current_process = GetCurrentProcess()

    # Set the working set size
    min_size = 6 * 1024 * 1024  # Minimum working set size
    max_size = 10 * 1024 * 1024  # Maximum working set size

    result = SetProcessWorkingSetSize(current_process, min_size, max_size)

    if not result:
        error_code = ctypes.get_last_error()
        error_message = ctypes.FormatError(error_code)
        raise RuntimeError(f"SetProcessWorkingSetSize failed with error code {error_code}: {error_message}")

# Call this before you mlock
setup_memory_limit()

Build from source

Browser

Open in Gitpod

Open in Codespaces

Geting sources from GitHub

Skip this if you're starting it in browser.

git clone https://github.com/radumarias/zeroize-python && cd zeroize-python

Compile and run

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

To configure your current shell, you need to source the corresponding env file under $HOME/.cargo. This is usually done by running one of the following (note the leading DOT):

. "$HOME/.cargo/env"
python -m venv .env
source .env/bin/activate
pip install -r requirements.txt
maturin develop
pytest
python examples/lock_and_zeroize.py
python examples/zeroize_before_fork.py
python examples/mlock.py

Contribute

Feel free to fork it, change and use it in any way that you want. If you build something interesting and feel like sharing pull requests are always appreciated.

How to contribute

Please see CONTRIBUTING.md.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zeroize-1.1.2.tar.gz (17.7 kB view hashes)

Uploaded Source

Built Distributions

zeroize-1.1.2-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (209.7 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

zeroize-1.1.2-pp310-pypy310_pp73-manylinux_2_17_s390x.manylinux2014_s390x.whl (279.2 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ s390x

zeroize-1.1.2-pp310-pypy310_pp73-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (228.0 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ppc64le

zeroize-1.1.2-pp310-pypy310_pp73-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (217.3 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARMv7l

zeroize-1.1.2-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (215.0 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

zeroize-1.1.2-pp310-pypy310_pp73-manylinux_2_5_i686.manylinux1_i686.whl (214.8 kB view hashes)

Uploaded PyPy manylinux: glibc 2.5+ i686

zeroize-1.1.2-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (209.8 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

zeroize-1.1.2-pp39-pypy39_pp73-manylinux_2_17_s390x.manylinux2014_s390x.whl (279.1 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ s390x

zeroize-1.1.2-pp39-pypy39_pp73-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (227.9 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ppc64le

zeroize-1.1.2-pp39-pypy39_pp73-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (217.3 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARMv7l

zeroize-1.1.2-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (215.1 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

zeroize-1.1.2-pp39-pypy39_pp73-manylinux_2_5_i686.manylinux1_i686.whl (214.8 kB view hashes)

Uploaded PyPy manylinux: glibc 2.5+ i686

zeroize-1.1.2-pp38-pypy38_pp73-manylinux_2_17_s390x.manylinux2014_s390x.whl (279.1 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ s390x

zeroize-1.1.2-pp38-pypy38_pp73-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (227.9 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ppc64le

zeroize-1.1.2-pp38-pypy38_pp73-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (217.2 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARMv7l

zeroize-1.1.2-pp38-pypy38_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (215.0 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

zeroize-1.1.2-pp37-pypy37_pp73-manylinux_2_17_s390x.manylinux2014_s390x.whl (281.1 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ s390x

zeroize-1.1.2-pp37-pypy37_pp73-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (229.2 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ppc64le

zeroize-1.1.2-pp37-pypy37_pp73-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (218.6 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARMv7l

zeroize-1.1.2-pp37-pypy37_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (216.5 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

zeroize-1.1.2-cp312-none-win_amd64.whl (100.7 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

zeroize-1.1.2-cp312-none-win32.whl (96.3 kB view hashes)

Uploaded CPython 3.12 Windows x86

zeroize-1.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (209.2 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

zeroize-1.1.2-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl (275.1 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ s390x

zeroize-1.1.2-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (227.5 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ppc64le

zeroize-1.1.2-cp312-cp312-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (216.3 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARMv7l

zeroize-1.1.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (214.3 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

zeroize-1.1.2-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.whl (213.3 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.5+ i686

zeroize-1.1.2-cp312-cp312-macosx_11_0_arm64.whl (183.3 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

zeroize-1.1.2-cp312-cp312-macosx_10_12_x86_64.whl (189.2 kB view hashes)

Uploaded CPython 3.12 macOS 10.12+ x86-64

zeroize-1.1.2-cp311-none-win_amd64.whl (100.0 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

zeroize-1.1.2-cp311-none-win32.whl (96.2 kB view hashes)

Uploaded CPython 3.11 Windows x86

zeroize-1.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (209.7 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

zeroize-1.1.2-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl (279.0 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ s390x

zeroize-1.1.2-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (227.8 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ppc64le

zeroize-1.1.2-cp311-cp311-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (217.0 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARMv7l

zeroize-1.1.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (214.8 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

zeroize-1.1.2-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl (214.3 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.5+ i686

zeroize-1.1.2-cp311-cp311-macosx_11_0_arm64.whl (183.7 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

zeroize-1.1.2-cp311-cp311-macosx_10_12_x86_64.whl (189.8 kB view hashes)

Uploaded CPython 3.11 macOS 10.12+ x86-64

zeroize-1.1.2-cp310-none-win_amd64.whl (100.1 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

zeroize-1.1.2-cp310-none-win32.whl (96.2 kB view hashes)

Uploaded CPython 3.10 Windows x86

zeroize-1.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (209.8 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

zeroize-1.1.2-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl (278.9 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ s390x

zeroize-1.1.2-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (227.8 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ppc64le

zeroize-1.1.2-cp310-cp310-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (217.1 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARMv7l

zeroize-1.1.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (214.9 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

zeroize-1.1.2-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.whl (214.5 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.5+ i686

zeroize-1.1.2-cp310-cp310-macosx_11_0_arm64.whl (183.8 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

zeroize-1.1.2-cp310-cp310-macosx_10_12_x86_64.whl (189.9 kB view hashes)

Uploaded CPython 3.10 macOS 10.12+ x86-64

zeroize-1.1.2-cp39-none-win_amd64.whl (100.0 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

zeroize-1.1.2-cp39-none-win32.whl (96.3 kB view hashes)

Uploaded CPython 3.9 Windows x86

zeroize-1.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (209.7 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

zeroize-1.1.2-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl (279.1 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ s390x

zeroize-1.1.2-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (227.8 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ppc64le

zeroize-1.1.2-cp39-cp39-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (217.1 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARMv7l

zeroize-1.1.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (214.8 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

zeroize-1.1.2-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.whl (214.4 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.5+ i686

zeroize-1.1.2-cp39-cp39-macosx_11_0_arm64.whl (183.8 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

zeroize-1.1.2-cp39-cp39-macosx_10_12_x86_64.whl (189.8 kB view hashes)

Uploaded CPython 3.9 macOS 10.12+ x86-64

zeroize-1.1.2-cp38-none-win_amd64.whl (99.9 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

zeroize-1.1.2-cp38-none-win32.whl (96.1 kB view hashes)

Uploaded CPython 3.8 Windows x86

zeroize-1.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (209.6 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

zeroize-1.1.2-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl (279.0 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ s390x

zeroize-1.1.2-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (227.6 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ppc64le

zeroize-1.1.2-cp38-cp38-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (216.8 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARMv7l

zeroize-1.1.2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (214.6 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

zeroize-1.1.2-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.whl (214.2 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.5+ i686

zeroize-1.1.2-cp37-none-win_amd64.whl (99.9 kB view hashes)

Uploaded CPython 3.7 Windows x86-64

zeroize-1.1.2-cp37-none-win32.whl (96.1 kB view hashes)

Uploaded CPython 3.7 Windows x86

zeroize-1.1.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (209.6 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

zeroize-1.1.2-cp37-cp37m-manylinux_2_17_s390x.manylinux2014_s390x.whl (279.0 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ s390x

zeroize-1.1.2-cp37-cp37m-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (227.6 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ppc64le

zeroize-1.1.2-cp37-cp37m-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (216.8 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARMv7l

zeroize-1.1.2-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (214.6 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARM64

zeroize-1.1.2-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.whl (214.2 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.5+ i686

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page