aboutsummaryrefslogtreecommitdiff
path: root/grp/putgrent.c
diff options
context:
space:
mode:
authorFlorian Weimer <fweimer@redhat.com>2015-10-02 11:34:13 +0200
committerFlorian Weimer <fweimer@redhat.com>2015-10-02 11:34:13 +0200
commit676599b36a92f3c201c5682ee7a5caddd9f370a4 (patch)
tree6860752c26ccab76ee9db5e60ff465d1edf25feb /grp/putgrent.c
parentb0f81637d5bda47be93bac34b68f429a12979321 (diff)
downloadglibc-676599b36a92f3c201c5682ee7a5caddd9f370a4.tar
glibc-676599b36a92f3c201c5682ee7a5caddd9f370a4.tar.gz
glibc-676599b36a92f3c201c5682ee7a5caddd9f370a4.tar.bz2
glibc-676599b36a92f3c201c5682ee7a5caddd9f370a4.zip
Harden putpwent, putgrent, putspent, putspent against injection [BZ #18724]
This prevents injection of ':' and '\n' into output functions which use the NSS files database syntax. Critical fields (user/group names and file system paths) are checked strictly. For backwards compatibility, the GECOS field is rewritten instead. The getent program is adjusted to use the put*ent functions in libc, instead of local copies. This changes the behavior of getent if user names start with '-' or '+'.
Diffstat (limited to 'grp/putgrent.c')
-rw-r--r--grp/putgrent.c15
1 files changed, 8 insertions, 7 deletions
diff --git a/grp/putgrent.c b/grp/putgrent.c
index e4d662c6b5..1ac7f19a0b 100644
--- a/grp/putgrent.c
+++ b/grp/putgrent.c
@@ -16,7 +16,9 @@
<http://www.gnu.org/licenses/>. */
#include <errno.h>
+#include <nss.h>
#include <stdio.h>
+#include <string.h>
#include <grp.h>
#define flockfile(s) _IO_flockfile (s)
@@ -27,13 +29,14 @@
/* Write an entry to the given stream.
This must know the format of the group file. */
int
-putgrent (gr, stream)
- const struct group *gr;
- FILE *stream;
+putgrent (const struct group *gr, FILE *stream)
{
int retval;
- if (__glibc_unlikely (gr == NULL) || __glibc_unlikely (stream == NULL))
+ if (__glibc_unlikely (gr == NULL) || __glibc_unlikely (stream == NULL)
+ || gr->gr_name == NULL || !__nss_valid_field (gr->gr_name)
+ || !__nss_valid_field (gr->gr_passwd)
+ || !__nss_valid_list_field (gr->gr_mem))
{
__set_errno (EINVAL);
return -1;
@@ -56,9 +59,7 @@ putgrent (gr, stream)
if (gr->gr_mem != NULL)
{
- int i;
-
- for (i = 0 ; gr->gr_mem[i] != NULL; i++)
+ for (size_t i = 0; gr->gr_mem[i] != NULL; i++)
if (fprintf (stream, i == 0 ? "%s" : ",%s", gr->gr_mem[i]) < 0)
{
/* What else can we do? */