[master] 91f6926e3 ws: Almost ban direct access to the workspace start

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Mon Aug 31 18:41:10 UTC 2020


commit 91f6926e3af1413664809f42d9ba3af826fa55a6
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Wed May 6 18:48:29 2020 +0200

    ws: Almost ban direct access to the workspace start
    
    To accomodate the optimization for session attributes new functions are
    added and with them extra checks.

diff --git a/bin/varnishd/cache/cache_session.c b/bin/varnishd/cache/cache_session.c
index 5e7994bab..41fec887e 100644
--- a/bin/varnishd/cache/cache_session.c
+++ b/bin/varnishd/cache/cache_session.c
@@ -89,10 +89,9 @@ ses_get_attr(const struct sess *sp, enum sess_attr a, void **dst)
 	if (sp->sattr[a] == 0xffff) {
 		*dst = NULL;
 		return (-1);
-	} else {
-		*dst = sp->ws->s + sp->sattr[a];
-		return (0);
 	}
+	*dst = WS_AtOffset(sp->ws, sp->sattr[a], 0);
+	return (0);
 }
 
 static int
@@ -106,7 +105,7 @@ ses_set_attr(const struct sess *sp, enum sess_attr a, const void *src, int sz)
 
 	if (sp->sattr[a] == 0xffff)
 		return (-1);
-	dst = sp->ws->s + sp->sattr[a];
+	dst = WS_AtOffset(sp->ws, sp->sattr[a], sz);
 	AN(dst);
 	memcpy(dst, src, sz);
 	return (0);
@@ -115,19 +114,18 @@ ses_set_attr(const struct sess *sp, enum sess_attr a, const void *src, int sz)
 static int
 ses_res_attr(struct sess *sp, enum sess_attr a, void **dst, int sz)
 {
-	ssize_t o;
+	unsigned o;
 
 	CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
 	assert(a < SA_LAST);
 	assert(sz >= 0);
 	AN(dst);
-	o = WS_ReserveSize(sp->ws, sz);
-	if (o < sz)
+	if (WS_ReserveSize(sp->ws, sz) == 0)
 		return (0);
 	*dst = WS_Reservation(sp->ws);
-	o = sp->ws->f - sp->ws->s;
+	o = WS_ReservationOffset(sp->ws);
 	WS_Release(sp->ws, sz);
-	assert(o >= 0 && o <= 0xffff);
+	assert(o <= 0xffff);
 	sp->sattr[a] = (uint16_t)o;
 	return (1);
 }
diff --git a/bin/varnishd/cache/cache_varnishd.h b/bin/varnishd/cache/cache_varnishd.h
index 339a89ddd..dca3a5cc2 100644
--- a/bin/varnishd/cache/cache_varnishd.h
+++ b/bin/varnishd/cache/cache_varnishd.h
@@ -465,6 +465,15 @@ void WRK_Init(void);
 
 /* cache_ws.c */
 void WS_Rollback(struct ws *, uintptr_t);
+void *WS_AtOffset(const struct ws *ws, unsigned off, unsigned len);
+
+static inline unsigned
+WS_ReservationOffset(const struct ws *ws)
+{
+
+	AN(ws->r);
+	return (ws->f - ws->s);
+}
 
 /* http1/cache_http1_pipe.c */
 void V1P_Init(void);
diff --git a/bin/varnishd/cache/cache_ws.c b/bin/varnishd/cache/cache_ws.c
index 64defc78b..7f51b5379 100644
--- a/bin/varnishd/cache/cache_ws.c
+++ b/bin/varnishd/cache/cache_ws.c
@@ -351,6 +351,17 @@ WS_Overflowed(const struct ws *ws)
 	return (1);
 }
 
+void *
+WS_AtOffset(const struct ws *ws, unsigned off, unsigned len)
+{
+	char *ptr;
+
+	WS_Assert(ws);
+	ptr = ws->s + off;
+	WS_Assert_Allocated(ws, ptr, len);
+	return (ptr);
+}
+
 /*---------------------------------------------------------------------
  * Build a VSB on a workspace.
  * Usage pattern:


More information about the varnish-commit mailing list