Skip to contentSkip to conversation

alfred: gpsd: random fixes

PATCH New Sven Eckelmann 1 participant 8 messages Jul 28

lore
Share & tools
lore
#0 lore raw

cover alfred: gpsd: random fixes

Similar to the batctl fixes, I have now compiled a list of potential fixes for alfred. Instead of posting everything at once, I will be posted in smaller chunks. They are just various (mostly minor) problems which should be tackled at some point.

This time, we will only handle the gpsd changes.

---
Sven Eckelmann (7):
alfred: gpsd: show unix-path option in the usage output
alfred: gpsd: reject too long unix socket paths
alfred: gpsd: fix counter underflow on leading carriage return
alfred: gpsd: handle short reads on the unix socket
alfred: gpsd: report failures while reading the answer
alfred: gpsd: don't drop the rest of the answer for a bogus dataset
alfred: gpsd: handle empty or not 0-terminated TPV

 gpsd/alfred-gpsd.c | 104 ++++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 83 insertions(+), 21 deletions(-)

---
base-commit: 462a0cf40615f62e0912c36d72a75accbb26f5ee
change-id: 20260728-bugfixes-gpsd-a364ec31e7f6

Best regards,

Signature · 1 line
Sven Eckelmann <[email protected]>
#1 lore raw

PATCH 1/7 alfred: gpsd: show unix-path option in the usage output

gpsd_init() accepts -u/--unix-path to select the unix socket of the alfred daemon. It should be documented for gpsd as it is for all other clients.

Fixes 6c6a6f735054 ("alfred: Make unix socket path configurable")
---
 gpsd/alfred-gpsd.c | 1 +
 1 file changed, 1 insertion(+)
gpsd/alfred-gpsd.c +1 -0
index 04f7a46..4a00526 100644
@@ -354,6 +354,7 @@ static void gpsd_usage(void)
 	printf("  -s, --server                start up in server mode, which regularly updates gpsd data from batman-adv\n");
 	printf("  -l <lat>,<lon>,<alt>        Static location\n");
 	printf("  -g server[:port[:device]]   GPSD source\n");
+	printf("  -u, --unix-path <path>      path to unix socket used for alfred server communication (default: \""ALFRED_SOCK_PATH_DEFAULT"\")\n");
 	printf("  -v, --version               print the version\n");
 	printf("  -h, --help                  this help\n");
 	printf("\n");
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/7 alfred: gpsd: reject too long unix socket paths

In reply to Sven Eckelmann

The unix socket path needs to be stored in sockaddr_un::sun_path. But strncpy() is silently truncating the string when the target buffer is too small.

Reject such paths with a clear error message.

Fixes 6c6a6f735054 ("alfred: Make unix socket path configurable")
---
 gpsd/alfred-gpsd.c | 6 ++++++
 1 file changed, 6 insertions(+)
gpsd/alfred-gpsd.c +6 -0
index 4a00526..eecf914 100644
@@ -16,6 +16,12 @@ static int alfred_open_sock(struct globals *globals)
 {
 	struct sockaddr_un addr;
 
+	if (strlen(globals->unix_path) >= sizeof(addr.sun_path)) {
+		fprintf(stderr, "unix socket path too long\n");
+		globals->unix_sock = -1;
+		return -1;
+	}
+
 	globals->unix_sock = socket(AF_LOCAL, SOCK_STREAM, 0);
 	if (globals->unix_sock < 0) {
 		perror("can't create unix socket");
Signature · 1 line
2.47.3
#3 lore raw

PATCH 3/7 alfred: gpsd: fix counter underflow on leading carriage return

In reply to Sven Eckelmann

gpsd_read_gpsd() strips carriage returns from received lines by decrementing the write position so the next character overwrites the '\r'. A line starting with a carriage return causes an underflow to SIZE_MAX. The read is aborted because it will be much larger than the size of the buffer.

Simplify the logic by going through the buffer and only incrementing the write position on valid characters.

Fixes 2b901d69d8fb ("alfred: Add support for passing location information over alfred.")
---
 gpsd/alfred-gpsd.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
gpsd/alfred-gpsd.c +12 -6
index eecf914..6456e74 100644
@@ -314,29 +314,33 @@ static void gpsd_read_gpsd(struct globals *globals)
 				sizeof(struct alfred_data) -
 				sizeof(*globals->gpsd_data);
 	bool eol = false;
+	size_t received;
 	char buf[4096];
 	ssize_t ret;
 	size_t cnt;
+	char c;
 
 	cnt = 0;
-	do {
-		ret = read(globals->gpsdata.gps_fd, &buf[cnt], 1);
+	for (received = 0; received < sizeof(buf) - 1 && !eol; received++) {
+		ret = read(globals->gpsdata.gps_fd, &c, 1);
 		if (ret != 1) {
 			gps_close(&globals->gpsdata);
 			globals->gpsdata.gps_fd = -1;
 			return;
 		}
 
-		switch (buf[cnt]) {
+		switch (c) {
 		case '\r':
-			cnt--;
+			/* strip carriage returns */
 			break;
 		case '\n':
 			eol = true;
-			buf[cnt] = '\0';
+			break;
+		default:
+			buf[cnt++] = c;
 			break;
 		}
-	} while (cnt++ < sizeof(buf) - 1 && !eol);
+	}
 
 	if (!eol) {
 		gps_close(&globals->gpsdata);
@@ -344,6 +348,8 @@ static void gpsd_read_gpsd(struct globals *globals)
 		return;
 	}
 
+	buf[cnt] = '\0';
+
 #define STARTSWITH(str, prefix)	strncmp(str, prefix, sizeof(prefix)-1)==0
 	if (STARTSWITH(buf, "{\"class\":\"TPV\"")) {
 		strncpy(globals->gpsd_data->tpv, buf, tpv_size);
Signature · 1 line
2.47.3
#4 lore raw

PATCH 4/7 alfred: gpsd: handle short reads on the unix socket

In reply to Sven Eckelmann

The answer of the alfred daemon is received over a SOCK_STREAM unix socket which doesn't preserve message boundaries. read() can therefore return less than the requested number of bytes. Such a short read was treated like an end of stream, the current packet was dropped and the answer loop terminated.

Retry the read until the requested amount of data, end of stream or a real error was received (ignoring temporary interruptions EINTR).

Fixes 2b901d69d8fb ("alfred: Add support for passing location information over alfred.")
---
 gpsd/alfred-gpsd.c | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)
gpsd/alfred-gpsd.c +26 -2
index 6456e74..43a3a83 100644
@@ -139,6 +139,30 @@ static int gpsd_request_data(struct globals *globals)
 	return globals->unix_sock;
 }
 
+static ssize_t read_full(int fd, void *buf, size_t count)
+{
+	size_t read_len = 0;
+	uint8_t *pos = buf;
+	ssize_t ret;
+
+	while (read_len < count) {
+		ret = read(fd, pos + read_len, count - read_len);
+		if (ret < 0) {
+			if (errno == EINTR)
+				continue;
+
+			return ret;
+		}
+
+		if (ret == 0)
+			break;
+
+		read_len += ret;
+	}
+
+	return read_len;
+}
+
 static struct gpsd_v1 *gpsd_receive_answer_packet(int sock, uint16_t *len,
 						  uint8_t *source)
 {
@@ -149,7 +173,7 @@ static struct gpsd_v1 *gpsd_receive_answer_packet(int sock, uint16_t *len,
 	int ret;
 	int l;
 
-	ret = read(sock, buf, sizeof(*tlv));
+	ret = read_full(sock, buf, sizeof(*tlv));
 	if (ret < 0)
 		return NULL;
 
@@ -171,7 +195,7 @@ static struct gpsd_v1 *gpsd_receive_answer_packet(int sock, uint16_t *len,
 		return NULL;
 
 	/* read the rest of the packet */
-	ret = read(sock, buf + sizeof(*tlv), l);
+	ret = read_full(sock, buf + sizeof(*tlv), l);
 	if (ret < l)
 		return NULL;
 
Signature · 1 line
2.47.3
#5 lore raw

PATCH 5/7 alfred: gpsd: report failures while reading the answer

In reply to Sven Eckelmann

gpsd_receive_answer_packet() returns NULL for a:

* closed connection
* read error
* malformed packet
* unexpected answer

All of them were indistinguishable from the latter regular end of the answer. Even on errors alfred-gpsd printed an empty (well formed document). An error could not directly be detected by the return code.

Report the error state of the packet parsing separately and use it for the exit code.

Fixes 2b901d69d8fb ("alfred: Add support for passing location information over alfred.")
---
 gpsd/alfred-gpsd.c | 40 ++++++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)
gpsd/alfred-gpsd.c +30 -10
index 43a3a83..57ac9cf 100644
@@ -164,7 +164,7 @@ static ssize_t read_full(int fd, void *buf, size_t count)
 }
 
 static struct gpsd_v1 *gpsd_receive_answer_packet(int sock, uint16_t *len,
-						  uint8_t *source)
+						  uint8_t *source, int *error)
 {
 	struct alfred_push_data_v0 *push;
 	static uint8_t buf[65536];
@@ -173,15 +173,29 @@ static struct gpsd_v1 *gpsd_receive_answer_packet(int sock, uint16_t *len,
 	int ret;
 	int l;
 
+	*error = -1;
+
 	ret = read_full(sock, buf, sizeof(*tlv));
-	if (ret < 0)
+	if (ret < 0) {
+		perror("read from unix socket failed");
 		return NULL;
+	}
+
+	/* end of the answer */
+	if (ret == 0) {
+		*error = 0;
+		return NULL;
+	}
 
 	if (ret < (int)sizeof(*tlv))
 		return NULL;
 
 	tlv = (struct alfred_tlv *)buf;
-	/* TODO: might return an ALFRED_STATUS_ERROR too, handle it */
+	if (tlv->type == ALFRED_STATUS_ERROR) {
+		fprintf(stderr, "Request of gpsd data failed\n");
+		return NULL;
+	}
+
 	if (tlv->type != ALFRED_PUSH_DATA)
 		return NULL;
 
@@ -209,6 +223,8 @@ static struct gpsd_v1 *gpsd_receive_answer_packet(int sock, uint16_t *len,
 	if (data->header.version != GPSD_PACKETVERSION)
 		return NULL;
 
+	*error = 0;
+
 	memcpy(source, data->source, ETH_ALEN);
 	return (struct gpsd_v1 *) data->data;
 }
@@ -219,12 +235,12 @@ static int gpsd_read_answer(struct globals *globals)
 	uint8_t source[ETH_ALEN];
 	bool first_line = true;
 	uint16_t len;
+	int ret = 0;
 
 	printf("[\n");
 
 	while ((gpsd_data = gpsd_receive_answer_packet(globals->unix_sock,
-						       &len,
-						       source)) != NULL) {
+						       &len, source, &ret)) != NULL) {
 		if (len < sizeof(*gpsd_data))
 			break;
 
@@ -246,7 +262,7 @@ static int gpsd_read_answer(struct globals *globals)
 	printf("\n]\n");
 
 
-	return 0;
+	return ret;
 }
 
 /* Standard parsing of a GPS data source spec. Taken from gpsdclient.c
@@ -300,14 +316,16 @@ static void alfred_gpsd_source_spec(const char *arg,
 
 static int gpsd_get_data(struct globals *globals)
 {
+	int ret;
+
 	globals->unix_sock = gpsd_request_data(globals);
 	if (globals->unix_sock < 0)
 		return -1;
 
-	gpsd_read_answer(globals);
+	ret = gpsd_read_answer(globals);
 	close(globals->unix_sock);
 
-	return 0;
+	return ret;
 }
 
 static void gpsd_connect_gpsd(struct globals *globals)
@@ -584,8 +602,10 @@ int main(int argc, char *argv[])
 		return gpsd_server(globals);
 		break;
 	case OPMODE_CLIENT:
-		return gpsd_get_data(globals);
-		break;
+		if (gpsd_get_data(globals) < 0)
+			return EXIT_FAILURE;
+
+		return EXIT_SUCCESS;
 	}
 
 	return EXIT_FAILURE;
Signature · 1 line
2.47.3
#6 lore raw

PATCH 6/7 alfred: gpsd: don't drop the rest of the answer for a bogus dataset

In reply to Sven Eckelmann

A dataset which is too short to even hold the length of the TPV string aborted the processing of the answer. The positions of all following nodes were dropped from the printed document although only the dataset of a single node was malformed.

Skip such a dataset like the other bogus datasets instead.

Fixes 2b901d69d8fb ("alfred: Add support for passing location information over alfred.")
---
 gpsd/alfred-gpsd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
gpsd/alfred-gpsd.c +1 -1
index 57ac9cf..8e2f1f8 100644
@@ -242,7 +242,7 @@ static int gpsd_read_answer(struct globals *globals)
 	while ((gpsd_data = gpsd_receive_answer_packet(globals->unix_sock,
 						       &len, source, &ret)) != NULL) {
 		if (len < sizeof(*gpsd_data))
-			break;
+			continue;
 
 		/* check size and skip bogus packets */
 		if (len != GPSD_DATA_SIZE(gpsd_data))
Signature · 1 line
2.47.3
#7 lore raw

PATCH 7/7 alfred: gpsd: handle empty or not 0-terminated TPV

In reply to Sven Eckelmann

gpsd_read_answer() printed the received TPV data with %s without verifying that the announced tpv_len bytes actually contain a terminating null byte. A record without termination made printf() read beyond the received data until the next null byte in memory.

Ensure printf is only outputting the content of the buffer. A record with tpv_len == 0 has no TPV bytes at all and must not be printed either.

Fixes 2b901d69d8fb ("alfred: Add support for passing location information over alfred.")
---
 gpsd/alfred-gpsd.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
gpsd/alfred-gpsd.c +7 -2
index 8e2f1f8..bac5711 100644
@@ -234,6 +234,7 @@ static int gpsd_read_answer(struct globals *globals)
 	struct gpsd_v1 *gpsd_data;
 	uint8_t source[ETH_ALEN];
 	bool first_line = true;
+	uint32_t tpv_len;
 	uint16_t len;
 	int ret = 0;
 
@@ -248,16 +249,20 @@ static int gpsd_read_answer(struct globals *globals)
 		if (len != GPSD_DATA_SIZE(gpsd_data))
 			continue;
 
+		tpv_len = ntohl(gpsd_data->tpv_len);
+		if (tpv_len == 0)
+			continue;
+
 		if (first_line)
 			first_line = false;
 		else
 			printf(",\n");
 
 		printf("  { \"source\" : \"%02x:%02x:%02x:%02x:%02x:%02x\", "
-		       "\"tpv\" : %s }",
+		       "\"tpv\" : %.*s }",
 		       source[0], source[1], source[2],
 		       source[3], source[4], source[5],
-		       gpsd_data->tpv);
+		       tpv_len, gpsd_data->tpv);
 	}
 	printf("\n]\n");
 
Signature · 1 line
2.47.3