/* Test that svcudp_recv bounds XDR decoding to the received datagram length, preventing stale buffer data from being decoded as part of a subsequent shorter request. Copyright (C) 2026 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. The GNU C Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with the GNU C Library; if not, see . */ #include #include #include #include #include #include #include #include #include #include #include struct echo_args { uint32_t values[4]; }; static bool_t xdr_echo_args (XDR *xdrs, ...) { va_list ap; va_start (ap, xdrs); struct echo_args *p = va_arg (ap, void *); va_end (ap); return xdr_uint32_t (xdrs, &p->values[0]) && xdr_uint32_t (xdrs, &p->values[1]) && xdr_uint32_t (xdrs, &p->values[2]) && xdr_uint32_t (xdrs, &p->values[3]); } enum { PROGNUM = 25641, VERSNUM = 1, PROC_ECHO = 1, PROC_EXIT = 2, EXIT_MARKER = 55, }; static void server_dispatch (struct svc_req *request, SVCXPRT *transport) { switch (request->rq_proc) { case PROC_ECHO: { struct echo_args args; memset (&args, 0, sizeof (args)); if (!svc_getargs (transport, xdr_echo_args, (void *) &args)) { svcerr_decode (transport); return; } assert (svc_sendreply (transport, xdr_echo_args, (void *) &args)); } break; case PROC_EXIT: assert (svc_sendreply (transport, (xdrproc_t) xdr_void, NULL)); _exit (EXIT_MARKER); break; default: svcerr_noproc (transport); break; } } static pid_t server_pid; static void kill_server (void) { kill (server_pid, SIGTERM); } int main (void) { SVCXPRT *transport = svcudp_create (RPC_ANYSOCK); assert (transport != NULL); assert (svc_register (transport, PROGNUM, VERSNUM, server_dispatch, 0)); struct sockaddr_in server_addr = { .sin_family = AF_INET, .sin_addr.s_addr = htonl (INADDR_LOOPBACK), .sin_port = htons (transport->xp_port), }; server_pid = fork (); assert (server_pid >= 0); if (server_pid == 0) { svc_run (); assert (false && "svc_run returned"); } atexit (kill_server); /* Send a well-formed echo request to verify that the service works and to place known argument data into the server's receive buffer. After the reply is encoded, the argument bytes remain at the tail of the buffer because the reply is shorter than the request. */ { int sock = RPC_ANYSOCK; CLIENT *clnt = clntudp_create (&server_addr, PROGNUM, VERSNUM, (struct timeval) { 5, 0 }, &sock); assert (clnt != NULL); struct echo_args query = { .values = { 0xAABBCCDD, 0x11223344, 0x55667788, 0x99AABBCC }, }; struct echo_args response; memset (&response, 0, sizeof (response)); assert (clnt_call (clnt, PROC_ECHO, xdr_echo_args, (void *) &query, xdr_echo_args, (void *) &response, ((struct timeval) { 5, 0 })) == RPC_SUCCESS); assert (response.values[0] == 0xAABBCCDD); assert (response.values[1] == 0x11223344); assert (response.values[2] == 0x55667788); assert (response.values[3] == 0x99AABBCC); clnt_destroy (clnt); } /* Send a truncated RPC call using a raw socket. The header is well-formed and uses AUTH_NONE, but the 40-byte datagram carries no procedure argument payload. The echo service expects 16 bytes of arguments via svc_getargs. If the XDR decode stream is correctly bounded to the datagram length, svc_getargs fails (x_handy is exhausted after the 40-byte header) and the server responds with GARBAGE_ARGS. If the decode bound is the full buffer size instead, svc_getargs reads 16 bytes of stale data left by the previous request and the server echoes them back as a successful response. */ { int sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP); assert (sock >= 0); /* RPC CALL header in network byte order, AUTH_NONE, no arguments. PROGNUM=25641=0x6429, VERSNUM=1, PROC_ECHO=1. */ static const char header[] = "\xDE\xAD\x00\x01" /* xid */ "\x00\x00\x00\x00" /* CALL */ "\x00\x00\x00\x02" /* RPC_MSG_VERSION */ "\x00\x00\x64\x29" /* PROGNUM */ "\x00\x00\x00\x01" /* VERSNUM */ "\x00\x00\x00\x01" /* PROC_ECHO */ "\x00\x00\x00\x00" /* cred flavor */ "\x00\x00\x00\x00" /* cred length */ "\x00\x00\x00\x00" /* verf flavor */ "\x00\x00\x00\x00" /* verf length */; ssize_t sent = sendto (sock, header, sizeof (header) - 1, 0, (struct sockaddr *) &server_addr, sizeof (server_addr)); assert (sent == (ssize_t) sizeof (header) - 1); struct pollfd pfd = { .fd = sock, .events = POLLIN }; int ret = poll (&pfd, 1, 3000); assert (ret > 0); char reply[256]; ssize_t rlen = recvfrom (sock, reply, sizeof (reply), 0, NULL, NULL); assert (rlen >= 24); /* Expected reply in network byte order: xid=0xDEAD0001, REPLY=1, MSG_ACCEPTED=0, verf_flavor=AUTH_NONE=0, verf_length=0, accept_stat=GARBAGE_ARGS=4. */ static char expected[] = "\xDE\xAD\x00\x01" /* xid */ "\x00\x00\x00\x01" /* REPLY */ "\x00\x00\x00\x00" /* MSG_ACCEPTED */ "\x00\x00\x00\x00" /* verf flavor */ "\x00\x00\x00\x00" /* verf length */ "\x00\x00\x00\x04" /* GARBAGE_ARGS */ ; if (rlen != sizeof (expected) - 1 || memcmp (reply, expected, rlen) != 0) { printf ("error: did not receive expected response, got %zd bytes:\n ", rlen); for (ssize_t i = 0; i < rlen; ) { printf (" %02X", reply[i] & 0xff); ++i; if (((i % 16) == 0) && i < rlen) printf ("\n "); } putchar ('\n'); return 1; } close (sock); } /* Shut down the server. This also verifies that the server is still functional after rejecting the truncated request. */ { int sock = RPC_ANYSOCK; CLIENT *clnt = clntudp_create (&server_addr, PROGNUM, VERSNUM, (struct timeval) { 5, 0 }, &sock); assert (clnt != NULL); assert (clnt_call (clnt, PROC_EXIT, (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_void, NULL, ((struct timeval) { 5, 0 })) == RPC_SUCCESS); clnt_destroy (clnt); } int status; assert (waitpid (server_pid, &status, 0) >= 0); assert (WIFEXITED (status) && WEXITSTATUS (status) == EXIT_MARKER); SVC_DESTROY (transport); return 0; }