summaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
authorColin Watson <cjwatson@debian.org>2014-02-09 16:09:58 +0000
committerColin Watson <cjwatson@debian.org>2017-03-29 01:39:47 +0100
commit0b9c0482cbff9ce16384e4247d955676d4d77df3 (patch)
treedcaaa33c770f1520a423e31d287ef10d827acc1b /misc.c
parent980646a9f7f03b43b678272b2a56e30906c6ddec (diff)
Allow harmless group-writability
Allow secure files (~/.ssh/config, ~/.ssh/authorized_keys, etc.) to be group-writable, provided that the group in question contains only the file's owner. Rejected upstream for IMO incorrect reasons (e.g. a misunderstanding about the contents of gr->gr_mem). Given that per-user groups and umask 002 are the default setup in Debian (for good reasons - this makes operating in setgid directories with other groups much easier), we need to permit this by default. Bug: https://bugzilla.mindrot.org/show_bug.cgi?id=1060 Bug-Debian: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=314347 Last-Update: 2013-09-14 Patch-Name: user-group-modes.patch
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c69
1 files changed, 68 insertions, 1 deletions
diff --git a/misc.c b/misc.c
index cfd32729a..6e972f563 100644
--- a/misc.c
+++ b/misc.c
@@ -51,8 +51,9 @@
51#include <netdb.h> 51#include <netdb.h>
52#ifdef HAVE_PATHS_H 52#ifdef HAVE_PATHS_H
53# include <paths.h> 53# include <paths.h>
54#include <pwd.h>
55#endif 54#endif
55#include <pwd.h>
56#include <grp.h>
56#ifdef SSH_TUN_OPENBSD 57#ifdef SSH_TUN_OPENBSD
57#include <net/if.h> 58#include <net/if.h>
58#endif 59#endif
@@ -61,6 +62,7 @@
61#include "misc.h" 62#include "misc.h"
62#include "log.h" 63#include "log.h"
63#include "ssh.h" 64#include "ssh.h"
65#include "platform.h"
64 66
65/* remove newline at end of string */ 67/* remove newline at end of string */
66char * 68char *
@@ -713,6 +715,71 @@ read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz,
713 return -1; 715 return -1;
714} 716}
715 717
718/*
719 * return 1 if the specified uid is a uid that may own a system directory
720 * otherwise 0.
721 */
722int
723platform_sys_dir_uid(uid_t uid)
724{
725 if (uid == 0)
726 return 1;
727#ifdef PLATFORM_SYS_DIR_UID
728 if (uid == PLATFORM_SYS_DIR_UID)
729 return 1;
730#endif
731 return 0;
732}
733
734int
735secure_permissions(struct stat *st, uid_t uid)
736{
737 if (!platform_sys_dir_uid(st->st_uid) && st->st_uid != uid)
738 return 0;
739 if ((st->st_mode & 002) != 0)
740 return 0;
741 if ((st->st_mode & 020) != 0) {
742 /* If the file is group-writable, the group in question must
743 * have exactly one member, namely the file's owner.
744 * (Zero-member groups are typically used by setgid
745 * binaries, and are unlikely to be suitable.)
746 */
747 struct passwd *pw;
748 struct group *gr;
749 int members = 0;
750
751 gr = getgrgid(st->st_gid);
752 if (!gr)
753 return 0;
754
755 /* Check primary group memberships. */
756 while ((pw = getpwent()) != NULL) {
757 if (pw->pw_gid == gr->gr_gid) {
758 ++members;
759 if (pw->pw_uid != uid)
760 return 0;
761 }
762 }
763 endpwent();
764
765 pw = getpwuid(st->st_uid);
766 if (!pw)
767 return 0;
768
769 /* Check supplementary group memberships. */
770 if (gr->gr_mem[0]) {
771 ++members;
772 if (strcmp(pw->pw_name, gr->gr_mem[0]) ||
773 gr->gr_mem[1])
774 return 0;
775 }
776
777 if (!members)
778 return 0;
779 }
780 return 1;
781}
782
716int 783int
717tun_open(int tun, int mode) 784tun_open(int tun, int mode)
718{ 785{