Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2eb6ad8

Browse files
tiranvstinner
authored andcommitted
bpo-35050: AF_ALG length check off-by-one error (GH-10058)
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 <[email protected]>
1 parent 8e04186 commit 2eb6ad8

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

Lib/test/test_socket.py

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

5972+
def test_length_restriction(self):
5973+
# bpo-35050, off-by-one error in length check
5974+
sock = socket.socket(socket.AF_ALG, socket.SOCK_SEQPACKET, 0)
5975+
self.addCleanup(sock.close)
5976+
5977+
# salg_type[14]
5978+
with self.assertRaises(FileNotFoundError):
5979+
sock.bind(("t" * 13, "name"))
5980+
with self.assertRaisesRegex(ValueError, "type too long"):
5981+
sock.bind(("t" * 14, "name"))
5982+
5983+
# salg_name[64]
5984+
with self.assertRaises(FileNotFoundError):
5985+
sock.bind(("type", "n" * 63))
5986+
with self.assertRaisesRegex(ValueError, "name too long"):
5987+
sock.bind(("type", "n" * 64))
5988+
5989+
59725990
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
59735991
class TestMSWindowsTCPFlags(unittest.TestCase):
59745992
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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2245,13 +2245,15 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
22452245
{
22462246
return 0;
22472247
}
2248-
/* sockaddr_alg has fixed-sized char arrays for type and name */
2249-
if (strlen(type) > sizeof(sa->salg_type)) {
2248+
/* sockaddr_alg has fixed-sized char arrays for type, and name
2249+
* both must be NULL terminated.
2250+
*/
2251+
if (strlen(type) >= sizeof(sa->salg_type)) {
22502252
PyErr_SetString(PyExc_ValueError, "AF_ALG type too long.");
22512253
return 0;
22522254
}
22532255
strncpy((char *)sa->salg_type, type, sizeof(sa->salg_type));
2254-
if (strlen(name) > sizeof(sa->salg_name)) {
2256+
if (strlen(name) >= sizeof(sa->salg_name)) {
22552257
PyErr_SetString(PyExc_ValueError, "AF_ALG name too long.");
22562258
return 0;
22572259
}

0 commit comments

Comments
 (0)