-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME
More file actions
642 lines (407 loc) · 22.4 KB
/
Copy pathREADME
File metadata and controls
642 lines (407 loc) · 22.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
RFC: Modernizing System Authentication Logs (lastlog, btmp, utmp, wtmp) with SQLite
Status: Individual Submission (Draft)
Author: Roman Bakshansky
Date: March 12, 2026
Discussion: <https://github.com/bakshansky/linux-auth-logs>
1. Introduction
The system logs lastlog (last login time), btmp (failed login
attempts), utmp (current sessions), and wtmp (login/logout history)
are fundamental components of security auditing and monitoring in
Linux. Their formats were defined in the 1980s and have remained
virtually unchanged since then. All of them use fixed-structure
records that include a 32-bit time field (time_t in lastlog, tv_sec
in utmpx). This creates a number of serious problems that become
critical as the year 2038 approaches.
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
"SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and
"OPTIONAL" in this document are to be interpreted as described in
BCP 14 [RFC2119] [RFC8174] when, and only when, they appear in all
capitals, as shown here.
1.1. The Year 2038 Problem (Y2038)
On January 19, 2038, the 32-bit seconds counter will overflow. Even
on 64-bit systems, due to ABI compatibility requirements, the time
fields remain 32-bit. Therefore, all Linux systems, regardless of
architecture, are affected by this problem.
1.2. Other Systemic Limitations
The existing formats suffer from additional limitations that make
them unsuitable for modern requirements:
- Lack of extensibility. Adding any new field (e.g., container ID,
service name, source IP) requires changing the structure and
recompiling all programs that work with these files. In practice,
this makes format evolution impossible.
- Poor query performance. Utilities (last, lastb, who, lastlog) are
forced to scan files linearly. When logs grow to hundreds of
megabytes or gigabytes, this leads to unacceptable delays and
excessive disk I/O load.
- No atomicity or integrity. Writing to a binary file is not an
atomic operation. If a writing process crashes in the middle of
an operation, the file can become corrupted.
- Concurrency problems. Multiple processes may try to write to the
same file simultaneously (e.g., sshd and login during concurrent
logins). Traditional file locking (flock) addresses contention
but does not guarantee atomicity and can lead to deadlocks.
1.3. Scope
This document proposes a complete replacement of the legacy binary
logs with specialized libraries that use SQLite as the embedded
storage engine. The proposed solution covers all four types of logs
and forms a unified ecosystem for managing authentication data. It
is intended for discussion within the Linux community and does not
currently represent any stream's official position.
Bakshansky [Page 1]
2. Current State and Its Limitations
2.1. Data Formats
- lastlog (usually /var/log/lastlog): a file with fixed-size records,
each corresponding to one UID. The struct lastlog (from
<lastlog.h>) contains:
struct lastlog {
time_t ll_time; /* last login time, 32 bits */
char ll_line[UT_LINESIZE];
char ll_host[UT_HOSTSIZE];
};
- btmp (/var/log/btmp): the log of failed login attempts. It uses
the same structure as utmp/wtmp.
- utmp (/var/run/utmp): current sessions.
- wtmp (/var/log/wtmp): login/logout history.
For utmp/wtmp/btmp, the struct utmpx defined in <utmpx.h> is used.
According to POSIX, it must contain at least:
struct utmpx {
char ut_user[]; /* user login name */
char ut_id[]; /* unspecified initialization process
identifier */
char ut_line[]; /* device name (terminal) */
pid_t ut_pid; /* process ID */
short ut_type; /* type of entry */
struct timeval ut_tv; /* time entry was made */
};
The ut_tv field is of type struct timeval, where tv_sec is int32_t.
Most implementations also include fields like ut_host and others,
but these are not standardized.
2.2. Lack of Extensibility
A fixed record size means that adding any new field (e.g.,
container_id, service_name, source_ip) requires changing the
structure, which immediately breaks backward compatibility. Old
programs reading the file would misinterpret the new data, leading
to errors or crashes. As a result, the format has remained frozen
for decades, unable to reflect system evolution.
2.3. Query Performance
Utilities that work with these files MUST read them sequentially.
For example:
- last MUST traverse the entire wtmp file from end to beginning to
obtain a list of recent logins for a user. With millions of
records, this takes seconds and creates significant disk I/O load.
- lastb MUST scan the whole btmp file to count failed attempts.
- lastlog reads the record at offset UID * sizeof(struct lastlog),
which is efficient for single queries but does not support complex
queries like "show all users who logged in after a given date"
(that would require a full file scan).
Bakshansky [Page 2]
2.4. Integrity and Atomicity
Writing to a binary file is not atomic. If a process crashes in the
middle of a write, the file MAY end up partially written – a portion
of the record is written, the rest is not. For security auditing,
losing or corrupting even a single record is unacceptable. File
locking mechanisms (flock) do not solve atomicity; they only prevent
concurrent writes.
2.5. Concurrency
Multiple processes MAY try to write to the same file concurrently
(e.g., sshd and login during simultaneous logins). Traditional file
locking addresses contention but does not guarantee record-level
atomicity and can lead to deadlocks.
3. Requirements for a New Solution
A new system for storing session and authentication data MUST meet
the following requirements:
REQ1: Time scale. It MUST support time beyond 2038 using a 64-bit
representation.
REQ2: Extensibility. It MUST allow adding new fields without
breaking existing software.
REQ3: Query performance. It SHOULD provide indexes for common
queries (by user, time range, event type). Queries of the
form "last N records for user X since time Y" MUST execute in
better than linear time.
REQ4: Atomicity and integrity. It MUST guarantee that each record
is either fully stored or not stored at all, even in the event
of a system crash. This implies ACID compliance.
REQ5: Concurrency. It MUST support simultaneous writes by multiple
processes without data loss and with minimal locking.
REQ6: Portability. It MUST work on all Linux systems, including
embedded systems and musl-based containers, regardless of the
presence of systemd.
REQ7: Backward compatibility. It MUST allow a gradual transition
without breaking existing tools and scripts.
REQ8: Uniformity. All logs SHOULD be managed through similar
interfaces to ease learning and maintenance.
4. Proposed Solution: SQLite-Based Libraries
We propose using SQLite – an embedded relational database management
system that is widely used, in the public domain, and has no
external dependencies. SQLite is ideal for system programming due
to its small footprint, reliability, and rich feature set.
For each of the four log types, a separate public shared library
with a corresponding C interface is created:
- liblastlog2 – for storing the last login time (replaces
/var/log/lastlog).
Bakshansky [Page 3]
- libbtmp2 – for the failed login attempts log (replaces
/var/log/btmp).
- libutmp2 – for current sessions (replaces /var/run/utmp).
- libwtmp2 – for login/logout history (replaces /var/log/wtmp).
All libraries follow a common approach but MAY have different table
schemas optimized for their specific tasks. They MAY be implemented
as separate shared libraries or combined into one with different
entry points – this is open for discussion.
The libraries provide functions for initialization, adding records,
executing parameterized queries, and database maintenance (purging
old records, optimization).
4.1. Preliminary Database Schema (Subject to Discussion)
Below is a draft of the table structures. This is not a final
version – it is open for discussion and MAY be changed based on
community input.
For event logs (btmp, utmp, wtmp) a common table events is proposed
with the following fields:
Field SQLite Type Semantics
============ ============== ================================
id INTEGER PRIMARY Unique record identifier
KEY AUTOINCREMENT
timestamp INTEGER Event time in microseconds since
epoch (64-bit)
type INTEGER Record type (login, logout,
reboot, etc.)
pid INTEGER Process ID
user TEXT Username
line TEXT Terminal (e.g., "tty1", "pts/0")
host TEXT Remote host (may be empty)
service TEXT Name of the service that created
the record (e.g., "sshd", "login")
– new field
source_ip TEXT Source IP address (if applicable)
– new field
container TEXT Container identifier (if
applicable) – new field
For lastlog2 – a lastlog table with the user as the primary key:
Field SQLite Type Semantics
============== ============== ==============================
user TEXT PRIMARY Username
KEY
last_timestamp INTEGER Last login time (microseconds)
last_line TEXT Last terminal
last_host TEXT Last remote host
last_service TEXT Last service
last_source_ip TEXT Last source IP
Bakshansky [Page 4]
Indexes SHOULD be created for the most frequent queries. Example:
CREATE INDEX idx_user_time ON events(user, timestamp);
CREATE INDEX idx_time ON events(timestamp);
CREATE INDEX idx_service ON events(service);
These indexes will enable queries like "last 10 records for user X"
to execute in logarithmic time.
4.2. Preliminary API (Subject to Discussion)
Below is a draft of the API for libwtmp2 (similar for other
libraries). This is not a final version – only a starting point for
discussion. All details (function names, parameter types, error
handling) MAY be changed.
/* Open/create the database.
path – full path to the database file (e.g.,
"/var/lib/wtmp/wtmp.db").
flags – combination of flags: O_RDONLY, O_RDWR, O_CREAT.
Returns 0 on success, -1 on error (errno is set). */
int wtmp_open(const char *path, int flags);
/* Close the database. */
void wtmp_close(void);
/* Add an event record. Parameters correspond to table fields.
Optional parameters (e.g., container) MAY be NULL.
Returns 0 on success, -1 on error. */
int wtmp_add(int type, pid_t pid, const char *user,
const char *line, const char *host,
const char *service, const char *source_ip,
const char *container);
/* Retrieve the last N records for a user starting from a given
time. The result is returned as an array of structures that
MUST be freed by the caller using wtmp_free_entries().
Returns the number of records or -1 on error. */
ssize_t wtmp_get_recent(const char *user,
uint64_t since_timestamp,
int limit,
struct wtmp_entry **entries);
/* Get the number of failed login attempts for a user after a
given time. (Useful for PAM modules similar to
pam_lastlog2.) */
int wtmp_get_failed_count(const char *user,
uint64_t since_timestamp,
uint64_t *count);
/* Delete records older than a given timestamp (for log
rotation). */
int wtmp_prune(uint64_t before_timestamp);
/* Optimize the database (VACUUM, rebuild indexes). */
int wtmp_maintenance(void);
/* Free memory allocated for entries. */
void wtmp_free_entries(struct wtmp_entry *entries,
size_t count);
Bakshansky [Page 5]
For liblastlog2 the API might look like:
/* Open/create the lastlog database.
path – full path to the database file (e.g.,
"/var/lib/lastlog/lastlog.db").
flags – combination of flags: O_RDONLY, O_RDWR, O_CREAT.
Returns 0 on success, -1 on error (errno is set). */
int lastlog_open(const char *path, int flags);
/* Close the lastlog database. */
void lastlog_close(void);
/* Update the last login information for a user.
If the user does not exist, a new record is created.
Returns 0 on success, -1 on error. */
int lastlog_update(const char *user, uint64_t timestamp,
const char *line, const char *host,
const char *service, const char *source_ip);
/* Query the last login information for a user.
The result is returned in the provided structure.
Returns 0 on success, -1 if the user is not found or on error. */
int lastlog_query(const char *user,
struct lastlog_entry *entry);
/* Get a list of all users who have logged in since a given time.
The result is returned as an array of strings that MUST be
freed by the caller using lastlog_free_users().
Returns the number of users or -1 on error. */
int lastlog_get_all_users_since(uint64_t since_timestamp,
char ***users, size_t *count);
/* Free memory allocated for the user list. */
void lastlog_free_users(char **users, size_t count);
All functions SHOULD be designed with multithreading in mind:
except for open/close, they SHOULD be reentrant and MAY be called
concurrently from different threads, because SQLite in WAL mode
supports concurrent reads and one write transaction.
4.3. Advantages of Using SQLite
- 64-bit time. The INTEGER type in SQLite is stored as a signed
64-bit value, permanently solving the Y2038 problem.
- Indexes. Enable complex queries in logarithmic time, radically
reducing disk I/O load.
- Extensibility. New columns can be added with ALTER TABLE; old
records remain readable, and old programs simply ignore unknown
columns.
- ACID. SQLite guarantees atomicity, consistency, isolation, and
durability. Even in the event of a system crash, a record is
never lost or partially written.
- Concurrency (WAL mode). Write-Ahead Logging allows multiple
simultaneous reads and one write, perfectly matching the scenario
with several writing processes.
- Portability. SQLite runs on any platform with a C compiler,
including embedded systems and musl environments.
- Proven. SQLite is used by billions of devices and applications;
its reliability is well known.
Bakshansky [Page 6]
5. Comparison with Alternative Approaches
5.1. Simple Extension of the Binary Format
One could define new structures with 64-bit time and enlarged
fields. However, such an approach:
- does not solve the extensibility problem;
- provides no indexes;
- does not guarantee atomicity;
- requires synchronizing changes across dozens of projects, which is
practically impossible.
5.2. Using systemd-journald
journald collects all events, but:
- it is tied to systemd, leaving systems without systemd out of the
solution;
- it requires libsystemd, complicating tools;
- it lacks a simple file-based interface;
- it does not guarantee ACID at the individual record level.
5.3. Keeping the Status Quo
Doing nothing guarantees a Y2038 disaster and retains all the
existing shortcomings. This option is unacceptable.
5.4. Why SQLite Is the Optimal Solution
SQLite simultaneously solves all identified problems: Y2038,
performance, extensibility, integrity, concurrency, and portability.
No other approach offers the same set of advantages in a single
solution.
6. Migration Plan
A dual-write strategy ensures a smooth transition.
1. Adapting writing programs. Programs that currently write to
lastlog, btmp, utmp, wtmp (e.g., login, sshd, su, sudo, cron)
SHOULD be modified to write both to the old binary file (using
existing mechanisms) and to the new SQLite database via the
appropriate library. The changes are minimal – a call to
*_add() after the traditional write.
2. Developing new reading utilities. New versions of utilities
(last2, lastb2, who2, lastlog2) SHOULD be created that read from
the SQLite databases and utilize indexes. Old utilities
continue to work with the old files.
3. Distribution switchover. Distributions MAY ship the new
utilities alongside the old ones and include patches for dual
writes. After 2–3 years, most systems will have migrated to the
new libraries.
4. Deprecating the old formats. When the fraction of systems
relying on the old files becomes negligible, writing to them MAY
be disabled, and support for reading them MAY eventually be
removed from standard utilities.
Backward compatibility is maintained at all stages.
Bakshansky [Page 7]
7. Open Questions for Discussion
1. Separation or unification of libraries. Should we create
separate libraries or one common library (e.g., libsession2)?
2. Naming. What names should the new libraries and utilities have?
Keep historical names (lastlog2, btmp2) or choose more generic
ones?
3. Database location. Store in /var/lib/ (as application state) or
in /var/log/? Given that databases are updated, not merely
appended, /var/lib/ seems more appropriate.
4. Versioning scheme. Use PRAGMA user_version to track schema
versions and apply migration scripts?
5. Internationalization. Store strings in UTF-8, converting legacy
8-bit data if necessary?
6. Performance under high load. Which SQLite settings (synchronous,
cache_size, journal_mode) are optimal for servers and embedded
systems?
7. Security. Should encryption or integrity checks be considered?
Access permissions (group adm)?
8. Integration with PAM and NSS. Are new PAM modules needed? Can
data be exposed via NSS?
9. Fallback to a binary format for systems without SQLite. There
are embedded systems with tight memory or code size constraints
where SQLite might be too heavy. Should the libraries provide a
simplified binary backend (with 64-bit time and fixed records)
as a fallback? If so, how to keep the API uniform? With a
binary backend, indexes and some functionality would be lost,
but basic operations (add, read recent records) could remain.
This would require a more complex library implementation
(backend choice at compile time or via an environment variable).
8. Security Considerations
The proposed solution significantly improves the security posture of
system authentication logs. ACID compliance ensures that logs
cannot be partially corrupted during system crashes. Indexes and
structured queries allow for faster forensic analysis.
However, the introduction of SQLite also introduces new
considerations:
- File permissions MUST be set appropriately (e.g., group adm) to
prevent unauthorized reading or modification.
- The SQLite library SHOULD be kept up-to-date to address any
security vulnerabilities.
- If encryption is not implemented, the database files remain
readable by anyone with file system access. Implementations MAY
consider using encrypted database formats or file-system-level
encryption where required.
The fallback binary option (if adopted) MUST maintain the same
security properties as the SQLite backend to the greatest extent
possible.
9. IANA Considerations
This document has no IANA actions.
Bakshansky [Page 8]
10. Acknowledgements
The author would like to thank the Linux community for their
continued work on system auditing and for providing feedback on this
proposal.
11. References
11.1. Normative References
[RFC2119] Bradner, S., "Key words for use in RFCs to Indicate
Requirement Levels", BCP 14, RFC 2119,
DOI 10.17487/RFC2119, March 1997,
<https://www.rfc-editor.org/info/rfc2119>.
[RFC8174] Leiba, B., "Ambiguity of Uppercase vs Lowercase in
RFC 2119 Key Words", BCP 14, RFC 8174,
DOI 10.17487/RFC8174, May 2017,
<https://www.rfc-editor.org/info/rfc8174>.
[SQLite] "SQLite", <https://www.sqlite.org/>.
Author's Address
Roman Bakshansky
Email: bakshansky@protonmail.com
Email (for mailing lists): bakshansky.lists@gmail.com
GitHub: https://github.com/bakshansky
Discussion: https://github.com/bakshansky/linux-auth-logs
Bakshansky [Page 9]