<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.freebsd.org/bugzilla/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.freebsd.org/bugzilla/"
          
          maintainer="bugmeister@FreeBSD.org"
>

    <bug>
          <bug_id>263893</bug_id>
          
          <creation_ts>2022-05-10 10:43:38 +0000</creation_ts>
          <short_desc>pam_exec.so in auth stack with expose_authtok option makes su segfault</short_desc>
          <delta_ts>2023-06-29 07:32:27 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>Base System</product>
          <component>bin</component>
          <version>13.1-RELEASE</version>
          <rep_platform>Any</rep_platform>
          <op_sys>Any</op_sys>
          <bug_status>Closed</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc>https://reviews.freebsd.org/D35169</bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>crash</keywords>
          <priority>---</priority>
          <bug_severity>Affects Some People</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Felix Palmen">zirias</reporter>
          <assigned_to name="Ka Ho Ng">khng</assigned_to>
          <cc>lwhsu</cc>
    
    <cc>nyan</cc>
    
    <cc>titus</cc>
          

      

      

      <flag name="mfc-stable13"
          id="66242"
          type_id="10"
          status="?"
          setter="koobs"
    />
    <flag name="mfc-stable12"
          id="66939"
          type_id="9"
          status="-"
          setter="koobs"
    />

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1210625</commentid>
    <comment_count>0</comment_count>
    <who name="Felix Palmen">zirias</who>
    <bug_when>2022-05-10 10:43:38 +0000</bug_when>
    <thetext>su(1) segfaults when there&apos;s pam_exec.so in the &quot;auth&quot; stack with the option expose_authtok.

To reproduce, use the following &quot;auth&quot; config in /etc/pam.d/system:

auth            sufficient      pam_exec.so             expose_authtok /usr/bin/false
auth            required        pam_unix.so             use_first_pass nullok

When removing the &apos;use_first_pass&apos; option from &apos;pam_unix.so&apos;, su asks for a password a second time (as expected), but still segfaults.

When removing the &apos;expose_authtok&apos; option from &apos;pam_exec.so&apos;, the segfault is gone.

A lot of (probably irrelevant) context is here: https://forums.freebsd.org/threads/su-segfaults-when-adding-some-custom-pam_exec-to-the-auth-stack.85112/</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1210658</commentid>
    <comment_count>1</comment_count>
    <who name="titus m">titus</who>
    <bug_when>2022-05-10 13:53:22 +0000</bug_when>
    <thetext>from pam_exec(8)
expose_authtok
             Write the authentication token to the program&apos;s standard input
             stream, followed by a NUL character.  Ignored for
             pam_sm_setcred().


problem is that it is not ignored
when code _pam_exec() is trying to retrieve the auth token when it is called from pam_sm_setcred pam_get_item will set item to null; PAM_AUTHTOK item is set to null when pam_authenticate finishes
then a strlen is performed on null and it segfaults

if (options-&gt;use_first_pass ||
                    strcmp(func, &quot;pam_sm_setcred&quot;) == 0) {
                        /* don&apos;t prompt, only expose existing token */
                        rc = pam_get_item(pamh, PAM_AUTHTOK, &amp;item);
                        authtok = item;
                } 
......
  authtok_size = strlen(authtok) + 1; // &lt;= bombs here</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1210689</commentid>
    <comment_count>2</comment_count>
    <who name="">nyan</who>
    <bug_when>2022-05-10 18:31:11 +0000</bug_when>
    <thetext>This patch should fix it.
https://reviews.freebsd.org/D35169</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1212751</commentid>
    <comment_count>3</comment_count>
    <who name="Kubilay Kocak">koobs</who>
    <bug_when>2022-05-25 00:53:52 +0000</bug_when>
    <thetext>The branch main has been updated by khng:

URL: https://cgit.FreeBSD.org/src/commit/?id=b75e0eed345d2ab047a6b1b00a9a7c3bf92e992c

commit b75e0eed345d2ab047a6b1b00a9a7c3bf92e992c
Author:     Yan Ka Chiu &lt;nyan@myuji.xyz&gt;
AuthorDate: 2022-05-22 16:33:02 +0000
Commit:     Ka Ho Ng &lt;khng@FreeBSD.org&gt;
CommitDate: 2022-05-22 16:36:48 +0000

    pam_exec: fix segfault when authtok is null
    
    According to pam_exec(8), the `expose_authtok` option should be ignored
    when the service function is `pam_sm_setcred`. Currently `pam_exec` only
    prevent prompt for anth token when `expose_authtok` is set on
    `pam_sm_setcred`. This subsequently led to segfault when there isn&apos;t an
    existing auth token available.
    
    Bug reported on this: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=263893
    
    After reading https://reviews.freebsd.org/rS349556 I am not sure if the
    default behaviour supposed to be simply not prompt for authentication
    token, or is it to ignore the option entirely as stated in the man page.
    
    This patch is therefore only adding an additional NULL check on the item
    `pam_get_item` provide, and exit with `PAM_SYSTEM_ERR` when such item is
    NULL.
    
    MFC after:      1 week
    Reviewed by:    des, khng
    Differential Revision:  https://reviews.freebsd.org/D35169</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1212752</commentid>
    <comment_count>4</comment_count>
    <who name="Kubilay Kocak">koobs</who>
    <bug_when>2022-05-25 00:54:10 +0000</bug_when>
    <thetext>^Triage: Assign to committer resolving</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1213370</commentid>
    <comment_count>5</comment_count>
    <who name="Felix Palmen">zirias</who>
    <bug_when>2022-05-28 08:55:24 +0000</bug_when>
    <thetext>Thanks for the commit!

What would be necessary to create an EN to get this fix on releng/* after MFC?

Background: xscreensaver dropped all support for external PAM helpers in 6.03 and kscreenlocker from KDE plasma also removed their own helper in 5.25. I have a workaround based on pam_exec ready (port is prepared and tested), but it won&apos;t work without this fix.

So, consequently, some port updates would be blocked...</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1215865</commentid>
    <comment_count>6</comment_count>
    <who name="Felix Palmen">zirias</who>
    <bug_when>2022-06-15 07:11:09 +0000</bug_when>
    <thetext>I&apos;ll give some more context explaining why it&apos;s important to get this fix in the -RELEASE versions.

Background is that pam_unix.so requires the caller to have root privileges for authentication. LinuxPAM&apos;s pam_unix.so contains a workaround specifically for authenticating as yourself, by calling its own suid-root helper. Screen lockers start taking that for granted (so far xscreensaver and kscreenlocker from KDE plasma).

Adding the same kind of workaround to FreeBSD&apos;s pam_unix.so was rejected by des@ and others, with des@ specifically recommending to use pam_exec.so instead.

The clean and correct solution might be some authentication service that could be used by pam_unix.so instead of accessing the passwd database directly, but this is a complex thing to do, it&apos;s certainly off the table for a timely solution.

So, calling a helper with pam_exec.so is the unintrusive way to have a workaround similar to LinuxPAM and make these screenlockers &quot;just work&quot; (given a correctly configured PAM service policy is installed for them). A pam_exec.so that might crash when used for authentication blocks that solution.

The only other option remaining would be to patch in support for external suid-root helpers in the affected screen lockers. That&apos;s not sustainable in the longer run, IMHO we shouldn&apos;t even start doing that...</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1216261</commentid>
    <comment_count>7</comment_count>
    <who name="">nyan</who>
    <bug_when>2022-06-18 09:39:03 +0000</bug_when>
    <thetext>This patch can be MFC to 13 but does not need for stable/12 as the regression is introduced in 13 along with the introduction of use_first_pass option in pam_exec</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1216347</commentid>
    <comment_count>8</comment_count>
    <who name="Felix Palmen">zirias</who>
    <bug_when>2022-06-19 13:22:52 +0000</bug_when>
    <thetext>(In reply to nyan from comment #7)

Thanks, tested and confirmed 12.3-RELEASE is not affected.

Still the fix is needed for 13.0-RELEASE and 13.1-RELEASE to move forward with port updates.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1217377</commentid>
    <comment_count>9</comment_count>
    <who name="Felix Palmen">zirias</who>
    <bug_when>2022-06-27 07:48:32 +0000</bug_when>
    <thetext>Thanks for MFC to stable/13! Should probably be flagged here to correctly reflect progress, but it seems I don&apos;t have the required privileges to do so…

Now, how would the process continue for ENs to get it on the releases? Anything I can do to help here?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1221435</commentid>
    <comment_count>10</comment_count>
    <who name="Li-Wen Hsu">lwhsu</who>
    <bug_when>2022-08-02 13:33:36 +0000</bug_when>
    <thetext>Secteam responsed that this is in the queue for the next release.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1222179</commentid>
    <comment_count>11</comment_count>
    <who name="Felix Palmen">zirias</who>
    <bug_when>2022-08-08 12:53:19 +0000</bug_when>
    <thetext>(In reply to Li-Wen Hsu from comment #10)
That&apos;s great, thanks! Then I guess this PR can be closed (and mfc-stable13 set to + as it already happened)?</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>