Skip to content

Commit

Permalink
MDEV-26228 ASAN heap-use-after-free with ON UPDATE CASCADE
Browse files Browse the repository at this point in the history
In commit 83d2e08 (MDEV-24041)
we failed to notice that in addition to the bug with
DELETE and ON DELETE CASCADE, there is another bug with
UPDATE and ON UPDATE CASCADE.

row_ins_foreign_fill_virtual(): Use the correct memory heap
for everything that will be reachable from the cascade->update
that we return to the caller.

Note: It is correct to use the shorter-lived cascade->heap for
rec_get_offsets(), because that memory will be abandoned when
row_ins_foreign_fill_virtual() returns.
  • Loading branch information
dr-m committed Jul 23, 2021
1 parent 4c4237e commit 173e562
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
3 changes: 3 additions & 0 deletions mysql-test/suite/gcol/r/innodb_virtual_fk.result
Original file line number Diff line number Diff line change
Expand Up @@ -809,15 +809,18 @@ generated_email_id int as (email_id),
PRIMARY KEY (id),
KEY mautic_generated_sent_date_email_id (generated_email_id),
FOREIGN KEY (email_id) REFERENCES emails (id) ON DELETE SET NULL
ON UPDATE CASCADE
) ENGINE=InnoDB;
CREATE TABLE emails_metadata (
email_id int,
PRIMARY KEY (email_id),
CONSTRAINT FK FOREIGN KEY (email_id) REFERENCES emails (id) ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB;
INSERT INTO emails VALUES (1);
INSERT INTO email_stats (id, email_id, date_sent) VALUES (1,1,'Jan');
INSERT INTO emails_metadata VALUES (1);
UPDATE emails SET id=2;
DELETE FROM emails;
DROP TABLE email_stats;
DROP TABLE emails_metadata;
Expand Down
3 changes: 3 additions & 0 deletions mysql-test/suite/gcol/t/innodb_virtual_fk.test
Original file line number Diff line number Diff line change
Expand Up @@ -670,20 +670,23 @@ CREATE TABLE email_stats (
PRIMARY KEY (id),
KEY mautic_generated_sent_date_email_id (generated_email_id),
FOREIGN KEY (email_id) REFERENCES emails (id) ON DELETE SET NULL
ON UPDATE CASCADE
) ENGINE=InnoDB;


CREATE TABLE emails_metadata (
email_id int,
PRIMARY KEY (email_id),
CONSTRAINT FK FOREIGN KEY (email_id) REFERENCES emails (id) ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB;


INSERT INTO emails VALUES (1);
INSERT INTO email_stats (id, email_id, date_sent) VALUES (1,1,'Jan');
INSERT INTO emails_metadata VALUES (1);

UPDATE emails SET id=2;
DELETE FROM emails;

DROP TABLE email_stats;
Expand Down
6 changes: 3 additions & 3 deletions storage/innobase/row/row0ins.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*****************************************************************************
Copyright (c) 1996, 2016, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2016, 2020, MariaDB Corporation.
Copyright (c) 2016, 2021, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Expand Down Expand Up @@ -969,8 +969,8 @@ row_ins_foreign_fill_virtual(
upd_field = update->fields + n_diff;

upd_field->old_v_val = static_cast<dfield_t*>(
mem_heap_alloc(cascade->heap,
sizeof *upd_field->old_v_val));
mem_heap_alloc(update->heap,
sizeof *upd_field->old_v_val));

dfield_copy(upd_field->old_v_val, vfield);

Expand Down

0 comments on commit 173e562

Please sign in to comment.