Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[security] Incorrect file permissions for ccache file #357

Open
dafanasiev opened this issue Sep 9, 2023 · 9 comments · May be fixed by #382
Open

[security] Incorrect file permissions for ccache file #357

dafanasiev opened this issue Sep 9, 2023 · 9 comments · May be fixed by #382
Labels

Comments

@dafanasiev
Copy link

Describe the bug
Only owner can read/write ccache file (when krb5 tiket cache store as local file).
Currently (at least on Linux) the generated ccache file has the wrong permissions (because of umask?).

To Reproduce

var client = new KerberosClient(new Krb5Config
{
    Defaults =
    {
        DnsLookupKdc = false,
        DefaultCCacheName = "FILE:/tmp/ccache.test",   // file not exists
    },
})
{
    CacheInMemory = false
};

var kerbCred = new KerberosPasswordCredential("[email protected]", "password", "company.corp");

client.PinKdc("company.corp", "dc.company.corp");
await client.Authenticate(kerbCred);

// see in shell: 
//
// # umask
// 0002
// # ls -la /tmp/ccache.test
// -rw-rw-r--  ......  /tmp/ccache.test

Additional context
The same problem was previously fixed in nuget cli: https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/NuGet.Packaging/NuGetExtractionFileIO.cs

@dafanasiev dafanasiev added the bug label Sep 9, 2023
@SteveSyfuhs
Copy link
Collaborator

Why would we want to allow more than the owner to access the ticket cache for their own user?

@dafanasiev
Copy link
Author

dafanasiev commented Sep 11, 2023

Why would we want to allow more than the owner to access the ticket cache for their own user?

No, we don't want that.
Now (with umask=0002) any user can read (and any user in owner group can read-write) saved ticket.
We need to set the unix file security attributes explicitly, regardless of the umask value.

See also: https://github.com/krb5/krb5/blob/master/src/lib/krb5/ccache/cc_file.c#L866-L873

@DanielMGoldberg
Copy link

Hey, I'm trying to save my ccache file inside my linux container but it doesn't seem to work and I don't see any error.
I'm hosting my app on openshift.

Anyone knows what might be the issue ?

@amranmo1
Copy link

up. would like to know how to achieve this too

@wfurt
Copy link
Member

wfurt commented Oct 30, 2024

I think it boils down to

File.Open("test",  FileMode.Create, FileAccess.Write, FileShare.None);

creates file like

-rw-rw-r--  1 furt furt    0 Oct 30 22:09 test

This file is readable by anybody as the base File class has no idea it contains security sensitive information.

While many distributions create unique group for each user I would expect that Kerberos.Net should not depend on it and locations like "/tmp/XXXX" may be viewed as security weakness @SteveSyfuhs.

Starting with .NET 7.0 File.SetUnixFileMode is available and it should probably be called explicitly if !OperatingSystem.IsWindows()

I could probably craft PR @SteveSyfuhs - but I'm not sure if we care about Framework or .NET Standard...?

@wfurt
Copy link
Member

wfurt commented Oct 30, 2024

Note that since the cache location if known, anybody who needs to can use the SetUnixFileMode directly as workaround.

@SteveSyfuhs
Copy link
Collaborator

Yeah we need to continue supporting Framework for a while still.

There's a hacky workaround I use in other places that does a reflection call to find the relevant method: https://github.com/dotnet/Kerberos.NET/blob/develop/Kerberos.NET/Crypto/Pal/Rfc2898DeriveBytes.cs

That would probably work well enough here?

@wfurt
Copy link
Member

wfurt commented Oct 31, 2024

yes, one can possibly multi-target and have separate binaries ... but that is pain IMHO. This is not perf critical IMHO and the API is unlikely to change or vanish.

@wfurt wfurt linked a pull request Nov 1, 2024 that will close this issue
2 tasks
@amranmo1
Copy link

amranmo1 commented Nov 7, 2024

I manage to get it work locally by configuring the right volume mount and permission.
Setting the default CCachename in kerberos.net config to write on writeable volume (basically any location within /app is writable)
also to set CacheInMemory = false as the default is true and it won't write to file when its writing to memory

Kerberos.NET config

client.Configuration.Defaults.DefaultCCacheName = "FILE:/app/krb_cache/krb5cc";

docker-compose.override.yml

volumes:
      - krbcache:/app/krb_cache

krb5.conf

[libdefaults]
    default_ccache_name = FILE:/app/krb_cache/krb5cc

dockerfile (to copy the modified default cache file krb5.conf to etc)

COPY ["krb5.conf", "/etc/krb5.conf"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants