Skip to content

Commit cf7254e

Browse files
committed
appsec: fix recv/writev calls in the face of interrupting signals
1 parent dc9911b commit cf7254e

File tree

3 files changed

+121
-183
lines changed

3 files changed

+121
-183
lines changed

appsec/src/extension/commands_helpers.c

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ static inline ATTR_WARN_UNUSED mpack_error_t _omsg_finish(
2929
static inline void _omsg_destroy(dd_omsg *nonnull omsg);
3030
static inline dd_result _omsg_send(
3131
dd_conn *nonnull conn, dd_omsg *nonnull omsg);
32-
static inline dd_result _omsg_send_cred(
33-
dd_conn *nonnull conn, dd_omsg *nonnull omsg);
3432
static void _dump_in_msg(
3533
dd_log_level_t lvl, const char *nonnull data, size_t data_len);
3634
static void _dump_out_msg(dd_log_level_t lvl, zend_llist *iovecs);
@@ -42,16 +40,14 @@ typedef struct _dd_imsg {
4240
mpack_node_t root;
4341
} dd_imsg;
4442

45-
// iif these two return success, _imsg_destroy must be called
46-
static inline dd_result _imsg_recv(
47-
dd_imsg *nonnull imsg, dd_conn *nonnull conn);
48-
static inline ATTR_WARN_UNUSED dd_result _imsg_recv_cred(
43+
// iif this returns success, _imsg_destroy must be called
44+
static dd_result ATTR_WARN_UNUSED _imsg_recv(
4945
dd_imsg *nonnull imsg, dd_conn *nonnull conn);
5046

5147
static inline ATTR_WARN_UNUSED mpack_error_t _imsg_destroy(
5248
dd_imsg *nonnull imsg);
5349

54-
static dd_result _dd_command_exec(dd_conn *nonnull conn, bool check_cred,
50+
static dd_result _dd_command_exec(dd_conn *nonnull conn,
5551
const dd_command_spec *nonnull spec, void *unspecnull ctx)
5652
{
5753
#define NAME_L (int)spec->name_len, spec->name
@@ -78,11 +74,7 @@ static dd_result _dd_command_exec(dd_conn *nonnull conn, bool check_cred,
7874
return dd_error;
7975
}
8076

81-
if (check_cred) {
82-
res = _omsg_send_cred(conn, &omsg);
83-
} else {
84-
res = _omsg_send(conn, &omsg);
85-
}
77+
res = _omsg_send(conn, &omsg);
8678
_dump_out_msg(dd_log_trace, &omsg.iovecs);
8779
_omsg_destroy(&omsg);
8880
if (res) {
@@ -96,11 +88,7 @@ static dd_result _dd_command_exec(dd_conn *nonnull conn, bool check_cred,
9688
dd_result res;
9789
{
9890
dd_imsg imsg = {0};
99-
if (check_cred) {
100-
res = _imsg_recv_cred(&imsg, conn);
101-
} else {
102-
res = _imsg_recv(&imsg, conn);
103-
}
91+
res = _imsg_recv(&imsg, conn);
10492
if (res) {
10593
if (res != dd_helper_error) {
10694
mlog(dd_log_warning,
@@ -194,20 +182,25 @@ static dd_result _dd_command_exec(dd_conn *nonnull conn, bool check_cred,
194182
dd_result ATTR_WARN_UNUSED dd_command_exec(dd_conn *nonnull conn,
195183
const dd_command_spec *nonnull spec, void *unspecnull ctx)
196184
{
197-
return _dd_command_exec(conn, false, spec, ctx);
185+
return _dd_command_exec(conn, spec, ctx);
198186
}
199187

200188
dd_result ATTR_WARN_UNUSED dd_command_exec_req_info(dd_conn *nonnull conn,
201189
const dd_command_spec *nonnull spec, struct req_info *nonnull ctx)
202190
{
203191
ctx->command_name = spec->name;
204-
return _dd_command_exec(conn, false, spec, ctx);
192+
return _dd_command_exec(conn, spec, ctx);
205193
}
206194

207195
dd_result ATTR_WARN_UNUSED dd_command_exec_cred(dd_conn *nonnull conn,
208196
const dd_command_spec *nonnull spec, void *unspecnull ctx)
209197
{
210-
return _dd_command_exec(conn, true, spec, ctx);
198+
dd_result res = dd_conn_check_credentials(conn);
199+
if (res) {
200+
return res;
201+
}
202+
203+
return _dd_command_exec(conn, spec, ctx);
211204
}
212205

213206
// outgoing
@@ -247,24 +240,13 @@ static inline dd_result _omsg_send(dd_conn *nonnull conn, dd_omsg *nonnull omsg)
247240
return dd_conn_sendv(conn, &omsg->iovecs);
248241
}
249242

250-
static inline dd_result _omsg_send_cred(
251-
dd_conn *nonnull conn, dd_omsg *nonnull omsg)
252-
{
253-
return dd_conn_sendv_cred(conn, &omsg->iovecs);
254-
}
255-
256243
// incoming
257-
static inline dd_result _dd_imsg_recv(
258-
dd_imsg *nonnull imsg, dd_conn *nonnull conn, bool check_cred)
244+
static ATTR_WARN_UNUSED dd_result _imsg_recv(
245+
dd_imsg *nonnull imsg, dd_conn *nonnull conn)
259246
{
260247
mlog(dd_log_debug, "Will receive response from helper");
261248

262-
dd_result res;
263-
if (check_cred) {
264-
res = dd_conn_recv_cred(conn, &imsg->_data, &imsg->_size);
265-
} else {
266-
res = dd_conn_recv(conn, &imsg->_data, &imsg->_size);
267-
}
249+
dd_result res = dd_conn_recv(conn, &imsg->_data, &imsg->_size);
268250
if (res) {
269251
return res;
270252
}
@@ -290,17 +272,6 @@ static inline dd_result _dd_imsg_recv(
290272
return dd_success;
291273
}
292274

293-
ATTR_WARN_UNUSED dd_result _imsg_recv(
294-
dd_imsg *nonnull imsg, dd_conn *nonnull conn)
295-
{
296-
return _dd_imsg_recv(imsg, conn, false);
297-
}
298-
ATTR_WARN_UNUSED dd_result _imsg_recv_cred(
299-
dd_imsg *nonnull imsg, dd_conn *nonnull conn)
300-
{
301-
return _dd_imsg_recv(imsg, conn, true);
302-
}
303-
304275
static inline ATTR_WARN_UNUSED mpack_error_t _imsg_destroy(
305276
dd_imsg *nonnull imsg)
306277
{

0 commit comments

Comments
 (0)