Skip to content

Commit 1a7b62d

Browse files
authored
bpo-35050: AF_ALG length check off-by-one error (GH-10058) (GH-11069)
The length check for AF_ALG salg_name and salg_type had a off-by-one error. The code assumed that both values are not necessarily NULL terminated. However the Kernel code for alg_bind() ensures that the last byte of both strings are NULL terminated. Signed-off-by: Christian Heimes <christian@python.org> (cherry picked from commit 2eb6ad8)
1 parent c3cc751 commit 1a7b62d

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

Lib/test/test_socket.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5944,6 +5944,24 @@ def test_sendmsg_afalg_args(self):
59445944
with self.assertRaises(TypeError):
59455945
sock.sendmsg_afalg(op=socket.ALG_OP_ENCRYPT, assoclen=-1)
59465946

5947+
def test_length_restriction(self):
5948+
# bpo-35050, off-by-one error in length check
5949+
sock = socket.socket(socket.AF_ALG, socket.SOCK_SEQPACKET, 0)
5950+
self.addCleanup(sock.close)
5951+
5952+
# salg_type[14]
5953+
with self.assertRaises(FileNotFoundError):
5954+
sock.bind(("t" * 13, "name"))
5955+
with self.assertRaisesRegex(ValueError, "type too long"):
5956+
sock.bind(("t" * 14, "name"))
5957+
5958+
# salg_name[64]
5959+
with self.assertRaises(FileNotFoundError):
5960+
sock.bind(("type", "n" * 63))
5961+
with self.assertRaisesRegex(ValueError, "name too long"):
5962+
sock.bind(("type", "n" * 64))
5963+
5964+
59475965
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
59485966
class TestMSWindowsTCPFlags(unittest.TestCase):
59495967
knownTCPFlags = {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:mod:`socket`: Fix off-by-one bug in length check for ``AF_ALG`` name and type.

Modules/socketmodule.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,14 +2157,18 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
21572157

21582158
if (!PyArg_ParseTuple(args, "ss|HH:getsockaddrarg",
21592159
&type, &name, &sa->salg_feat, &sa->salg_mask))
2160+
{
21602161
return 0;
2161-
/* sockaddr_alg has fixed-sized char arrays for type and name */
2162-
if (strlen(type) > sizeof(sa->salg_type)) {
2162+
}
2163+
/* sockaddr_alg has fixed-sized char arrays for type, and name
2164+
* both must be NULL terminated.
2165+
*/
2166+
if (strlen(type) >= sizeof(sa->salg_type)) {
21632167
PyErr_SetString(PyExc_ValueError, "AF_ALG type too long.");
21642168
return 0;
21652169
}
21662170
strncpy((char *)sa->salg_type, type, sizeof(sa->salg_type));
2167-
if (strlen(name) > sizeof(sa->salg_name)) {
2171+
if (strlen(name) >= sizeof(sa->salg_name)) {
21682172
PyErr_SetString(PyExc_ValueError, "AF_ALG name too long.");
21692173
return 0;
21702174
}

0 commit comments

Comments
 (0)