[SRU] smbclient cannot connect anonymously in Kerberos context (freeipa)

Bug #1892145 reported by Kees Bakker
18
This bug affects 2 people
Affects Status Importance Assigned to Milestone
samba
Unknown
Unknown
samba (Ubuntu)
Fix Released
Undecided
Unassigned
Focal
Fix Released
Undecided
Paride Legovini
Groovy
Won't Fix
Undecided
Unassigned
Hirsute
Fix Released
Undecided
Unassigned

Bug Description

This is a Focal-only SRU.

[Impact (from https://bugzilla.samba.org/show_bug.cgi?id=14344#c2)]

If there is a problem reading credential cache then smbclient can core with double free.

e.g. something like

smbclient -L //foo.bar.com

can result in

Enter TUX-NET\tux's password:
Failed to resolve credential cache 'DIR:/run/user/1000/krb5cc'! (No credentials cache found)
*** Error in `smbclient': double free or corruption (fasttop): 0x0000560cd2ea8890 ***
Aborted (core dumped)

[Test Plan]

Setting up a reproducer is not easy as the crash is not 100% reproducible, however the samba package ships with autopkgtests which can be used for regression testing of the "base" samba functionalities, but they do not cover integration with Kerberos. Proper testing requires setting up a krb5 and making samba authenticate against with when the krb5 credential cache is unresolved.

Test PPA (amd64, ppc64el, s390x):

https://launchpad.net/~paride/+archive/ubuntu/samba-lp1892145

[Regression Potential]

The patch is a cherry-pick from upstream and has a little and well defined scope: it removes a free() in a given situation. The patch is a cherry-pick from upstream and has already been released in stable upstream branches and as such it's already shipped in a stable release of Ubuntu (Hirsute), in the current devel release (Impish) and in Debian Bullseye (currently testing). Therefore it can be considered field tested. The patch doesn't modify the behavior of any interface or user-facing component.

The regression potential can be considered low.

[Development Fix]

The patch is included in the following upstream and Ubuntu releases:

 * >= 4.11.9
 * >= 4.12.3
 * >= 4.13.0 (>= Hirsute)

[Original Description]

It is not possible anymore to connect anonymously to a Samba server, if there is a Kerberos environment. It does not matter if there is a valid Kerberos ticket or not. I'm using FreeIPA.

This is with smbclient 2:4.11.6+dfsg-0ubuntu1.4
For example,

$ smbclient -L '//dist.ghs.nl/space' -N
Failed to resolve credential cache 'KEYRING:persistent:60001'! (Unknown credential cache type)
free(): double free detected in tcache 2
Aborted (core dumped)

On Ubuntu 18.04, with smbclient 2:4.7.6+dfsg~ubuntu-0ubuntu2.18 it works as expected (albeit with many messages about failing krb5_init_context and smb_krb5_context_init_basic)

The combination Samba + FreeIPA + Ubuntu has never worked since I started using FreeIPA a few years ago. But anonymous access to a Samba server did work, until I switched to Ubuntu 20.04.

Related branches

Revision history for this message
Kees Bakker (keestux) wrote :

There is an option -k, to enable Kerberos. But there is no option to disable it. Smbclient decides on its own to use Kerberos, and it crashes (core dumped) while doing so.

Revision history for this message
Kees Bakker (keestux) wrote :

A workaround is to confuse smbclient by setting KRB5CCNAME to an unknown file

     KRB5CCNAME=FILE:/none-existing-file

I just strumbled on a note from Alexander Bokovoy

    "... and Samba on Debian/Ubuntu is compiled with Heimdal Kerberos
    ... Heimdal has no support for KEYRING type"

Revision history for this message
Lucas Kanashiro (lucaskanashiro) wrote :

Thanks for taking the time to report this issue and try to make Ubuntu better.

You mentioned you already have a workaround for your problem, it's a good start. You also mentioned it crashed and you got a core dump, could you please share this core dump with us?

I am changing the status to Incomplete, when you provide the core dump please change it back to New and we will revisit this bug.

Changed in samba (Ubuntu):
status: New → Incomplete
Revision history for this message
Kees Bakker (keestux) wrote :

Well, it didn't actually create a core dump. It only said so. I'll see what I can do to actually create the dump.

Revision history for this message
Kees Bakker (keestux) wrote :

Here is the core dump.

Changed in samba (Ubuntu Bionic):
status: New → Fix Released
Changed in samba (Ubuntu Groovy):
status: Incomplete → Triaged
Changed in samba (Ubuntu Focal):
status: New → Confirmed
no longer affects: samba (Ubuntu Groovy)
Revision history for this message
Andreas Hasenack (ahasenack) wrote :

Can you share your /etc/krb5.conf file? I don't know all the options that the freeipa setup changes there from the defaults.

Revision history for this message
Andreas Hasenack (ahasenack) wrote :

I'm using a focal container for this test, with kdc and samba on localhost, but using fqdn's for the access.
krb5-kdc 1.17-6ubuntu4
samba 2:4.11.6+dfsg-0ubuntu1.4

With the default ccache_type of FILE in ubuntu/debian:
$ klist
Ticket cache: FILE:/tmp/krb5cc_1000
Default principal: <email address hidden>
...

smbclient //focal-smbclient-kerberos.lxd/storage -k (after kinit)
smbclient -L focal-smbclient-kerberos.lxd -k (after kinit)
smbclient -L focal-smbclient-kerberos.lxd -N (with or without kinit)

work.

The moment I set this in /etc/krb5.conf:

default_ccache_name = KEYRING:persistent:%{uid}

(is that the setting you have?)

Then some things change, but I don't get a core dump.

This works with or without kinit:
smbclient -L focal-smbclient-kerberos -N

These don't work after kinit:

$ smbclient -L focal-smbclient-kerberos -k
gensec_spnego_client_negTokenInit_step: Could not find a suitable mechtype in NEG_TOKEN_INIT
session setup failed: NT_STATUS_INVALID_PARAMETER

$ smbclient //focal-smbclient-kerberos.lxd/storage -k
gensec_spnego_client_negTokenInit_step: Could not find a suitable mechtype in NEG_TOKEN_INIT
session setup failed: NT_STATUS_INVALID_PARAMETER
$ klist
Ticket cache: KEYRING:persistent:1000:1000
Default principal: <email address hidden>

Valid starting Expires Service principal
08/31/20 14:49:10 09/01/20 00:49:10 <email address hidden>
 renew until 09/01/20 14:49:09

I did find an upstream heimdal bug about adding support for KEYRING, and it's closed now with a fix committed:
https://github.com/heimdal/heimdal/issues/166

I will have to investigate further to see how samba was built and confirm our heimdal libraries in ubuntu have this support available. And if this is the problem we are seeing here.

I'll check your core dump file now.

From your side, if you switch the ccache type to FILE (or just remove the KEYRING overriding config), does the core dump go away?

Revision history for this message
Andreas Hasenack (ahasenack) wrote :

I'm failing to understand upstream's release process. The commit that added keyring support is from December 24th, 2018. Heimdal 7.7.0 (latest release) was tagged in June 3rd, 2019:

commit e1959605bd490e1eb9ea5e2277f4a332208097de (tag: heimdal-7.7.0)
Author: Jeffrey Altman <email address hidden>
Date: Mon Jun 3 23:53:04 2019 -0400

    Bump version to 7.7.0

But does not have the keyring commit.

Furthermore, the 7.7.0 tag is in the heimdal-7-1 branch, which I assumed was for an old 7.1.x series, but doesn't like it.

Bottom line, KEYRING support is not in any release of heimdal yet.

The smbclient core dump is something I would like to reproduce, though, but I think I will need your config files for that, as just setting the ccache type to KEYRING is not enough.

Revision history for this message
Andreas Hasenack (ahasenack) wrote :

Interesting, on a machine where I installed smbclient and heimdal-clients, smbclient -L -N does grab the cifs/ kerberos ticket, even though I didn't supply -k:

ubuntu@focal-heimdal-client:~$ klist
klist: No ticket file: /tmp/krb5cc_1000

ubuntu@focal-heimdal-client:~$ kinit
<email address hidden>'s Password:

ubuntu@focal-heimdal-client:~$ smbclient -L focal-smbclient-kerberos.lxd -N

 Sharename Type Comment
 --------- ---- -------
 print$ Disk Printer Drivers
 storage Disk
 IPC$ IPC IPC Service (focal-smbclient-kerberos server (Samba, Ubuntu))
 ubuntu Disk Home directory of ubuntu
SMB1 disabled -- no workgroup available

ubuntu@focal-heimdal-client:~$ klist
Credentials cache: FILE:/tmp/krb5cc_1000
        Principal: <email address hidden>

  Issued Expires Principal
Aug 31 17:26:04 2020 Sep 1 03:26:04 2020 <email address hidden>
Aug 31 17:26:06 2020 Sep 1 03:26:04 2020 <email address hidden>

That is not the behavior when I'm using kinit from krb5-user (MIT).

Revision history for this message
Kees Bakker (keestux) wrote :

The /etc/krb5.conf is installed and configured by FreeIPA. See attached krb5.conf

Indeed it has

[libdefaults]
  default_ccache_name = KEYRING:persistent:%{uid}

Revision history for this message
Jesse Michael (jesse.michael) wrote :
Download full text (6.8 KiB)

This bug is due to a double-free in source3/librpc/crypto/gse.c where gse_ctx->k5ctx is freed twice if gse_context_init fails and the err_out path is taken.

The beginning of gse_context_init calls talloc_set_destructor to call gse_context_destructor:

        talloc_set_destructor((TALLOC_CTX *)gse_ctx, gse_context_destructor);

This gse_context_destructor callback function is triggered by calls to TALLOC_FREE(gse_ctx) and frees pointers stored in the gse_ctx structure including gse_ctx->k5ctx:

        if (gse_ctx->k5ctx) {
                if (gse_ctx->ccache) {
                        krb5_cc_close(gse_ctx->k5ctx, gse_ctx->ccache);
                        gse_ctx->ccache = NULL;
                }
                if (gse_ctx->keytab) {
                        krb5_kt_close(gse_ctx->k5ctx, gse_ctx->keytab);
                        gse_ctx->keytab = NULL;
                }
                krb5_free_context(gse_ctx->k5ctx);
                gse_ctx->k5ctx = NULL;
        }

However, if gse_context_init fails and takes the err_out path, gse_ctx->k5ctx is freed without setting that pointer to NULL and then immediately calls TALLOC_FREE(gse_ctx) which then attempts to free gse_ctx->k5ctx a second time:

err_out:
        if (gse_ctx->k5ctx) {
                krb5_free_context(gse_ctx->k5ctx);
        }

        TALLOC_FREE(gse_ctx);

This results in the following double-free stack trace:

(gdb) bt
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1 0x00007ffff7443859 in __GI_abort () at abort.c:79
#2 0x00007ffff74ae3ee in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7ffff75d8285 "%s\n") at ../sysdeps/posix/libc_fatal.c:155
#3 0x00007ffff74b647c in malloc_printerr (str=str@entry=0x7ffff75da5d0 "free(): double free detected in tcache 2") at malloc.c:5347
#4 0x00007ffff74b80ed in _int_free (av=0x7ffff7609b80 <main_arena>, p=0x5555555ce2c0, have_lock=0) at malloc.c:4201
#5 0x00007ffff6cc29f9 in krb5_free_context (context=0x5555555cdcd0) at ../../source4/heimdal/lib/krb5/context.c:595
#6 0x00007ffff73e75d1 in gse_context_destructor (ptr=ptr@entry=0x5555555cdc50) at ../../source3/librpc/crypto/gse.c:84
#7 0x00007ffff776553e in _tc_free_internal (tc=0x5555555cdbf0, location=0x7ffff73f2480 "../../source3/librpc/crypto/gse.c:241") at ../../talloc.c:1157
#8 0x00007ffff73e826c in gse_context_init (mem_ctx=mem_ctx@entry=0x5555555cdb60, do_sign=<optimized out>, do_seal=<optimized out>, add_gss_c_flags=<optimized out>, _gse_ctx=_gse_ctx@entry=0x7fffffffd500,
    ccache_name=<optimized out>) at ../../source3/librpc/crypto/gse.c:241
#9 0x00007ffff73e8433 in gse_init_client (ccache_name=0x0, realm=<optimized out>, username=<optimized out>, password=<optimized out>, _gse_ctx=<synthetic pointer>, add_gss_c_flags=<optimized out>,
    service=0x5555555ccdf0 "cifs", server=0x5555555ccfb0 "freenas", do_seal=<optimized out>, do_sign=<optimized out>, mem_ctx=0x5555555cdb60) at ../../source3/librpc/crypto/gse.c:268
#10 gensec_gse_client_start (gensec_security=0x5555555cdb60) at ../../source3/librpc/crypto/gse.c:786
#11 0x00007ffff7390453 in gensec_start_mech (gensec_security=0x5555555cdb60) at ../../auth/gensec/gensec_start...

Read more...

Revision history for this message
Christian Ehrhardt  (paelzer) wrote :

I agree to the analysis.
Latest master looks fine thou.
Change seems to be in https://git.samba.org/samba.git/?p=samba.git;a=commit;h=34f8ab774d1484b0e60

That is released in 4.13 and thereby fix released in 21.04.

SRUs to Focal and Groovy need to be considered.

Changed in samba (Ubuntu):
status: Triaged → Fix Released
Changed in samba (Ubuntu Groovy):
status: New → Confirmed
tags: added: server-next
Revision history for this message
Sergio Durigan Junior (sergiodj) wrote :

Like Andreas, I cannot reproduce the coredump either.

# smbclient //focal-01.example.com/myshare -k
gensec_spnego_client_negTokenInit_step: Could not find a suitable mechtype in NEG_TOKEN_INIT
session setup failed: NT_STATUS_INVALID_PARAMETER

Even after using the same options as the ones provided in the krb5.conf from comment #10, the coredump doesn't happen. I wonder if Jesse Michael is able to provide a reproducer for this, because otherwise it will be hard to SRU this without having a reliable test case.

Revision history for this message
Sergio Durigan Junior (sergiodj) wrote :

@Kees Baker, thanks for providing your krb5.conf. Would it be possible to also provide the contents of any file under /etc/krb5.conf.d/ and /var/lib/sss/pubconf/krb5.include.d/. I'm wondering if there's anything in those directories that is influencing the bug. Thanks.

Revision history for this message
Kees Bakker (keestux) wrote :

In /etc/krb5.conf.d/freeipa there is

[libdefaults]
    spake_preauth_groups = edwards25519

And in /var/lib/sss/pubconf/krb5.include.d there is the following

$ more /var/lib/sss/pubconf/krb5.include.d/*|cat
::::::::::::::
/var/lib/sss/pubconf/krb5.include.d/domain_realm_ghs_nl
::::::::::::::
[domain_realm]
::::::::::::::
/var/lib/sss/pubconf/krb5.include.d/krb5_libdefaults
::::::::::::::
[libdefaults]
 canonicalize = true
::::::::::::::
/var/lib/sss/pubconf/krb5.include.d/localauth_plugin
::::::::::::::
[plugins]
 localauth = {
  module = sssd:/usr/lib/x86_64-linux-gnu/sssd/modules/sssd_krb5_localauth_plugin.so
 }

Paride Legovini (paride)
Changed in samba (Ubuntu Focal):
assignee: nobody → Paride Legovini (paride)
Changed in samba (Ubuntu Groovy):
assignee: nobody → Paride Legovini (paride)
Revision history for this message
Brian Murray (brian-murray) wrote :

The Groovy Gorilla has reached end of life, so this bug will not be fixed for that release

Changed in samba (Ubuntu Groovy):
status: Confirmed → Won't Fix
Paride Legovini (paride)
Changed in samba (Ubuntu Groovy):
assignee: Paride Legovini (paride) → nobody
Paride Legovini (paride)
summary: - smbclient cannot connect anonymously in Kerberos context (freeipa)
+ [SRU] smbclient cannot connect anonymously in Kerberos context (freeipa)
Changed in samba (Ubuntu Hirsute):
status: New → Fix Released
no longer affects: samba (Ubuntu Bionic)
Changed in samba (Ubuntu Focal):
status: Confirmed → In Progress
Paride Legovini (paride)
description: updated
description: updated
Paride Legovini (paride)
description: updated
Revision history for this message
Paride Legovini (paride) wrote :
Revision history for this message
Brian Murray (brian-murray) wrote :

For what its worth this crash in the Error Tracker seems to be about the same issue:

https://errors.ubuntu.com/problem/c49600167f3e13fe9a97f89cca4e6e9d8649890f

However, given the frequency of crashes it probably won't work as a way to confirm that the crash is fixed.

Changed in samba (Ubuntu Focal):
status: In Progress → Fix Committed
tags: added: verification-needed verification-needed-focal
Revision history for this message
Brian Murray (brian-murray) wrote : Please test proposed package

Hello Kees, or anyone else affected,

Accepted samba into focal-proposed. The package will build now and be available at https://launchpad.net/ubuntu/+source/samba/2:4.11.6+dfsg-0ubuntu1.10 in a few hours, and then in the -proposed repository.

Please help us by testing this new package. See https://wiki.ubuntu.com/Testing/EnableProposed for documentation on how to enable and use -proposed. Your feedback will aid us getting this update out to other Ubuntu users.

If this package fixes the bug for you, please add a comment to this bug, mentioning the version of the package you tested, what testing has been performed on the package and change the tag from verification-needed-focal to verification-done-focal. If it does not fix the bug for you, please add a comment stating that, and change the tag to verification-failed-focal. In either case, without details of your testing we will not be able to proceed.

Further information regarding the verification process can be found at https://wiki.ubuntu.com/QATeam/PerformingSRUVerification . Thank you in advance for helping!

N.B. The updated package will be released to -updates after the bug(s) fixed by this package have been verified and the package has been in -proposed for a minimum of 7 days.

Revision history for this message
Paride Legovini (paride) wrote :

@Brian I agree that crash *may* be due to this bug, but the crash frequency is low so that won't be a good indicator.

Unfortunately I wasn't able to setup a reliable reproducer for this bug, however I checked for obvious regressions and did a manual autopkgtest run, everything looks good.

@Kees: ideally we'd need some feedback from you here. Does the samba package currently in focal-proposed fix the problem you reported?

In case of missing feedback I'll do some more local testing. I'm leaving this as verification-needed for now.

---

autopkgtest [13:26:34]: test smbclient-share-access: - - - - - - - - - - results - - - - - - - - - -
smbclient-share-access PASS
autopkgtest [13:26:34]: @@@@@@@@@@@@@@@@@@@@ summary
reinstall-samba-common-bin SKIP Test needs to reboot testbed but testbed does not provide reboot capability
cifs-share-access PASS
python-smoke PASS
smbclient-anonymous-share-list PASS
smbclient-authenticated-share-list PASS
smbclient-share-access PASS

Paride Legovini (paride)
description: updated
Revision history for this message
Paride Legovini (paride) wrote :

In absence of further feedback I tried to do the verification of this one, setting up krb5 with

[libdefaults]
  default_ccache_name = KEYRING:persistent:%{uid}

and doing:

  smbclient -L //foo.bar.com

pre- and post-upgrade. In both cases I could not get a coredump, but both the Ubuntu bug and the upstream bug mention that the crash is not always reproducible. Either I've been lucky or I didn't setup a proper reproducer. Anyway I couldn't spot any misbehavior in the package, so I what I can say with more confidence is that we're not regressing it.

tags: added: verification-done verification-done-focal
removed: verification-needed verification-needed-focal
Revision history for this message
Ilya Belyaev (bejik) wrote (last edit ):

We have reproduced the problem.
After installing the packages from proposed, everything works.
Update: It was about focal release. We had this error on Mint 20 with FreeIPA LDAP authentification.

Revision history for this message
Paride Legovini (paride) wrote :

Excellent, thanks Ilya, that's the best feedback we can get!

Revision history for this message
Kees Bakker (keestux) wrote :

Sorry for not responding sooner. I'm away from that system where I first experienced the problem. I'll be able to test it not sooner than early September.

Besides that, I have a setup (with LXC containers). Unfortunately I can't reproduce the initial problem in this setup.

Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package samba - 2:4.11.6+dfsg-0ubuntu1.10

---------------
samba (2:4.11.6+dfsg-0ubuntu1.10) focal; urgency=medium

  * d/p/fix-double-free-with-unresolved-credentia-cache.patch: Fix
    double free with unresolved credential cache. (LP: #1892145)

 -- Paride Legovini <email address hidden> Fri, 06 Aug 2021 14:17:29 +0200

Changed in samba (Ubuntu Focal):
status: Fix Committed → Fix Released
Revision history for this message
Brian Murray (brian-murray) wrote : Update Released

The verification of the Stable Release Update for samba has completed successfully and the package is now being released to -updates. Subsequently, the Ubuntu Stable Release Updates Team is being unsubscribed and will not receive messages about this bug report. In the event that you encounter a regression using the package from -updates please report a new bug using ubuntu-bug and tag the bug report regression-update so we can easily find any regressions.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.