Giter VIP home page Giter VIP logo

Comments (4)

alandekok avatar alandekok commented on July 19, 2024

Thanks. You can fork the repo, add the patch, and push it back. Or, if it's a github issue, just attach the patch again in a few minues.

from freeradius-client.

pem avatar pem commented on July 19, 2024

Since it's short I'll just add it here:

--- lib/sendserver.c.orig	2018-01-23 10:57:07.861253190 +0100
+++ lib/sendserver.c	2018-01-23 11:19:12.328861458 +0100
@@ -214,6 +214,15 @@
  *
  */
 
+/* Used to avoid casts between char buffers and the header struct since
+** this might cause alignment problems on some architectures.
+ */
+typedef union auth_buf_u
+{
+    AUTH_HDR hdr;
+    char buf[BUFFER_LEN];
+} auth_buf_t;
+
 int rc_send_server (rc_handle *rh, SEND_DATA *data, char *msg)
 {
 	int             sockfd;
@@ -230,8 +239,8 @@
 	size_t			secretlen;
 	char            secret[MAX_SECRET_LENGTH + 1];
 	unsigned char   vector[AUTH_VECTOR_LEN];
-	char            recv_buffer[BUFFER_LEN];
-	char            send_buffer[BUFFER_LEN];
+        auth_buf_t      recv_auth_buf;
+        auth_buf_t      send_auth_buf;
 	int		retries;
 	VALUE_PAIR 	*vp;
 	struct pollfd	pfd;
@@ -313,7 +322,7 @@
 	}
 
 	/* Build a request */
-	auth = (AUTH_HDR *) send_buffer;
+        auth = &send_auth_buf.hdr;
 	auth->code = data->code;
 	auth->id = data->seq_nbr;
 
@@ -345,7 +354,7 @@
 
 	for (;;)
 	{
-		sendto (sockfd, (char *) auth, (unsigned int) total_length, (int) 0,
+		sendto (sockfd, send_auth_buf.buf, (unsigned int) total_length, (int) 0,
 			SA(&sinremote), sizeof (struct sockaddr_in));
 
 		pfd.fd = sockfd;
@@ -383,8 +392,8 @@
 		}
 	}
 	salen = sizeof(sinremote);
-	length = recvfrom (sockfd, (char *) recv_buffer,
-			   (int) sizeof (recv_buffer),
+	length = recvfrom (sockfd, recv_auth_buf.buf,
+			   (int) sizeof (recv_auth_buf),
 			   (int) 0, SA(&sinremote), &salen);
 
 	if (length <= 0)
@@ -396,7 +405,7 @@
 		return ERROR_RC;
 	}
 
-	recv_auth = (AUTH_HDR *)recv_buffer;
+        recv_auth = &recv_auth_buf.hdr;
 
 	if (length < AUTH_HDR_LEN || length < ntohs(recv_auth->length)) {
 		rc_log(LOG_ERR, "rc_send_server: recvfrom: %s:%d: reply is too short",

from freeradius-client.

pem avatar pem commented on July 19, 2024

Someone pointed out that this is misusing unions. That's true, in theory, but in practice we know it will work. (The issue is that the standard doesn't guarantee that setting one union field and then reading the other will do what you expect.)
If you want to be very strict, I don't think you can guarantee that the AUTH_HDR struct layout will be compact either. (Although we know that it will, in sane implementations.)

Another solution is to simply change
char secret[MAX_SECRET_LENGTH + 1];
into
char secret[MAX_SECRET_LENGTH + 4];
but I thought that had a kludgy feel to it, and it still relies on casts.

Alternatively, don't use the AUTH_HDR overlay struct, and pack/unpack the header byte by byte instead.

from freeradius-client.

alandekok avatar alandekok commented on July 19, 2024

The simpler way to fix it is to just declare the buffer as uint64_t instead of char. The functions taking char * will happily take a pointer cast from uint64_t *. But the reverse isn't true.

from freeradius-client.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.