Skip to content

Commit 4a40fa0

Browse files
committed
dix: Opt-in keyboard isolation from unfocused windows.
Adds "IsolateKeyboard" option to "ServerFlags" section. If enabled: Disallows raw keyboard events and prevents keyboard input events to unfocused windows and clients without window to prevent keylogging. If disabled: Follows default X behavior. Signed-off-by: itz-me-zappex <[email protected]>
1 parent 116aa93 commit 4a40fa0

File tree

9 files changed

+55
-0
lines changed

9 files changed

+55
-0
lines changed

dix/events.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2489,6 +2489,21 @@ DeliverRawEvent(RawDeviceEvent *ev, DeviceIntPtr device)
24892489
if (grab)
24902490
DeliverGrabbedEvent((InternalEvent *) ev, device, FALSE);
24912491

2492+
/*
2493+
* To prevent keylogging, not grabbed raw keyboard event
2494+
* should not be sent to any client.
2495+
*/
2496+
if (globalIsolateKeyboard) {
2497+
switch (ev->type) {
2498+
case ET_RawKeyPress:
2499+
case ET_RawKeyRelease:
2500+
free(xi);
2501+
return;
2502+
default:
2503+
break;
2504+
}
2505+
}
2506+
24922507
filter = GetEventFilter(device, xi);
24932508

24942509
DIX_FOR_EACH_SCREEN({
@@ -2828,6 +2843,25 @@ static int
28282843
DeliverOneEvent(InternalEvent *event, DeviceIntPtr dev, enum InputLevel level,
28292844
WindowPtr win, Window child, GrabPtr grab)
28302845
{
2846+
/*
2847+
* Deliver keyboard events only if client is focused.
2848+
* Even if it does not have a window, that means unfocused.
2849+
* Needed to prevent keylogging.
2850+
*/
2851+
if (globalIsolateKeyboard) {
2852+
WindowPtr focus = inputInfo.keyboard->focus->win;
2853+
2854+
if (focus != win) {
2855+
switch (event->any.type) {
2856+
case ET_KeyPress:
2857+
case ET_KeyRelease:
2858+
return 0;
2859+
default:
2860+
break;
2861+
}
2862+
}
2863+
}
2864+
28312865
xEvent *xE = NULL;
28322866
int count = 0;
28332867
int deliveries = 0;

dix/globals.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,5 @@ int monitorResolution = 0;
122122

123123
Bool explicit_display = FALSE;
124124
char *ConnectionInfo;
125+
126+
Bool globalIsolateKeyboard = FALSE;

hw/xfree86/common/xf86Config.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@ typedef enum {
658658
FLAG_DEBUG,
659659
FLAG_ALLOW_BYTE_SWAPPED_CLIENTS,
660660
FLAG_SINGLE_DRIVER,
661+
FLAG_ISOLATEKEYBOARD,
661662
} FlagValues;
662663

663664
/**
@@ -721,6 +722,8 @@ static OptionInfoRec FlagOptions[] = {
721722
{0}, FALSE},
722723
{FLAG_SINGLE_DRIVER, "SingleDriver", OPTV_BOOLEAN,
723724
{0}, FALSE},
725+
{FLAG_ISOLATEKEYBOARD, "IsolateKeyboard", OPTV_BOOLEAN,
726+
{0}, FALSE},
724727
{-1, NULL, OPTV_NONE,
725728
{0}, FALSE},
726729
};
@@ -757,6 +760,9 @@ configServerFlags(XF86ConfFlagsPtr flagsconf, XF86OptionPtr layoutopts)
757760
xf86GetOptValBool(FlagOptions, FLAG_DONTZAP, &xf86Info.dontZap);
758761
xf86GetOptValBool(FlagOptions, FLAG_DONTZOOM, &xf86Info.dontZoom);
759762

763+
xf86GetOptValBool(FlagOptions, FLAG_ISOLATEKEYBOARD, &xf86Info.isolateKeyboard);
764+
globalIsolateKeyboard = xf86Info.isolateKeyboard;
765+
760766
xf86GetOptValBool(FlagOptions, FLAG_IGNORE_ABI, &xf86Info.ignoreABI);
761767
if (xf86Info.ignoreABI) {
762768
LogMessageVerb(X_CONFIG, 1, "Ignoring ABI Version\n");

hw/xfree86/common/xf86Globals.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ xf86InfoRec xf86Info = {
132132
#endif
133133
.autoBindGPU = TRUE,
134134
.singleDriver = FALSE,
135+
.isolateKeyboard = FALSE,
135136
};
136137

137138
const char *xf86ConfigFile = NULL;

hw/xfree86/common/xf86Privstr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ typedef struct {
9797

9898
Bool singleDriver; /* Only the first successfully probed driver adds primary screens,
9999
* others may add GPU secondary screens only */
100+
101+
Bool isolateKeyboard;
100102
} xf86InfoRec, *xf86InfoPtr;
101103

102104
/* ISC's cc can't handle ~ of UL constants, so explicitly type cast them. */

hw/xfree86/man/xorg.conf.man

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,11 @@ Allow clients with a different byte-order than the server. Disabled by default.
692692
Only the first successfully probed driver is allowed to add screens to the current layout, others may
693693
add secondary GPU screens only (e.g., if non-primary GPUs are used for offloading).
694694
Disabled by default.
695+
.TP 7
696+
.BI "Option \*qIsolateKeyboard\*q \*q" boolean \*q
697+
Disallows not grabbed raw keyboard events and keyboard input events to
698+
both unfocused windows and clients without window preventing keylogging.
699+
Disabled by default.
695700
.SH "MODULE SECTION"
696701
The
697702
.B Module

hw/xfree86/parser/Flags.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ static const xf86ConfigSymTabRec ServerFlagsTab[] = {
8080
{SUSPENDTIME, "suspendtime"},
8181
{OFFTIME, "offtime"},
8282
{DEFAULTLAYOUT, "defaultserverlayout"},
83+
{ISOLATEKEYBOARD, "isolatekeyboard"},
8384
{-1, ""},
8485
};
8586

@@ -127,6 +128,7 @@ xf86parseFlagsSection(XF86ConfFlagsPtr ptr)
127128
case DISABLEMODINDEV:
128129
case MODINDEVALLOWNONLOCAL:
129130
case ALLOWMOUSEOPENFAIL:
131+
case ISOLATEKEYBOARD:
130132
{
131133
int i = 0;
132134

hw/xfree86/parser/xf86tokens.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ typedef enum {
107107
SUSPENDTIME,
108108
OFFTIME,
109109
DEFAULTLAYOUT,
110+
ISOLATEKEYBOARD,
110111

111112
/* Monitor tokens */
112113
MODEL,

include/globals.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ extern _X_EXPORT int defaultColorVisualClass;
1212

1313
extern _X_EXPORT char *SeatId;
1414

15+
extern _X_EXPORT Bool globalIsolateKeyboard;
16+
1517
#endif /* !_XSERV_GLOBAL_H_ */

0 commit comments

Comments
 (0)