Skip to content

Commit 3a87a86

Browse files
committed
audit: introduce audit_session_is_valid() and make use of it everywhere
Let's add a proper validation function, since validation isn't entirely trivial. Make use of it where applicable. Also make use of AUDIT_SESSION_INVALID where we need a marker for an invalid audit session.
1 parent ab7e3ef commit 3a87a86

File tree

5 files changed

+14
-10
lines changed

5 files changed

+14
-10
lines changed

src/basic/audit-util.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ int audit_session_from_pid(pid_t pid, uint32_t *id) {
5454
if (r < 0)
5555
return r;
5656

57-
if (u == AUDIT_SESSION_INVALID || u <= 0)
57+
if (!audit_session_is_valid(u))
5858
return -ENODATA;
5959

6060
*id = u;
@@ -81,7 +81,7 @@ int audit_loginuid_from_pid(pid_t pid, uid_t *uid) {
8181
if (r < 0)
8282
return r;
8383

84-
*uid = (uid_t) u;
84+
*uid = u;
8585
return 0;
8686
}
8787

src/basic/audit-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ int audit_session_from_pid(pid_t pid, uint32_t *id);
2929
int audit_loginuid_from_pid(pid_t pid, uid_t *uid);
3030

3131
bool use_audit(void);
32+
33+
static inline bool audit_session_is_valid(uint32_t id) {
34+
return id > 0 && id != AUDIT_SESSION_INVALID;
35+
}

src/libsystemd/sd-bus/bus-creds.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ _public_ int sd_bus_creds_get_audit_session_id(sd_bus_creds *c, uint32_t *sessio
570570
if (!(c->mask & SD_BUS_CREDS_AUDIT_SESSION_ID))
571571
return -ENODATA;
572572

573-
if (c->audit_session_id == AUDIT_SESSION_INVALID)
573+
if (!audit_session_is_valid(c->audit_session_id))
574574
return -ENXIO;
575575

576576
*sessionid = c->audit_session_id;

src/login/logind-dbus.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -767,8 +767,8 @@ static int method_create_session(sd_bus_message *message, void *userdata, sd_bus
767767
if (hashmap_size(m->sessions) >= m->sessions_max)
768768
return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of sessions (%" PRIu64 ") reached, refusing further sessions.", m->sessions_max);
769769

770-
audit_session_from_pid(leader, &audit_id);
771-
if (audit_id > 0) {
770+
(void) audit_session_from_pid(leader, &audit_id);
771+
if (audit_session_is_valid(audit_id)) {
772772
/* Keep our session IDs and the audit session IDs in sync */
773773

774774
if (asprintf(&id, "%"PRIu32, audit_id) < 0)
@@ -780,7 +780,7 @@ static int method_create_session(sd_bus_message *message, void *userdata, sd_bus
780780
* ID */
781781
if (hashmap_get(m->sessions, id)) {
782782
log_warning("Existing logind session ID %s used by new audit session, ignoring", id);
783-
audit_id = 0;
783+
audit_id = AUDIT_SESSION_INVALID;
784784

785785
id = mfree(id);
786786
}

src/login/logind-session.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Session* session_new(Manager *m, const char *id) {
8282
s->manager = m;
8383
s->fifo_fd = -1;
8484
s->vtfd = -1;
85+
s->audit_id = AUDIT_SESSION_INVALID;
8586

8687
return s;
8788
}
@@ -283,7 +284,7 @@ int session_save(Session *s) {
283284
if (s->leader > 0)
284285
fprintf(f, "LEADER="PID_FMT"\n", s->leader);
285286

286-
if (s->audit_id > 0)
287+
if (audit_session_is_valid(s->audit_id))
287288
fprintf(f, "AUDIT=%"PRIu32"\n", s->audit_id);
288289

289290
if (dual_timestamp_is_set(&s->timestamp))
@@ -459,9 +460,8 @@ int session_load(Session *s) {
459460
}
460461

461462
if (leader) {
462-
k = parse_pid(leader, &s->leader);
463-
if (k >= 0)
464-
audit_session_from_pid(s->leader, &s->audit_id);
463+
if (parse_pid(leader, &s->leader) >= 0)
464+
(void) audit_session_from_pid(s->leader, &s->audit_id);
465465
}
466466

467467
if (type) {

0 commit comments

Comments
 (0)