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>2016-12-23 11:25:02 +0000
commitdf060c830ad66289a93be24268f3f70e7021be29 (patch)
tree4c41c8ff0deed41d671309af897a3ec6ca069668 /misc.c
parent7083ae25ccce8bbdad40e7c7500f69f2c0cbce34 (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 65c9222aa..bf9153a61 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 *
@@ -708,6 +710,71 @@ read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz,
708 return -1; 710 return -1;
709} 711}
710 712
713/*
714 * return 1 if the specified uid is a uid that may own a system directory
715 * otherwise 0.
716 */
717int
718platform_sys_dir_uid(uid_t uid)
719{
720 if (uid == 0)
721 return 1;
722#ifdef PLATFORM_SYS_DIR_UID
723 if (uid == PLATFORM_SYS_DIR_UID)
724 return 1;
725#endif
726 return 0;
727}
728
729int
730secure_permissions(struct stat *st, uid_t uid)
731{
732 if (!platform_sys_dir_uid(st->st_uid) && st->st_uid != uid)
733 return 0;
734 if ((st->st_mode & 002) != 0)
735 return 0;
736 if ((st->st_mode & 020) != 0) {
737 /* If the file is group-writable, the group in question must
738 * have exactly one member, namely the file's owner.
739 * (Zero-member groups are typically used by setgid
740 * binaries, and are unlikely to be suitable.)
741 */
742 struct passwd *pw;
743 struct group *gr;
744 int members = 0;
745
746 gr = getgrgid(st->st_gid);
747 if (!gr)
748 return 0;
749
750 /* Check primary group memberships. */
751 while ((pw = getpwent()) != NULL) {
752 if (pw->pw_gid == gr->gr_gid) {
753 ++members;
754 if (pw->pw_uid != uid)
755 return 0;
756 }
757 }
758 endpwent();
759
760 pw = getpwuid(st->st_uid);
761 if (!pw)
762 return 0;
763
764 /* Check supplementary group memberships. */
765 if (gr->gr_mem[0]) {
766 ++members;
767 if (strcmp(pw->pw_name, gr->gr_mem[0]) ||
768 gr->gr_mem[1])
769 return 0;
770 }
771
772 if (!members)
773 return 0;
774 }
775 return 1;
776}
777
711int 778int
712tun_open(int tun, int mode) 779tun_open(int tun, int mode)
713{ 780{