From 0fae5823f6e6a9592110ae50dabc6fb248eef998 Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Tue, 5 Apr 2022 12:02:31 +0900 Subject: Take advantage of gpgrt_get_syscall_clamp function. * src/assuan-defs.h (_assuan_pre_syscall, _assuan_post_syscall): New. * src/assuan.c (_assuan_pre_syscall, _assuan_post_syscall): New. (pre_syscall_func, post_syscall_func): New. (_assuan_syscall_func_initialized): New. (assuan_new_ext): Call gpgrt_get_syscall_clamp to get clamp functions. * src/system-posix.c (_assuan_system_hooks): Use version 0. * src/system-w32.c (_assuan_system_hooks): Likewise. * src/system.c (_assuan_usleep): Call clamp functions if no hooks. (_assuan_close, _assuan_close_inheritable, _assuan_read): Likewise. (_assuan_write, _assuan_recvmsg, _assuan_sendmsg): Likewise. (_assuan_waitpid, _assuan_connect): Likewise. (_assuan_pipe): Call __assuan_pipe directly if no hooks. (_assuan_spawn): Call __assuan_spawn directly if no hooks. (_assuan_socketpair): Call __assuan_socketpair directly if no hooks. (_assuan_socket): Call __assuan_socket directly if no hooks. -- GnuPG-bug-id: 5914 Signed-off-by: NIIBE Yutaka --- NEWS | 3 + src/assuan-defs.h | 2 + src/assuan.c | 31 ++++++++++ src/system-posix.c | 2 +- src/system-w32.c | 2 +- src/system.c | 164 ++++++++++++++++++++++++++++++++++++++++++++++------- 6 files changed, 182 insertions(+), 22 deletions(-) diff --git a/NEWS b/NEWS index 7c328c1..0ed6e87 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,9 @@ Noteworthy changes in version 2.5.6 (unreleased) [C8/A8/R_] ------------------------------------------------ + * Use of ASSUAN_SYSTEM_NPTH is deprecated. Instead, please use + the gpgrt_set_syscall_clamp function from gpgrt library. + Noteworthy changes in version 2.5.5 (2021-03-22) [C8/A8/R5] ------------------------------------------------ diff --git a/src/assuan-defs.h b/src/assuan-defs.h index 9538223..fc2fdeb 100644 --- a/src/assuan-defs.h +++ b/src/assuan-defs.h @@ -433,5 +433,7 @@ void _assuan_server_release (assuan_context_t ctx); /* Encode the C formatted string SRC and return the malloc'ed result. */ char *_assuan_encode_c_string (assuan_context_t ctx, const char *src); +void _assuan_pre_syscall (void); +void _assuan_post_syscall (void); #endif /*ASSUAN_DEFS_H*/ diff --git a/src/assuan.c b/src/assuan.c index b6dec93..772e892 100644 --- a/src/assuan.c +++ b/src/assuan.c @@ -36,6 +36,14 @@ /* Global default state. */ +/* Functions called before and after blocking syscalls. */ +static void (*pre_syscall_func) (void); +static void (*post_syscall_func) (void); + +/* Variable to see if functions above are initialized. */ +static int _assuan_syscall_func_initialized; + + /* The default error source gor generated error codes. */ static gpg_err_source_t _assuan_default_err_source = GPG_ERR_SOURCE_USER_1; @@ -106,6 +114,23 @@ assuan_set_system_hooks (assuan_system_hooks_t system_hooks) } +/* Used before blocking system calls. */ +void +_assuan_pre_syscall (void) +{ + if (pre_syscall_func) + pre_syscall_func (); +} + + +/* Used after blocking system calls. */ +void +_assuan_post_syscall (void) +{ + if (post_syscall_func) + post_syscall_func (); +} + /* Create a new Assuan context. The initial parameters are all needed in the creation of the context. */ gpg_error_t @@ -116,6 +141,12 @@ assuan_new_ext (assuan_context_t *r_ctx, gpg_err_source_t err_source, struct assuan_context_s wctx; assuan_context_t ctx; + if (!_assuan_syscall_func_initialized) + { + gpgrt_get_syscall_clamp (&pre_syscall_func, &post_syscall_func); + _assuan_syscall_func_initialized = 1; + } + /* Set up a working context so we can use standard functions. */ memset (&wctx, 0, sizeof (wctx)); wctx.err_source = err_source; diff --git a/src/system-posix.c b/src/system-posix.c index b7da9e3..0f7732a 100644 --- a/src/system-posix.c +++ b/src/system-posix.c @@ -435,7 +435,7 @@ __assuan_connect (assuan_context_t ctx, int sock, struct sockaddr *addr, /* The default system hooks for assuan contexts. */ struct assuan_system_hooks _assuan_system_hooks = { - ASSUAN_SYSTEM_HOOKS_VERSION, + 0, __assuan_usleep, __assuan_pipe, __assuan_close, diff --git a/src/system-w32.c b/src/system-w32.c index 3927b5d..08b93e2 100644 --- a/src/system-w32.c +++ b/src/system-w32.c @@ -604,7 +604,7 @@ __assuan_connect (assuan_context_t ctx, assuan_fd_t sock, /* The default system hooks for assuan contexts. */ struct assuan_system_hooks _assuan_system_hooks = { - ASSUAN_SYSTEM_HOOKS_VERSION, + 0, __assuan_usleep, __assuan_pipe, __assuan_close, diff --git a/src/system.c b/src/system.c index e4de0e4..fa13987 100644 --- a/src/system.c +++ b/src/system.c @@ -141,7 +141,14 @@ _assuan_usleep (assuan_context_t ctx, unsigned int usec) TRACE1 (ctx, ASSUAN_LOG_SYSIO, "_assuan_usleep", ctx, "usec=%u", usec); - (ctx->system.usleep) (ctx, usec); + if (ctx->system.version) + (ctx->system.usleep) (ctx, usec); + else + { + _assuan_pre_syscall (); + __assuan_usleep (ctx, usec); + _assuan_post_syscall (); + } } @@ -155,7 +162,10 @@ _assuan_pipe (assuan_context_t ctx, assuan_fd_t fd[2], int inherit_idx) "inherit_idx=%i (Assuan uses it for %s)", inherit_idx, inherit_idx ? "reading" : "writing"); - err = (ctx->system.pipe) (ctx, fd, inherit_idx); + if (ctx->system.version) + err = (ctx->system.pipe) (ctx, fd, inherit_idx); + else + err = __assuan_pipe (ctx, fd, inherit_idx); if (err) return TRACE_SYSRES (err); @@ -172,7 +182,16 @@ _assuan_close (assuan_context_t ctx, assuan_fd_t fd) TRACE1 (ctx, ASSUAN_LOG_SYSIO, "_assuan_close", ctx, "fd=0x%x", fd); - return (ctx->system.close) (ctx, fd); + if (ctx->system.version) + return (ctx->system.close) (ctx, fd); + else + { + int res; + _assuan_pre_syscall (); + res = __assuan_close (ctx, fd); + _assuan_post_syscall (); + return res; + } } @@ -187,7 +206,16 @@ _assuan_close_inheritable (assuan_context_t ctx, assuan_fd_t fd) #ifdef HAVE_W32CE_SYSTEM return 0; /* Nothing to do because it is a rendezvous id. */ #else - return (ctx->system.close) (ctx, fd); + if (ctx->system.version) + return (ctx->system.close) (ctx, fd); + else + { + int res; + _assuan_pre_syscall (); + res = __assuan_close (ctx, fd); + _assuan_post_syscall (); + return res; + } #endif } @@ -200,10 +228,26 @@ _assuan_read (assuan_context_t ctx, assuan_fd_t fd, void *buffer, size_t size) ssize_t res; TRACE_BEG3 (ctx, ASSUAN_LOG_SYSIO, "_assuan_read", ctx, "fd=0x%x, buffer=%p, size=%i", fd, buffer, size); - res = (ctx->system.read) (ctx, fd, buffer, size); + if (ctx->system.version) + res = (ctx->system.read) (ctx, fd, buffer, size); + else + { + _assuan_pre_syscall (); + res = __assuan_read (ctx, fd, buffer, size); + _assuan_post_syscall (); + } return TRACE_SYSRES (res); #else - return (ctx->system.read) (ctx, fd, buffer, size); + if (ctx->system.version) + return (ctx->system.read) (ctx, fd, buffer, size); + else + { + ssize_t res; + _assuan_pre_syscall (); + res = __assuan_read (ctx, fd, buffer, size); + _assuan_post_syscall (); + return res; + } #endif } @@ -217,10 +261,26 @@ _assuan_write (assuan_context_t ctx, assuan_fd_t fd, const void *buffer, ssize_t res; TRACE_BEG3 (ctx, ASSUAN_LOG_SYSIO, "_assuan_write", ctx, "fd=0x%x, buffer=%p, size=%i", fd, buffer, size); - res = (ctx->system.write) (ctx, fd, buffer, size); + if (ctx->system.version) + res = (ctx->system.write) (ctx, fd, buffer, size); + else + { + _assuan_pre_syscall (); + res = __assuan_write (ctx, fd, buffer, size); + _assuan_post_syscall (); + } return TRACE_SYSRES (res); #else - return (ctx->system.write) (ctx, fd, buffer, size); + if (ctx->system.version) + return (ctx->system.write) (ctx, fd, buffer, size); + else + { + ssize_t res; + _assuan_pre_syscall (); + res = __assuan_write (ctx, fd, buffer, size); + _assuan_post_syscall (); + return res; + } #endif } @@ -234,7 +294,14 @@ _assuan_recvmsg (assuan_context_t ctx, assuan_fd_t fd, assuan_msghdr_t msg, ssize_t res; TRACE_BEG3 (ctx, ASSUAN_LOG_SYSIO, "_assuan_recvmsg", ctx, "fd=0x%x, msg=%p, flags=0x%x", fd, msg, flags); - res = (ctx->system.recvmsg) (ctx, fd, msg, flags); + if (ctx->system.version) + res = (ctx->system.recvmsg) (ctx, fd, msg, flags); + else + { + _assuan_pre_syscall (); + res = __assuan_recvmsg (ctx, fd, msg, flags); + _assuan_post_syscall (); + } if (res > 0) { struct cmsghdr *cmptr; @@ -255,7 +322,16 @@ _assuan_recvmsg (assuan_context_t ctx, assuan_fd_t fd, assuan_msghdr_t msg, } return TRACE_SYSRES (res); #else - return (ctx->system.recvmsg) (ctx, fd, msg, flags); + if (ctx->system.version) + return (ctx->system.recvmsg) (ctx, fd, msg, flags); + else + { + ssize_t res; + _assuan_pre_syscall (); + res = __assuan_recvmsg (ctx, fd, msg, flags); + _assuan_post_syscall (); + return res; + } #endif } @@ -286,10 +362,26 @@ _assuan_sendmsg (assuan_context_t ctx, assuan_fd_t fd, assuan_msghdr_t msg, cmptr->cmsg_level, cmptr->cmsg_type, *(int *)data); } } - res = (ctx->system.sendmsg) (ctx, fd, msg, flags); + if (ctx->system.version) + res = (ctx->system.sendmsg) (ctx, fd, msg, flags); + else + { + _assuan_pre_syscall (); + res = __assuan_sendmsg (ctx, fd, msg, flags); + _assuan_post_syscall (); + } return TRACE_SYSRES (res); #else - return (ctx->system.sendmsg) (ctx, fd, msg, flags); + if (ctx->system.version) + return (ctx->system.sendmsg) (ctx, fd, msg, flags); + else + { + ssize_t res; + _assuan_pre_syscall (); + res = __assuan_sendmsg (ctx, fd, msg, flags); + _assuan_post_syscall (); + return res; + } #endif } @@ -334,8 +426,12 @@ _assuan_spawn (assuan_context_t ctx, pid_t *r_pid, const char *name, } } - res = (ctx->system.spawn) (ctx, r_pid, name, argv, fd_in, fd_out, - fd_child_list, atfork, atforkvalue, flags); + if (ctx->system.version) + res = (ctx->system.spawn) (ctx, r_pid, name, argv, fd_in, fd_out, + fd_child_list, atfork, atforkvalue, flags); + else + res = __assuan_spawn (ctx, r_pid, name, argv, fd_in, fd_out, + fd_child_list, atfork, atforkvalue, flags); if (name) { @@ -362,10 +458,26 @@ _assuan_waitpid (assuan_context_t ctx, pid_t pid, int action, TRACE_BEG4 (ctx, ASSUAN_LOG_SYSIO, "_assuan_waitpid", ctx, "pid=%i, action=%i, status=%p, options=%i", pid, action, status, options); - res = (ctx->system.waitpid) (ctx, pid, action, status, options); + if (ctx->system.version) + res = (ctx->system.waitpid) (ctx, pid, action, status, options); + else + { + _assuan_pre_syscall (); + res = __assuan_waitpid (ctx, pid, action, status, options); + _assuan_post_syscall (); + } return TRACE_SYSRES (res); #else - return (ctx->system.waitpid) (ctx, pid, action, status, options); + if (ctx->system.version) + return (ctx->system.waitpid) (ctx, pid, action, status, options); + else + { + ssize_t res; + _assuan_pre_syscall (); + res = __assuan_waitpid (ctx, pid, action, status, options); + _assuan_post_syscall (); + return res; + } #endif } @@ -380,7 +492,10 @@ _assuan_socketpair (assuan_context_t ctx, int namespace, int style, "namespace=%i,style=%i,protocol=%i,filedes=%p", namespace, style, protocol, filedes); - res = (ctx->system.socketpair) (ctx, namespace, style, protocol, filedes); + if (ctx->system.version) + res = (ctx->system.socketpair) (ctx, namespace, style, protocol, filedes); + else + res = __assuan_socketpair (ctx, namespace, style, protocol, filedes); if (res == 0) TRACE_LOG2 ("filedes = { 0x%x, 0x%x }", filedes[0], filedes[1]); @@ -397,7 +512,10 @@ _assuan_socket (assuan_context_t ctx, int namespace, int style, int protocol) "namespace=%i,style=%i,protocol=%i", namespace, style, protocol); - res = (ctx->system.socket) (ctx, namespace, style, protocol); + if (ctx->system.version) + res = (ctx->system.socket) (ctx, namespace, style, protocol); + else + res = __assuan_socket (ctx, namespace, style, protocol); return TRACE_SYSRES (res); } @@ -410,7 +528,13 @@ _assuan_connect (assuan_context_t ctx, assuan_fd_t sock, TRACE_BEG3 (ctx, ASSUAN_LOG_SYSIO, "_assuan_connect", ctx, "socket=%i,addr=%p,length=%i", sock, addr, length); - res = (ctx->system.connect) (ctx, sock, addr, length); + if (ctx->system.version) + res = (ctx->system.connect) (ctx, sock, addr, length); + else + { + _assuan_pre_syscall (); + res = __assuan_connect (ctx, sock, addr, length); + _assuan_post_syscall (); + } return TRACE_SYSRES (res); } - -- cgit v1.2.1