Skip to content

nss-systemd should always copy strings returned via NSS structs into caller-provided buffer #20679

Closed

Description

@mcatanzaro

We found an issue where glib's g_get_user_database_entry() may crash after doing:

error = getpwnam_r (logname, &pwd, buffer, bufsize, &pw);
// ...
pw->pw_name[0] = g_ascii_toupper (pw->pw_name[0]);

in order to uppercase the first letter of the user's real name. This is a glib bug, because there is a different codepath that gets the pwd from vanilla getpwnam instead of getpwnam_r as shown here. When the pwd struct is returned by getpwnam, its fields point to static data owned by glibc/NSS, and so it must not be modified by the caller. After much debugging, Jamie Bainbridge has fixed this in https://gitlab.gnome.org/GNOME/glib/-/merge_requests/2244 by making a copy of the data before modifying it, and that resolves all problems for glib. Yay!

However, glib is crashing even when getpwnam_r is used instead of getpwnam! According to getpwnam_r(3), the strings in the pwd struct are supposed to be pointers into the buffer passed by the caller, so glib should be able to safely edit it directly in this case, so long as it doesn't try to increase the size of any of the strings. Unfortunately, nss-systemd gets this wrong in two different cases:

  1. In nss_pack_user_record in userdb-glue.c, systemd returns PASSWORD_SEE_SHADOW ("x") directly without copying it into buffer. This is really unlikely to cause problems, because I cannot imagine why callers would ever try to edit this field, but it's a bug.
  2. More importantly, various functions throughout nss-systemd.c return synthesized records declared at the top of the file: root_passwd, root_spwd, nobody_passwd, nobody_spwd, root_group, root_sgrp, nobody_group, nobody_sgrp. These records are returned directly and so contain pointers to static strings owned by libsystemd-nss. systemd must instead copy all the strings into the provided buffer.

You could say applications should just not do that, and I would be inclined to agree, but nevertheless it does appear to be an API guarantee. I'm working on a WIP pull request to fix both issues.

systemd version the issue has been seen with

systemd 239, but the root cause is still present in upstream systemd, and I intend to try reproducing it with git master

Used distribution

RHEL 8

Linux kernel version used (uname -a)

4.10.something-something.something.el8.x86_64, doesn't matter because this isn't kernel-related

CPU architecture issue was seen on

x86_64

Expected behaviour you didn't see

No crashy!

Unexpected behaviour you saw

Crashy! We found cases where this crashes NetworkManager, firewalld, and vmtoolsd. Probably a lot more. Example backtrace where NetworkManager crashes creating a GLocalFileMonitor looks like:

(gdb) bt
#0  0x00007f0fa857bc35 in g_get_user_database_entry () at gutils.c:710
#1  0x00007f0fa857c11d in g_get_home_dir () at gutils.c:878
#2  0x00007f0fa8d56525 in g_local_file_is_remote (filename=filename@entry=0x56437209ff40 "/lib/firmware") at glocalfile.c:2593
#3  0x00007f0fa8d5c80f in g_local_file_monitor_new_for_path (pathname=0x56437209ff40 "/lib/firmware", is_directory=1, flags=G_FILE_MONITOR_NONE, error=0x0) at glocalfilemonitor.c:868
#4  0x00005643719e57e2 in nm_manager_init (self=0x5643720a0030) at src/core/nm-manager.c:7871
#5  0x00007f0fa8a4a98a in g_type_create_instance (type=94847676042560) at gtype.c:1867
#6  0x00007f0fa8a2cfc8 in g_object_new_internal (class=class@entry=0x56437209df90, params=params@entry=0x0, n_params=n_params@entry=0) at gobject.c:1799
#7  0x00007f0fa8a2e715 in g_object_new_with_properties (object_type=94847676042560, n_properties=0, names=names@entry=0x0, values=values@entry=0x0) at gobject.c:1967
#8  0x00007f0fa8a2f2d1 in g_object_new (object_type=<optimized out>, first_property_name=first_property_name@entry=0x0) at gobject.c:1639
#9  0x00005643719f7081 in nm_manager_setup () at src/core/nm-manager.c:7711
#10 0x00005643719b55d1 in main (argc=<optimized out>, argv=<optimized out>) at src/core/main.c:484

Steps to reproduce the problem

Oh boy. I think the easiest way to reproduce is to delete your /etc/passwd to ensure nss-systemd gets to handle the record lookup. I'm going to try reproducing this myself soon, will be back with better instructions once I've set up a controlled environment.

Additional program output to the terminal or log subsystem illustrating the issue

N/A

Activity

  1. mcatanzaro commented on Sep 8, 2021

    @mcatanzaro
    ContributorAuthor

    WIP fix is at https://github.com/mcatanzaro/systemd/commit/968a0010a7f53c3e9a321aa9b9fd4837bac47ffb. It's not ready for an upstream pull request yet because (1) I haven't tested it yet, and (2) it's broken because I don't yet handle the gr_mem field of struct group and my eyes started to glaze over when I looked at how it's supposed to work. Will return to this soon.

  2. added
    bug 🐛
    on Sep 8, 2021
  3. mcatanzaro commented on Sep 9, 2021

    @mcatanzaro
    ContributorAuthor

    Some comments on how to reproduce, from Jamie in the the glib bug:

    As Michael suggested on his systemd issue, we can reproduce this by removing root from /etc/passwd and forcing systemd to resolve the username

    He also provided a simple test program:

    #include <stdio.h>
    #include <sys/types.h>
    #include <pwd.h>
    
    int main(void)
    {
            const struct passwd pwd = {0};
            const struct passwd *pwp = NULL;
            char buffer[1024] = {0};
    
            getpwnam_r("root", &pwd, buffer, 64, &pwp);
    
            return 0;
    }
    
  4. poettering commented on Sep 9, 2021

    @poettering
    Member

    meh, changing the field in the returned struct is mean... :-(

    But yeah, you are right, we should probably copy this into the provided buffer, to avoid any issues around this.

  5. poettering commented on Sep 9, 2021

    @poettering
    Member

    Patch looks good so far. I added two minor comments there.

  6. mcatanzaro commented on Sep 9, 2021

    @mcatanzaro
    ContributorAuthor

    Um, I forgot the other NSS modules.

    From a quick skim, I think nss-resolve, nss-mymachines, and nss-myhostname are all OK. I don't see static strings returned by nss-resolve or nss-mymachines, and I think nss-myhostname is careful to always copy them (in fill_in_hostent()). So I think this problem is isolated to nss-systemd. I didn't do a thorough check, though, so maintainers may want to confirm that.

  7. mcatanzaro commented on Sep 9, 2021

    @mcatanzaro
    ContributorAuthor

    I found the simplest test case of all: simply move systemd ahead of sssd and files on the passwd and group lines in /etc/nsswitch.conf, then reboot your system. With Fedora's systemd, several services crash during boot and gdm never loads. But if I forcibly install the libnss-systemd I built with my changes, all is good!

    I also tested the test program from #20679 (comment) and it also stops crashing.

  8. added a commit that references this issue on Oct 14, 2021
    055ba73
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

    bug 🐛nssplease-submit-as-pr

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions