Skip to content

Commit

Permalink
[control] Use current user on seat0 to select modes. Contributes to J…
Browse files Browse the repository at this point in the history
…B#49457

Use the actual current user on seat0 to select modes. This will prevent
accidental or malicious ways to trick user to use other user's mode.

Signed-off-by: Tomi Leppänen <tomi.leppanen@jolla.com>
  • Loading branch information
Tomin1 committed Jun 3, 2020
1 parent bc813a6 commit 9bfcbc8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
40 changes: 27 additions & 13 deletions src/usb_moded-control.c
Expand Up @@ -34,6 +34,15 @@
#include <string.h>
#include <stdlib.h>

#ifdef SYSTEMD
# include <systemd/sd-login.h>
#endif

/* Sanity check, configure should take care of this */
#if defined SAILFISH_ACCESS_CONTROL && !defined SYSTEMD
# error if SAILFISH_ACCESS_CONTROL is defined, SYSTEMD must be defined as well
#endif

/* ========================================================================= *
* Prototypes
* ========================================================================= */
Expand All @@ -59,7 +68,7 @@ void control_set_cable_state (cable_state_t cable_state);
cable_state_t control_get_cable_state (void);
void control_clear_cable_state (void);
bool control_get_connection_state (void);
void control_set_last_seen_user (uid_t uid);
uid_t control_get_current_user (void);

/* ========================================================================= *
* Data
Expand Down Expand Up @@ -91,12 +100,6 @@ static char *control_internal_mode = NULL;
*/
static cable_state_t control_cable_state = CABLE_STATE_UNKNOWN;

/** Last user seen
*
* Defaults to invalid user which has no rights.
*/
static uid_t control_last_seen_user = (uid_t)-1;

/* ========================================================================= *
* Functions
* ========================================================================= */
Expand Down Expand Up @@ -340,17 +343,18 @@ void control_select_usb_mode(void)
goto EXIT;
}

uid_t current_user = control_get_current_user();
mode_to_set = config_get_mode_setting();

/* If there is only one allowed mode, use it without
* going through ask-mode */
if( !strcmp(MODE_ASK, mode_to_set) ) {
if( control_last_seen_user == (uid_t)-1 ) {
if( current_user == UID_UNKNOWN ) {
/* Use charging only if no user has been seen */
free(mode_to_set), mode_to_set = 0;
} else {
// FIXME free() vs g_free() conflict
gchar *available = common_get_mode_list(AVAILABLE_MODES_LIST, control_last_seen_user);
gchar *available = common_get_mode_list(AVAILABLE_MODES_LIST, current_user);
if( *available && !strchr(available, ',') ) {
free(mode_to_set), mode_to_set = available, available = 0;
}
Expand Down Expand Up @@ -445,11 +449,21 @@ bool control_get_connection_state(void)
return connected;
}

/** Set the last seen user
/**
* Get the user using the device
*
* When built without Sailfish access control support,
* this returns root's uid (0) unconditionally.
*
* @param uid of last seen user, controls implicitly set modes
* @return current user on seat0 or UID_UNKNOWN if it can not be determined
*/
void control_set_last_seen_user(uid_t uid)
uid_t control_get_current_user(void)
{
control_last_seen_user = uid;
#ifdef SAILFISH_ACCESS_CONTROL
uid_t uid = UID_UNKNOWN;
sd_seat_get_active("seat0", 0, &uid);
return uid;
#else
return 0;
#endif
}
2 changes: 1 addition & 1 deletion src/usb_moded-control.h
Expand Up @@ -50,6 +50,6 @@ void control_set_cable_state (cable_state_t cable_state);
cable_state_t control_get_cable_state (void);
void control_clear_cable_state (void);
bool control_get_connection_state (void);
void control_set_last_seen_user (uid_t uid);
uid_t control_get_current_user (void);

#endif /* USB_MODED_CONTROL_H_ */
2 changes: 0 additions & 2 deletions src/usb_moded-dbus.c
Expand Up @@ -581,8 +581,6 @@ static DBusHandlerResult umdbus_msg_handler(DBusConnection *const connection, DB
if((reply = dbus_message_new_method_return(msg)))
dbus_message_append_args (reply, DBUS_TYPE_STRING, (const char *) &mode_list, DBUS_TYPE_INVALID);
g_free(mode_list);

control_set_last_seen_user(uid);
}
else if(!strcmp(member, USB_MODE_RESCUE_OFF))
{
Expand Down

0 comments on commit 9bfcbc8

Please sign in to comment.