Skip to content

Commit f9bd494

Browse files
committed
Add access attributes to buffer formatters
Several local helpers write into caller-supplied buffers but did not declare the relationship between the pointer argument and the size argument. That made it harder for GCC to diagnose undersized buffers at compile time and for fortified builds to catch misuse around functions such as format_fs_error_time. Add __attr_access write-only annotations to the filesystem error time formatter and similar local helpers that format status, timing, deferred-age, and CLI signal error text. Also reject buffers smaller than the requested minimum in format_fs_error_time before it writes fallback text.
1 parent dc7c3d9 commit f9bd494

5 files changed

Lines changed: 34 additions & 2 deletions

File tree

src/cli/fapolicyd-cli.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "fapolicyd-backend.h"
5252
#include "string-util.h"
5353
#include "daemon-config.h"
54+
#include "gcc-attributes.h"
5455
#include "message.h"
5556
#include "llist.h"
5657
#include "avl.h"
@@ -65,6 +66,15 @@ bool verbose = false;
6566
static bool lint_rules = false;
6667
static bool assume_yes = false;
6768

69+
static int send_state_report_signal(unsigned int pid, report_intent_t intent,
70+
char *reason, size_t reason_len)
71+
__attr_access ((__write_only__, 3, 4));
72+
static int send_timing_signal(unsigned int pid, report_intent_t intent,
73+
char *reason, size_t reason_len)
74+
__attr_access ((__write_only__, 3, 4));
75+
static int get_daemon_pid(unsigned int *pid, char *reason, size_t reason_len)
76+
__attr_access ((__write_only__, 2, 3));
77+
6878
static const char *usage =
6979
"Fapolicyd CLI Tool\n\n"
7080
"--check-config Check the daemon config for syntax errors\n"

src/daemon/decision-defer.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include <string.h>
5151
#include <time.h>
5252
#include "decision-defer.h"
53+
#include "gcc-attributes.h"
5354

5455
struct decision_defer_entry {
5556
/* Event envelope and permission fd owned while this entry is used. */
@@ -62,6 +63,9 @@ struct decision_defer_entry {
6263
int used;
6364
};
6465

66+
static void format_age(uint64_t age_ns, char *buf, size_t buf_size)
67+
__attr_access ((__write_only__, 2, 3));
68+
6569
/*
6670
* defer_now_ns - read monotonic time for defer age reporting.
6771
* Returns monotonic nanoseconds, or zero if the clock cannot be read.

src/daemon/fanotify-fs-error.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "escape.h"
5656
#include "failure-action.h"
5757
#include "fanotify-fs-error.h"
58+
#include "gcc-attributes.h"
5859
#include "message.h"
5960
#include "notify.h"
6061

@@ -107,7 +108,8 @@ static const char *fs_error_status(
107108
const struct fanotify_fs_error_details *details);
108109
static const char *fs_error_code_text(int error);
109110
static const char *format_fs_error_time(time_t when, char *buf,
110-
size_t buf_size);
111+
size_t buf_size)
112+
__attr_access ((__write_only__, 2, 3));
111113
#if FAPOLICYD_HAVE_FANOTIFY_FS_ERROR
112114
static int parse_fs_error_record(
113115
const struct fanotify_event_metadata *metadata,
@@ -173,7 +175,7 @@ static const char *format_fs_error_time(time_t when, char *buf,
173175
{
174176
struct tm tm;
175177

176-
if (buf_size == 0)
178+
if (buf_size < 11)
177179
return buf;
178180

179181
if (when == 0) {

src/daemon/state-report.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "decision-timing.h"
2424
#include "failure-action.h"
2525
#include "fanotify-fs-error.h"
26+
#include "gcc-attributes.h"
2627
#include "message.h"
2728
#include "notify.h"
2829
#include "paths.h"
@@ -39,6 +40,9 @@ extern conf_t config;
3940

4041
static time_t last_metrics_reset;
4142

43+
static const char *format_metrics_reset_time(char *buf, size_t buf_size)
44+
__attr_access ((__write_only__, 1, 2));
45+
4246
/*
4347
* state_report_operating_mode - write health and control state.
4448
* @f: report stream.

src/library/decision-timing.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#include <time.h>
6363
#include <unistd.h>
6464
#include "decision-timing.h"
65+
#include "gcc-attributes.h"
6566
#include "message.h"
6667
#include "paths.h"
6768

@@ -265,6 +266,17 @@ static atomic_bool missing_helper_driver_logged;
265266

266267
__thread struct decision_timing_context decision_timing_tls;
267268

269+
static const char *format_report_time(long when, char *buf, size_t buf_len)
270+
__attr_access ((__write_only__, 2, 3));
271+
static void format_count(unsigned long long value, char *buf, size_t buf_len)
272+
__attr_access ((__write_only__, 2, 3));
273+
static void format_scaled_time(double value, const char *unit, char *buf,
274+
size_t buf_len) __attr_access ((__write_only__, 3, 4));
275+
static void format_human_duration(unsigned long long ns, char *buf,
276+
size_t buf_len) __attr_access ((__write_only__, 2, 3));
277+
static void format_hms_duration(unsigned long long ns, char *buf,
278+
size_t buf_len) __attr_access ((__write_only__, 2, 3));
279+
268280
/*
269281
* ns_now - read monotonic time in nanoseconds.
270282
* Returns monotonic nanoseconds, or 0 if the clock cannot be read.

0 commit comments

Comments
 (0)