Index: sudo-1.7.6p2/check.c
===================================================================
--- sudo-1.7.6p2.orig/check.c	2013-04-22 12:12:24.085341083 +0200
+++ sudo-1.7.6p2/check.c	2013-04-22 12:12:24.100341548 +0200
@@ -82,6 +82,7 @@ static struct tty_info {
     dev_t rdev;			/* tty device ID */
     ino_t ino;			/* tty inode number */
     struct timeval ctime;	/* tty inode change time */
+    pid_t sid;			/* ID of session with controlling tty */
 } tty_info;
 
 static void  build_timestamp	__P((char **, char **));
@@ -107,13 +108,14 @@ check_user(validated, mode)
     struct stat sb;
     int status;
 
-    /* Stash the tty's ctime for tty ticket comparison. */
+    /* Stash the tty's device, session ID and ctime for ticket comparison. */
     if (def_tty_tickets && user_ttypath && stat(user_ttypath, &sb) == 0) {
 	tty_info.dev = sb.st_dev;
 	tty_info.ino = sb.st_ino;
 	tty_info.rdev = sb.st_rdev;
 	if (tty_is_devpts(user_ttypath))
 	    ctim_get(&sb, &tty_info.ctime);
+	tty_info.sid = user_sid;
     }
 
     /* Always prompt for a password when -k was specified with the command. */
Index: sudo-1.7.6p2/config.h.in
===================================================================
--- sudo-1.7.6p2.orig/config.h.in	2011-02-19 14:18:06.000000000 +0100
+++ sudo-1.7.6p2/config.h.in	2013-04-22 12:12:24.101341579 +0200
@@ -173,6 +173,9 @@
    passwords) */
 #undef HAVE_GETPWANAM
 
+/* Define to 1 if you have the `getsid' function. */
+#undef HAVE_GETSID
+
 /* Define to 1 if you have the `getspnam' function (SVR4-style shadow
    passwords) */
 #undef HAVE_GETSPNAM
Index: sudo-1.7.6p2/configure
===================================================================
--- sudo-1.7.6p2.orig/configure	2011-05-16 19:38:16.000000000 +0200
+++ sudo-1.7.6p2/configure	2013-04-22 12:12:24.106341733 +0200
@@ -15128,7 +15128,7 @@ LIBS=$ac_save_LIBS
 
 for ac_func in strchr strrchr memchr memcpy memset sysconf tzset \
 	       strftime setrlimit initgroups getgroups fstat gettimeofday \
-	       regcomp setlocale nl_langinfo getaddrinfo setenv \
+	       regcomp setlocale nl_langinfo getaddrinfo getsid setenv \
 	       mbr_check_membership setrlimit64
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
Index: sudo-1.7.6p2/configure.in
===================================================================
--- sudo-1.7.6p2.orig/configure.in	2013-04-22 12:12:23.929336250 +0200
+++ sudo-1.7.6p2/configure.in	2013-04-22 12:12:24.107341764 +0200
@@ -1972,7 +1972,7 @@ dnl
 AC_FUNC_GETGROUPS
 AC_CHECK_FUNCS(strchr strrchr memchr memcpy memset sysconf tzset \
 	       strftime setrlimit initgroups getgroups fstat gettimeofday \
-	       regcomp setlocale nl_langinfo getaddrinfo setenv \
+	       regcomp setlocale nl_langinfo getaddrinfo getsid setenv \
 	       mbr_check_membership setrlimit64)
 AC_CHECK_FUNCS(getline, [], [
     AC_LIBOBJ(getline)
Index: sudo-1.7.6p2/sudo.c
===================================================================
--- sudo-1.7.6p2.orig/sudo.c	2011-03-16 21:01:00.000000000 +0100
+++ sudo-1.7.6p2/sudo.c	2013-04-29 16:34:28.446392972 +0200
@@ -629,8 +629,7 @@ init_vars(envp)
 	}
     }
 
-    if ((p = ttyname(STDIN_FILENO)) || (p = ttyname(STDOUT_FILENO)) ||
-	(p = ttyname(STDERR_FILENO))) {
+    if ((p = get_process_ttyname()) != NULL) {
 	user_tty = user_ttypath = estrdup(p);
 	if (strncmp(user_tty, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
 	    user_tty += sizeof(_PATH_DEV) - 1;
@@ -691,6 +690,9 @@ init_vars(envp)
 #ifdef HAVE_MBR_CHECK_MEMBERSHIP
     mbr_uid_to_uuid(user_uid, user_uuid);
 #endif
+#ifdef HAVE_GETSID
+    user_sid = getsid(0);
+#endif
     if (user_shell == NULL || *user_shell == '\0')
 	user_shell = estrdup(sudo_user.pw->pw_shell);
 
Index: sudo-1.7.6p2/sudo.h
===================================================================
--- sudo-1.7.6p2.orig/sudo.h	2011-02-07 15:40:44.000000000 +0100
+++ sudo-1.7.6p2/sudo.h	2013-04-29 16:44:18.398051796 +0200
@@ -60,6 +60,7 @@ struct sudo_user {
     char *krb5_ccname;
     char *display;
     char *askpass;
+    pid_t sid;
     int   ngroups;
     GETGROUPS_T *groups;
     struct list_member *env_vars;
@@ -162,6 +163,7 @@ struct command_status {
 #define user_shell		(sudo_user.shell)
 #define user_ngroups		(sudo_user.ngroups)
 #define user_groups		(sudo_user.groups)
+#define user_sid		(sudo_user.sid)
 #define user_tty		(sudo_user.tty)
 #define user_ttypath		(sudo_user.ttypath)
 #define user_cwd		(sudo_user.cwd)
@@ -350,6 +352,9 @@ char *get_timestr __P((time_t, int));
 #define YY_DECL int yylex __P((void))
 YY_DECL;
 
+/* ttyname.c */
+char *get_process_ttyname(void);
+
 /* zero_bytes.c */
 void zero_bytes		__P((volatile void *, size_t));
 
