Skip to content

Commit 8dd210a

Browse files
author
Julia Kartseva
committed
core: add SocketBind{Allow|Deny} fragment parser
1 parent a8e5eb1 commit 8dd210a

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

src/core/load-fragment-gperf.gperf.m4

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,9 @@ $1.ManagedOOMMemoryPressure, config_parse_managed_oom_mode,
235235
$1.ManagedOOMMemoryPressureLimit, config_parse_managed_oom_mem_pressure_limit, 0, offsetof($1, cgroup_context.moom_mem_pressure_limit)
236236
$1.ManagedOOMPreference, config_parse_managed_oom_preference, 0, offsetof($1, cgroup_context.moom_preference)
237237
$1.NetClass, config_parse_warn_compat, DISABLED_LEGACY, 0
238-
$1.BPFProgram, config_parse_bpf_foreign_program, 0, offsetof($1, cgroup_context)'
238+
$1.BPFProgram, config_parse_bpf_foreign_program, 0, offsetof($1, cgroup_context)
239+
$1.SocketBindAllow, config_parse_cgroup_socket_bind, 0, offsetof($1, cgroup_context.socket_bind_allow)
240+
$1.SocketBindDeny, config_parse_cgroup_socket_bind, 0, offsetof($1, cgroup_context.socket_bind_deny)'
239241
)m4_dnl
240242
Unit.Description, config_parse_unit_string_printf, 0, offsetof(Unit, description)
241243
Unit.Documentation, config_parse_documentation, 0, offsetof(Unit, documentation)

src/core/load-fragment.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#endif
5656
#include "securebits-util.h"
5757
#include "signal-util.h"
58+
#include "socket-bind.h"
5859
#include "socket-netlink.h"
5960
#include "stat-util.h"
6061
#include "string-util.h"
@@ -5657,6 +5658,73 @@ int config_parse_bpf_foreign_program(
56575658
return 0;
56585659
}
56595660

5661+
int config_parse_cgroup_socket_bind(
5662+
const char *unit,
5663+
const char *filename,
5664+
unsigned line,
5665+
const char *section,
5666+
unsigned section_line,
5667+
const char *lvalue,
5668+
int ltype,
5669+
const char *rvalue,
5670+
void *data,
5671+
void *userdata) {
5672+
_cleanup_free_ CGroupSocketBindItem *item = NULL;
5673+
const char *address_family = NULL, *user_port;
5674+
uint16_t nr_ports = 0, port_min = 0;
5675+
CGroupSocketBindItem **head = data;
5676+
_cleanup_free_ char *word = NULL;
5677+
int af = AF_UNSPEC, r;
5678+
5679+
if (isempty(rvalue)) {
5680+
cgroup_context_remove_socket_bind(head);
5681+
return 0;
5682+
}
5683+
5684+
r = extract_first_word(&rvalue, &word, ":", 0);
5685+
if (r == -ENOMEM)
5686+
return log_oom();
5687+
5688+
if (rvalue)
5689+
address_family = word;
5690+
5691+
if (address_family) {
5692+
if (streq(address_family, "IPv4"))
5693+
af = AF_INET;
5694+
else if (streq(address_family, "IPv6"))
5695+
af = AF_INET6;
5696+
else
5697+
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
5698+
"Only IPv4 or IPv6 protocols are supported, ignoring");
5699+
}
5700+
5701+
user_port = rvalue ?: word;
5702+
if (!streq(user_port, "any")) {
5703+
uint16_t port_max;
5704+
5705+
r = parse_ip_port_range(user_port, &port_min, &port_max);
5706+
if (r == -ENOMEM)
5707+
return log_oom();
5708+
if (r < 0)
5709+
return log_warning_errno(r, "Invalid port or port range, ignoring: %m");
5710+
5711+
nr_ports = 1 + port_max - port_min;
5712+
}
5713+
5714+
item = new(CGroupSocketBindItem, 1);
5715+
if (!item)
5716+
return log_oom();
5717+
*item = (CGroupSocketBindItem) {
5718+
.address_family = af,
5719+
.nr_ports = nr_ports,
5720+
.port_min = port_min,
5721+
};
5722+
5723+
LIST_PREPEND(socket_bind_items, *head, TAKE_PTR(item));
5724+
5725+
return 0;
5726+
}
5727+
56605728
static int merge_by_names(Unit **u, Set *names, const char *id) {
56615729
char *k;
56625730
int r;

src/core/load-fragment.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ CONFIG_PARSER_PROTOTYPE(config_parse_mount_images);
141141
CONFIG_PARSER_PROTOTYPE(config_parse_socket_timestamping);
142142
CONFIG_PARSER_PROTOTYPE(config_parse_extension_images);
143143
CONFIG_PARSER_PROTOTYPE(config_parse_bpf_foreign_program);
144+
CONFIG_PARSER_PROTOTYPE(config_parse_cgroup_socket_bind);
144145

145146
/* gperf prototypes */
146147
const struct ConfigPerfItem* load_fragment_gperf_lookup(const char *key, GPERF_LEN_TYPE length);

0 commit comments

Comments
 (0)