Skip to content

[BUG] Salt 3008 PAM _authenticate() via ctypes fails when called from forked worker process but works in interactive shell #69304

Closed

Description

@ebraheem-abo

Description

This is a follow-up to issue #69303.

Even after fixing the Python path issue in __find_pyexe(),
a second bug exists in Salt 3008's PAM authentication.

The _authenticate() function which uses ctypes to call
libpam directly, fails when called from Salt's forked
worker process (MWorker-default-*), even though the
exact same function call succeeds in an interactive shell.

Both PAM_AUTHENTICATE and PAM_ACCT_MGMT fail when called
from the forked worker process context.

This means PAM authentication is broken at TWO levels
in Salt 3008.


Verified Environment


Steps to Reproduce

  1. Apply the workaround from issue [BUG] Salt 3008 PAM auth always returns 401 - __find_pyexe() returns system Python instead of Salt's bundled Python #69303:

/etc/salt/master.d/pam.conf

auth.pam.service: salt-api
auth.pam.python: /opt/saltstack/salt/bin/python3.14

  1. Install python-pam in Salt's Python:

sudo /opt/saltstack/salt/bin/pip install python-pam

  1. Create PAM service file:

/etc/pam.d/salt-api

#%PAM-1.0
auth required pam_unix.so nullok
account required pam_unix.so

  1. Create system user:

sudo useradd -M -s /bin/bash saltapi
sudo passwd saltapi

  1. Start services:

sudo service salt-master start
sudo service salt-api start

  1. Attempt to authenticate:

curl -si http://localhost:8000/login
-H 'Accept: application/json'
-d username=saltapi
-d password=yourpassword
-d eauth=pam


Expected Result

HTTP/1.1 200 OK
{
"return": [{
"token": "xxxxxxxxxxxxxxxx",
"user": "saltapi",
"eauth": "pam"
}]
}


Actual Result

HTTP/1.1 401 Unauthorized
"Could not authenticate using provided credentials"


Full Debugging Journey


DEBUG STEP 1 — Added debug to pam.py error log

Modified authenticate() function error line:

  • log.error("Pam auth failed for %s: %s %s",
  •       username, ret.stdout, ret.stderr)
    
  • log.error("Pam auth failed for %s: returncode=%s stdout=%s stderr=%s pyexe=%s",
  •       username, ret.returncode, ret.stdout, ret.stderr, str(pyexe))
    

Log revealed:
Pam auth failed for saltapi: returncode=1 stdout= stderr= pyexe=/usr/bin/python3.10

→ Root cause of first bug found (issue #69303)
__find_pyexe() returns wrong Python


DEBUG STEP 2 — Fixed Python path (workaround for #69303)

Added to /etc/salt/master.d/pam.conf:
auth.pam.python: /opt/saltstack/salt/bin/python3.14

Log now shows:
Pam auth failed for saltapi: returncode=1 stdout= stderr= pyexe=/opt/saltstack/salt/bin/python3.14

→ Correct Python now used BUT still failing
→ Second bug confirmed — different root cause


DEBUG STEP 3 — Verified PAM works outside Salt context

Test 1 — python-pam direct test: ✅

sudo /opt/saltstack/salt/bin/python3.14 -c "
import pam
p = pam.pam()
print(p.authenticate('saltapi', 'yourpassword', service='salt-api'))
"
Result: True ✅

Test 2 — subprocess simulation: ✅

sudo SALT_PAM_USERNAME=saltapi
SALT_PAM_PASSWORD=yourpassword
SALT_PAM_SERVICE=salt-api
SALT_PAM_ENCODING=utf-8
/opt/saltstack/salt/bin/python3.14
/opt/saltstack/salt/lib/python3.14/site-packages/salt/auth/pam.py
Exit code: 0 ✅

Test 3 — simulate exactly what Salt does with capture_output: ✅

sudo /opt/saltstack/salt/bin/python3.14 -c "
import os, subprocess
env = os.environ.copy()
env['SALT_PAM_USERNAME'] = 'saltapi'
env['SALT_PAM_PASSWORD'] = 'yourpassword'
env['SALT_PAM_SERVICE'] = 'salt-api'
env['SALT_PAM_ENCODING'] = 'utf-8'
pyfile = '/opt/saltstack/salt/lib/python3.14/site-packages/salt/auth/pam.py'
pyexe = '/opt/saltstack/salt/bin/python3.14'
ret = subprocess.run([pyexe, pyfile], env=env, capture_output=True, check=False)
print('returncode:', ret.returncode)
print('stdout:', ret.stdout)
print('stderr:', ret.stderr)
"
returncode: 0 ✅
stdout: b''
stderr: b''

→ PAM works perfectly outside Salt worker context
→ Failure is specific to Salt forked worker process


DEBUG STEP 4 — Bypassed subprocess entirely

Patched authenticate() in pam.py to call _authenticate directly:

  • def authenticate(username, password):
  • def __find_pyexe():
    
  •     ...
    
  • env = os.environ.copy()
    
  • env["SALT_PAM_USERNAME"] = username
    
  • env["SALT_PAM_PASSWORD"] = password
    
  • env["SALT_PAM_SERVICE"] = __opts__.get("auth.pam.service", "login")
    
  • env["SALT_PAM_ENCODING"] = __salt_system_encoding__
    
  • pyexe = pathlib.Path(__find_pyexe()).resolve()
    
  • pyfile = pathlib.Path(__file__).resolve()
    
  • ret = subprocess.run(
    
  •     [str(pyexe), str(pyfile)],
    
  •     env=env,
    
  •     capture_output=True,
    
  •     check=False,
    
  • )
    
  • if ret.returncode == 0:
    
  •     return True
    
  • log.error("Pam auth failed for %s: %s %s",
    
  •           username, ret.stdout, ret.stderr)
    
  • return False
    
  • def authenticate(username, password):
  • service = __opts__.get("auth.pam.service", "login")
    
  • result = _authenticate(username, password, service)
    
  • log.error("PAM DEBUG: user=%s service=%s result=%s",
    
  •           username, service, result)
    
  • return result
    

Log now shows:
PAM DEBUG: user=saltapi service=salt-api result=False ❌

→ _authenticate() itself returns False from worker process
→ Not a subprocess issue — the ctypes PAM call fails in worker


DEBUG STEP 5 — Isolated PAM_ACCT_MGMT failure

Changed /etc/pam.d/salt-api account line only:

  • account required pam_unix.so
  • account required pam_permit.so

/etc/pam.d/salt-api became:

auth required pam_unix.so nullok
account required pam_permit.so

Result: STILL 401 ❌

→ Proves PAM_AUTHENTICATE also fails in worker process
→ Not just PAM_ACCT_MGMT
→ pam_unix.so auth module itself fails in forked context


DEBUG STEP 6 — Replaced auth module too

Changed /etc/pam.d/salt-api auth line as well:

  • auth required pam_unix.so nullok
  • auth required pam_permit.so

/etc/pam.d/salt-api became:

auth required pam_permit.so
account required pam_permit.so

Result: HTTP 200 OK ✅
Token received successfully!

→ Confirms pam_unix.so is the failure point
→ pam_unix.so auth module fails when called via ctypes
from Salt's forked worker process


Root Cause

The ctypes-based conversation function my_conv() in
_authenticate() behaves differently in forked worker
process context vs interactive shell:

Context PAM_AUTHENTICATE PAM_ACCT_MGMT Result
Interactive shell ✅ Success ✅ Success True
subprocess from shell ✅ Success ✅ Success True
Salt forked worker ❌ FAILS Never reached False

The my_conv() callback function:

@CONV_FUNC
def my_conv(n_messages, messages, p_response, app_data):
for i in range(n_messages):
if messages[i].contents.msg_style == PAM_PROMPT_ECHO_OFF:
pw_copy = STRDUP(password)
p_response.contents[i].resp = cast(pw_copy, c_char_p)
p_response.contents[i].resp_retcode = 0
return 0

Only handles PAM_PROMPT_ECHO_OFF message type.
In forked worker context, pam_unix.so may send a
different message type that my_conv() does not handle,
causing PAM_AUTHENTICATE to fail silently.


Suggested Fix

my_conv() should handle ALL PAM message types:

Current broken code

if messages[i].contents.msg_style == PAM_PROMPT_ECHO_OFF:

Suggested fix

if messages[i].contents.msg_style in (PAM_PROMPT_ECHO_OFF,
PAM_PROMPT_ECHO_ON):

This would allow _authenticate() to work correctly
in all process contexts including forked workers.


Workaround

WARNING: This bypasses password verification entirely.
Only use in controlled/trusted environments.
NOT recommended for production use.

/etc/pam.d/salt-api

auth required pam_permit.so
account required pam_permit.so


Impact


Related

Activity

  1. welcome commented on Jun 3, 2026

    @welcome

    Hi there! Welcome to the Salt Community! Thank you for making your first contribution. We have a lengthy process for issues and PRs. Someone from the Core Team will follow up as soon as possible. In the meantime, here's some information that may help as you continue your Salt journey.
    Please be sure to review our Code of Conduct. Also, check out some of our community resources including:

    There are lots of ways to get involved in our community. Every month, there are around a dozen opportunities to meet with other contributors and the Salt Core team and collaborate in real time. The best way to keep track is by subscribing to the Salt Community Events Calendar.
    If you have additional questions, email us at [email protected]. We're glad you've joined our community and look forward to doing awesome things with you!

  2. added a commit that references this issue on Jun 6, 2026
    5470566
  3. added this to the Sulphur v3006.26 milestone on Jun 6, 2026
  4. added 4 commits that reference this issue on Jun 7, 2026
    e8090fd
    34e1c14
    727c063
    5a51684
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions